add session handling

This commit is contained in:
Philipp 2021-08-29 17:09:48 +02:00
parent 5c7d92f4f3
commit a80dac4985
No known key found for this signature in database
GPG Key ID: 276B613AF9DBE9C3
10 changed files with 64 additions and 12 deletions

View File

@ -9,12 +9,19 @@ import (
)
func main() {
store, err := postgres.NewStore("postgres://postgres:secret@localhost/postgres?sslmode=disable")
dsn := "postgres://postgres:secret@localhost/postgres?sslmode=disable"
store, err := postgres.NewStore(dsn)
if err != nil {
log.Fatal(err)
}
sessions, err := web.NewSessionsManager(dsn)
if err != nil {
log.Fatal(err)
}
csrfKey := []byte("dmwij82jda92jf9a202na#d2.e3i!824")
h := web.NewHandler(store, csrfKey)
h := web.NewHandler(store, sessions, csrfKey)
http.ListenAndServe(":3000", h)
}

2
go.mod
View File

@ -3,6 +3,8 @@ module git.snrd.de/Spaenny/goddit
go 1.16
require (
github.com/alexedwards/scs/postgresstore v0.0.0-20210822164020-33a92ced6c04 // indirect
github.com/alexedwards/scs/v2 v2.4.0 // indirect
github.com/go-chi/chi v1.5.4 // indirect
github.com/google/uuid v1.3.0 // direct
github.com/gorilla/csrf v1.7.1 // indirect

5
go.sum
View File

@ -1,3 +1,7 @@
github.com/alexedwards/scs/postgresstore v0.0.0-20210822164020-33a92ced6c04 h1:LDnHq6qDNQmLaYsC4jxMieUalD0HlGfGrqFXoeTdMaI=
github.com/alexedwards/scs/postgresstore v0.0.0-20210822164020-33a92ced6c04/go.mod h1:TDDdV/xnjj+/4zBQ9a2k+i2AbuAdY7SQjPUh5zoTZ3M=
github.com/alexedwards/scs/v2 v2.4.0 h1:XfnMamKnvp1muJVNr1WzikQTclopsBXWZtzz0NBjOK0=
github.com/alexedwards/scs/v2 v2.4.0/go.mod h1:ToaROZxyKukJKT/xLcVQAChi5k6+Pn1Gvmdl7h3RRj8=
github.com/cespare/reflex v0.3.1 h1:N4Y/UmRrjwOkNT0oQQnYsdr6YBxvHqtSfPB4mqOyAKk=
github.com/cespare/reflex v0.3.1/go.mod h1:I+0Pnu2W693i7Hv6ZZG76qHTY0mgUa7uCIfCtikXojE=
github.com/creack/pty v1.1.11 h1:07n33Z8lZxZ2qwegKbObQohDhXDQxiMMz1NOUGYlesw=
@ -21,6 +25,7 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.4.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.10.2 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8=
github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=

View File

@ -0,0 +1 @@
DROP TABLE sessions;

View File

@ -0,0 +1,7 @@
CREATE TABLE sessions (
token TEXT PRIMARY KEY,
data BYTEA NOT NULL,
expiry TIMESTAMPTZ NOT NULL
);
CREATE INDEX sessions_expiry_idx ON sessions (expiry);

View File

@ -4,12 +4,14 @@ import (
"net/http"
"git.snrd.de/Spaenny/goddit"
"github.com/alexedwards/scs/v2"
"github.com/go-chi/chi"
"github.com/google/uuid"
)
type CommentHandler struct {
store goddit.Store
store goddit.Store
sessions *scs.SessionManager
}
func (h *CommentHandler) Store() http.HandlerFunc {

View File

@ -5,23 +5,26 @@ import (
"net/http"
"git.snrd.de/Spaenny/goddit"
"github.com/alexedwards/scs/v2"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/gorilla/csrf"
)
func NewHandler(store goddit.Store, csrfKey []byte) *Handler {
func NewHandler(store goddit.Store, sessions *scs.SessionManager, csrfKey []byte) *Handler {
h := &Handler{
Mux: chi.NewMux(),
store: store,
Mux: chi.NewMux(),
store: store,
sessions: sessions,
}
threads := ThreadHandler{store: store}
posts := PostHandler{store: store}
comments := CommentHandler{store: store}
threads := ThreadHandler{store: store, sessions: sessions}
posts := PostHandler{store: store, sessions: sessions}
comments := CommentHandler{store: store, sessions: sessions}
h.Use(middleware.Logger)
h.Use(csrf.Protect(csrfKey, csrf.Secure(false)))
h.Use(sessions.LoadAndSave)
h.Get("/", h.Home())
h.Route("/threads", func(r chi.Router) {
@ -44,7 +47,8 @@ func NewHandler(store goddit.Store, csrfKey []byte) *Handler {
type Handler struct {
*chi.Mux
store goddit.Store
store goddit.Store
sessions *scs.SessionManager
}
func (h *Handler) Home() http.HandlerFunc {

View File

@ -5,13 +5,15 @@ import (
"net/http"
"git.snrd.de/Spaenny/goddit"
"github.com/alexedwards/scs/v2"
"github.com/go-chi/chi"
"github.com/google/uuid"
"github.com/gorilla/csrf"
)
type PostHandler struct {
store goddit.Store
store goddit.Store
sessions *scs.SessionManager
}
func (h *PostHandler) Create() http.HandlerFunc {

20
web/sessions.go Normal file
View File

@ -0,0 +1,20 @@
package web
import (
"database/sql"
"github.com/alexedwards/scs/postgresstore"
"github.com/alexedwards/scs/v2"
)
func NewSessionsManager(dataSourceName string) (*scs.SessionManager, error) {
db, err := sql.Open("postgres", dataSourceName)
if err != nil {
return nil, err
}
sessions := scs.New()
sessions.Store = postgresstore.New(db)
return sessions, nil
}

View File

@ -5,13 +5,15 @@ import (
"net/http"
"git.snrd.de/Spaenny/goddit"
"github.com/alexedwards/scs/v2"
"github.com/go-chi/chi"
"github.com/google/uuid"
"github.com/gorilla/csrf"
)
type ThreadHandler struct {
store goddit.Store
store goddit.Store
sessions *scs.SessionManager
}
func (h *ThreadHandler) List() http.HandlerFunc {