sermoni

"Service monitor" / cronjob status service
Log | Files | Refs

commit 19ecf03e7709502ddf30c82b0969be571946a521
parent a229d5639ef84c3c7f2012688542b56f231e3bab
Author: Vetle Haflan <vetle@haflan.dev>
Date:   Sun, 12 Apr 2020 06:03:55 +0200

Some work on http request handlers

Diffstat:
Mgo.mod | 1+
Ainternal/http/auth.go | 45+++++++++++++++++++++++++++++++++++++++++++++
Ainternal/http/events.go | 24++++++++++++++++++++++++
Minternal/http/http.go | 37+++++++++++++++----------------------
Ainternal/http/services.go | 15+++++++++++++++
5 files changed, 100 insertions(+), 22 deletions(-)

diff --git a/go.mod b/go.mod @@ -4,6 +4,7 @@ go 1.13 require ( github.com/fzipp/gocyclo v0.0.0-20150627053110-6acd4345c835 // indirect + github.com/gorilla/mux v1.7.4 github.com/gorilla/securecookie v1.1.1 github.com/gorilla/sessions v1.2.0 go.etcd.io/bbolt v1.3.4 diff --git a/internal/http/auth.go b/internal/http/auth.go @@ -0,0 +1,45 @@ +package http + +import ( + "log" + "net/http" + + "github.com/gorilla/sessions" +) + +func authorized(session *sessions.Session) bool { + val := session.Values["authenticated"] + auth, ok := val.(bool) + return ok && auth +} + +func loginHandler(w http.ResponseWriter, r *http.Request) { + session, _ := store.Get(r, "session") + if authorized(session) { + log.Println("Authenticated session requested website") + w.Write([]byte("logged in")) + } else { + log.Println("New session requested website") + session.Values["authenticated"] = true + log.Println(session.Save(r, w)) + w.Write([]byte("Not logged in")) + } + return +} + +func logoutHandler(w http.ResponseWriter, r *http.Request) { + session, _ := store.Get(r, "session") + session.Values["authenticated"] = false + log.Println(session.Save(r, w)) + w.Write([]byte("Logged out")) +} + +// Auth returns Authentication middleware for the simple sermoni auth scheme +// This doesn' work :v +func Auth() func(http.Handler) http.Handler { + return func(h http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h.ServeHTTP(w, r) + }) + } +} diff --git a/internal/http/events.go b/internal/http/events.go @@ -0,0 +1,24 @@ +package http + +import ( + "net/http" +) + +func getEvents(w http.ResponseWriter, r *http.Request) { + return +} + +// TODO: This is still a placeholder! +func deleteEvent(w http.ResponseWriter, r *http.Request) { + /* + vars := mux.Vars(r) + id, _ := strconv.ParseUint(vars["id"], 10, 64) + err := events.Delete(id) + fmt.Println(err) + */ + return +} + +func reportEvent(w http.ResponseWriter, r *http.Request) { + return +} diff --git a/internal/http/http.go b/internal/http/http.go @@ -7,6 +7,7 @@ import ( "sermoni/internal/config" + "github.com/gorilla/mux" "github.com/gorilla/sessions" ) @@ -19,9 +20,21 @@ func StartServer(port int) { conf = config.GetConfig() store = sessions.NewCookieStore(conf.SessionKey) - http.HandleFunc("/", homeHandler) - http.HandleFunc("/login", loginHandler) + router := mux.NewRouter() + router.HandleFunc("/", homeHandler) + router.HandleFunc("/login", loginHandler) + router.HandleFunc("/logout", logoutHandler) + router.HandleFunc("/services", getServices).Methods("GET") + router.HandleFunc("/services", postService).Methods("POST") + router.HandleFunc("/services/{id:[0-9]+}", deleteService).Methods("DELETE") + //router.HandleFunc("/services/{id:[0-9]+}", putService).Methods("PUT") (TODO) + + router.HandleFunc("/events", getEvents).Methods("GET") + router.HandleFunc("/events/{id:[0-9]+}", deleteEvent).Methods("DELETE") + + router.HandleFunc("/report", reportEvent).Methods("POST") + http.Handle("/", router) log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", port), nil)) } @@ -30,23 +43,3 @@ func homeHandler(w http.ResponseWriter, r *http.Request) { w.Write(getWebsite()) return } - -func authorized(session *sessions.Session) bool { - val := session.Values["authenticated"] - auth, ok := val.(bool) - return ok && auth -} - -func loginHandler(w http.ResponseWriter, r *http.Request) { - session, _ := store.Get(r, "session") - if authorized(session) { - log.Println("Authenticated session requested website") - w.Write([]byte("logged in")) - } else { - log.Println("New session requested website") - session.Values["authenticated"] = true - log.Println(session.Save(r, w)) - w.Write([]byte("Not logged in")) - } - return -} diff --git a/internal/http/services.go b/internal/http/services.go @@ -0,0 +1,15 @@ +package http + +import ( + "net/http" +) + +func getServices(w http.ResponseWriter, r *http.Request) { + return +} +func postService(w http.ResponseWriter, r *http.Request) { + return +} +func deleteService(w http.ResponseWriter, r *http.Request) { + return +}