add session handling
This commit is contained in:
parent
5c7d92f4f3
commit
a80dac4985
10 changed files with 64 additions and 12 deletions
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
20
web/sessions.go
Normal 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
|
||||
}
|
|
@ -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 {
|
||||
|
|
Reference in a new issue