linstrom/server/server.go

38 lines
891 B
Go
Raw Normal View History

package server
import (
"net/http"
"github.com/julienschmidt/httprouter"
"gitlab.com/mstarongitlab/linstrom/config"
"gitlab.com/mstarongitlab/linstrom/guardian"
"gitlab.com/mstarongitlab/linstrom/storage"
"gitlab.com/mstarongitlab/weblogger"
)
type Server struct {
// Register routes on here
Router *httprouter.Router
// Where to read and write stuff from
Storage *storage.Storage
// 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)
}