package server 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 } 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{}, } } func (s *Server) BuildUi() { s.Ui.CompilePages(s.Config.Server.UiPath) } func (s *Server) RegisterRoutes(routes Routes) { for _, r := range routes { s.Router.Method(r.Method, r.Path, r.HandlerFunc(s)) } } 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, }) }