aboutsummaryrefslogtreecommitdiff
path: root/src/server/server.go
blob: aa9f7d2531f07477961572d8cac59f311a52eda6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package server

import (
	"net/http"

	"github.com/go-chi/chi/v5"
	"github.com/jackc/pgx/v5/pgxpool"
	"gitlab.com/alexkavon/newsstand/src/conf"
)

type Server struct {
	Router *chi.Mux
	Db     *pgxpool.Pool
	Config *conf.Conf
	Ui     Ui
}

func NewServer(config *conf.Conf, db *pgxpool.Pool) *Server {
	return &Server{
		Router: NewRouter(config),
		Db:     db,
		Config: config,
		Ui:     NewUi(config),
	}
}

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)
}