2024-01-19 10:21:31 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-01-29 17:15:28 +00:00
|
|
|
"net/http"
|
|
|
|
|
2024-01-19 10:21:31 +00:00
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
"gitlab.com/mstarongitlab/linstrom/config"
|
2024-01-29 17:15:28 +00:00
|
|
|
"gitlab.com/mstarongitlab/linstrom/guardian"
|
2024-01-19 10:21:31 +00:00
|
|
|
"gitlab.com/mstarongitlab/linstrom/storage"
|
2024-01-29 17:15:28 +00:00
|
|
|
"gitlab.com/mstarongitlab/weblogger"
|
2024-01-19 10:21:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2024-01-29 17:15:28 +00:00
|
|
|
// Register routes on here
|
|
|
|
Router *httprouter.Router
|
|
|
|
// Where to read and write stuff from
|
2024-01-19 10:21:31 +00:00
|
|
|
Storage *storage.Storage
|
2024-01-29 17:15:28 +00:00
|
|
|
// Global config. READ ONLY
|
|
|
|
Config *config.Config
|
|
|
|
// Final handler given to http.ListenAndServe
|
|
|
|
// Can be any router or middleware wrapped around a router
|
|
|
|
handler http.Handler
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewServer(cfg *config.Config, s *storage.Storage) *Server {
|
|
|
|
router := httprouter.New()
|
|
|
|
return &Server{
|
|
|
|
Router: router,
|
|
|
|
Storage: s,
|
|
|
|
Config: cfg,
|
|
|
|
handler: guardian.NewMiddleware(weblogger.LoggingMiddleware(router), cfg, s),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Run() {
|
|
|
|
http.ListenAndServe(":8080", s.handler)
|
2024-01-19 10:21:31 +00:00
|
|
|
}
|