aboutsummaryrefslogtreecommitdiffstats
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go27
1 files changed, 18 insertions, 9 deletions
diff --git a/main.go b/main.go
index 2c79316..5317ffa 100644
--- a/main.go
+++ b/main.go
@@ -1,13 +1,16 @@
package main
import (
+ "html/template"
"log"
"net/http"
+ "strconv"
"time"
)
type App struct {
Store
+ Template *template.Template
}
func logMux(handler http.Handler) http.Handler {
@@ -18,15 +21,15 @@ func logMux(handler http.Handler) http.Handler {
})
}
-func (app *App) rootHandler(w http.ResponseWriter, r *http.Request) {
+func (app *App) validateHandler(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)
+ if s, ok := app.GetSession(c.Value); ok {
+ w.Header().Set("X-Remote-User", strconv.FormatInt(s.UserId, 10))
w.WriteHeader(http.StatusOK)
} else {
log.Println("Session does not exist:", c.Value)
@@ -42,20 +45,26 @@ func (app *App) loginHandler(w http.ResponseWriter, r *http.Request) {
} else {
username := r.Form.Get("username")
password := r.Form.Get("password")
- row := app.Query("SELECT id FROM users WHERE user_name = ?", username)
- var id int64
- if err := row.rowScan(&id); err != nil {
- panic(err)
+ u, ok := app.GetUser(username)
+ if ok && (u.Password == password) {
+
+ } else {
+
}
}
+ } else if r.Method == http.MethodGet {
+ app.Template.ExecuteTemplate(w, "login.tmpl", nil)
}
+
}
func main() {
//log.SetFlags(log.LstdFlags)
store := NewPgStore()
- app := &App{store}
- http.HandleFunc("/", app.rootHandler)
+ template := template.Must(template.New("").ParseGlob("templates/*.tmpl"))
+ app := &App{store, template}
+ http.HandleFunc("/validate", app.validateHandler)
+ http.HandleFunc("/login", app.loginHandler)
if err := http.ListenAndServe(":8080", logMux(http.DefaultServeMux)); err != nil {
panic(err)
}