From 218cd643550ed00d26b6e9772d8a64875b5abf2e Mon Sep 17 00:00:00 2001 From: Alexander Kavon Date: Thu, 30 Nov 2023 01:43:08 -0500 Subject: initial sessions support via sessions pkg, includes session middleware for setting sessions, guest sessions, method to return array of middlewares --- src/server/server.go | 32 +++++++++----------------------- 1 file changed, 9 insertions(+), 23 deletions(-) (limited to 'src/server/server.go') diff --git a/src/server/server.go b/src/server/server.go index c483069..eb5f785 100644 --- a/src/server/server.go +++ b/src/server/server.go @@ -4,26 +4,23 @@ import ( "net/http" "github.com/go-chi/chi/v5" - "github.com/google/uuid" "gitlab.com/alexkavon/newsstand/src/conf" "gitlab.com/alexkavon/newsstand/src/db" ) type Server struct { - Router *chi.Mux - Db *db.Database - Config *conf.Conf - Ui Ui - Sessions map[string]session + Router *chi.Mux + Db *db.Database + Config *conf.Conf + Ui Ui } func NewServer(config *conf.Conf, database *db.Database) *Server { return &Server{ - Router: NewRouter(config), - Db: database, - Config: config, - Ui: NewUi(config), - Sessions: map[string]session{}, + Router: NewRouter(config), + Db: database, + Config: config, + Ui: NewUi(config), } } @@ -33,6 +30,7 @@ func (s *Server) BuildUi() { func (s *Server) RegisterRoutes(routes Routes) { for _, r := range routes { + s.Router.Use(r.Middlewares...) s.Router.Method(r.Method, r.Path, r.HandlerFunc(s)) } } @@ -40,15 +38,3 @@ func (s *Server) RegisterRoutes(routes Routes) { func (s *Server) Serve() { http.ListenAndServe(":"+s.Config.Server.Port, s.Router) } - -func (s *Server) NewSession(w http.ResponseWriter, username string) { - token := uuid.NewString() - s.Sessions[token] = session{ - username: username, - } - - http.SetCookie(w, &http.Cookie{ - Name: "session_token", - Value: token, - }) -} -- cgit v1.2.3