diff options
| author | Thibaut Horel <thibaut.horel@gmail.com> | 2018-12-26 13:20:57 -0500 |
|---|---|---|
| committer | Thibaut Horel <thibaut.horel@gmail.com> | 2018-12-26 13:20:57 -0500 |
| commit | ed4f6fa1a785e14f39bda9715e3d081fb426ff36 (patch) | |
| tree | b3b9e4095281665a06b96e840133d563224c9fca /main.go | |
| download | id-ed4f6fa1a785e14f39bda9715e3d081fb426ff36.tar.gz | |
Initial commit
Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 46 |
1 files changed, 46 insertions, 0 deletions
@@ -0,0 +1,46 @@ +package main + +import ( + "log" + "net/http" + "time" +) + +type App struct { + SessionStore +} + +func logMux(handler http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + t := time.Now() + handler.ServeHTTP(w, r) + log.Println(r.Method, r.URL.Path, time.Since(t)) + }) +} + +func (app *App) rootHandler(w http.ResponseWriter, r *http.Request) { + c, err := r.Cookie("id") + //log.Println(r.Header.Get("X-Original-URI")) + //log.Println(r.Host) + if err != nil { + w.WriteHeader(http.StatusUnauthorized) + } else { + if s, ok := app.Get(c.Value); ok { + w.Header().Set("X-Remote-User", s.UserId) + w.WriteHeader(http.StatusOK) + } else { + log.Println("Session does not exist:", c.Value) + w.WriteHeader(http.StatusForbidden) + } + } +} + +func main() { + //log.SetFlags(log.LstdFlags) + store := NewPgStore() + app := &App{store} + http.HandleFunc("/", app.rootHandler) + if err := http.ListenAndServe(":8080", logMux(http.DefaultServeMux)); err != nil { + panic(err) + } +} |
