58 lines
1.1 KiB
Go
58 lines
1.1 KiB
Go
|
// Package router defines the base routes http server
|
||
|
package router
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"net/http"
|
||
|
"os"
|
||
|
"strings"
|
||
|
"time"
|
||
|
|
||
|
"git.staur.ca/stobbsm/clustvirt/lib/log"
|
||
|
"github.com/go-chi/chi/v5"
|
||
|
"github.com/go-chi/chi/v5/middleware"
|
||
|
)
|
||
|
|
||
|
type Server struct {
|
||
|
bindAddr string
|
||
|
ssl bool
|
||
|
middleware []http.Handler
|
||
|
}
|
||
|
|
||
|
// New creates a new HTTP Server instance.
|
||
|
// Requires the IP and port number to bind to
|
||
|
func New(listen string, port int) *Server {
|
||
|
s := &Server{bindAddr: fmt.Sprintf("%s:%d", listen, port)}
|
||
|
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
// Start starts the server and initializes the router and common middleware
|
||
|
func (s *Server) Start() {
|
||
|
tstart := time.Now()
|
||
|
defer func() {
|
||
|
log.Info("router.Server.Start").
|
||
|
Dur("upTime", time.Since(tstart)).
|
||
|
Msg("http server stopped")
|
||
|
}()
|
||
|
router := chi.NewRouter()
|
||
|
|
||
|
indev, _ := os.LookupEnv("CLUSTVIRT_DEV")
|
||
|
indev = strings.ToLower(indev)
|
||
|
switch indev {
|
||
|
case "true":
|
||
|
fallthrough
|
||
|
case "1":
|
||
|
fallthrough
|
||
|
case "yes":
|
||
|
fallthrough
|
||
|
case "on":
|
||
|
router.Use(middleware.NoCache)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (s *Server) AddMiddleware(m http.Handler) {
|
||
|
s.middleware = append(s.middleware,
|
||
|
m)
|
||
|
}
|