aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuillaume Horel <guillaume.horel@gmail.com>2018-12-27 07:57:48 -0500
committerGuillaume Horel <guillaume.horel@gmail.com>2018-12-27 07:57:48 -0500
commitb953a2c81fa8a66a58a9c0ec185da48ce853e8d9 (patch)
tree86fd378f3b38a3ea9343160d61472115ec50a179
parented4f6fa1a785e14f39bda9715e3d081fb426ff36 (diff)
downloadid-b953a2c81fa8a66a58a9c0ec185da48ce853e8d9.tar.gz
WIP: not working
-rw-r--r--main.go18
-rw-r--r--store.go19
2 files changed, 32 insertions, 5 deletions
diff --git a/main.go b/main.go
index 7d172b7..2c79316 100644
--- a/main.go
+++ b/main.go
@@ -7,7 +7,7 @@ import (
)
type App struct {
- SessionStore
+ Store
}
func logMux(handler http.Handler) http.Handler {
@@ -35,6 +35,22 @@ func (app *App) rootHandler(w http.ResponseWriter, r *http.Request) {
}
}
+func (app *App) loginHandler(w http.ResponseWriter, r *http.Request) {
+ if r.Method == http.MethodPost {
+ if err := r.ParseForm(); err != nil {
+ panic(err)
+ } 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)
+ }
+ }
+ }
+}
+
func main() {
//log.SetFlags(log.LstdFlags)
store := NewPgStore()
diff --git a/store.go b/store.go
index b744d88..55d808b 100644
--- a/store.go
+++ b/store.go
@@ -9,14 +9,20 @@ import (
type Session struct {
Id string
- UserId string
+ UserId int64
}
-type SessionStore interface {
- Get(id string) (*Session, bool)
+type User struct {
+ Id int64
+ UserName string
}
-type PgSessionStore struct {
+type Store interface {
+ GetSession(id string) (*Session, bool)
+ GetUser(id int64) (*User, bool)
+}
+
+type PgStore struct {
*sql.DB
cache map[string]*Session
}
@@ -31,5 +37,10 @@ func NewPgStore() *PgSessionStore {
func (store *PgSessionStore) Get(id string) (*Session, bool) {
s, ok := store.cache[id]
+ if !ok {
+ row := store.QueryRow("SELECT id, user_id FROM session WHERE id = ?", id)
+ var s Session
+ row.Scan(&s.
+ }
return s, ok
}