This repository has been archived on 2023-11-13. You can view files and clone it, but cannot push or open issues or pull requests.
imagini/cmd/server/server.go

58 lines
1.2 KiB
Go
Raw Normal View History

2021-01-16 22:00:17 +00:00
package server
import (
"time"
"context"
"net/http"
"reichard.io/imagini/internal/db"
"reichard.io/imagini/internal/api"
"reichard.io/imagini/internal/auth"
"reichard.io/imagini/internal/config"
log "github.com/sirupsen/logrus"
)
type Server struct {
2021-01-18 21:24:28 +00:00
API *api.API
Auth *auth.AuthManager
Config *config.Config
2021-01-16 22:00:17 +00:00
Database *db.DBManager
httpServer *http.Server
}
func NewServer() *Server {
2021-01-18 04:56:56 +00:00
c := config.Load()
db := db.NewMgr(c)
auth := auth.NewMgr(db, c)
2021-01-16 22:00:17 +00:00
api := api.NewApi(db, auth)
return &Server{
API: api,
Auth: auth,
2021-01-18 04:56:56 +00:00
Config: c,
2021-01-16 22:00:17 +00:00
Database: db,
}
}
func (s *Server) StartServer() {
listenAddr := (":" + s.Config.ListenPort)
s.httpServer = &http.Server{
Handler: s.API.Router,
Addr: listenAddr,
}
go func() {
err := s.httpServer.ListenAndServe()
if err != nil {
log.Error("Error starting server ", err)
2021-01-18 21:24:28 +00:00
return
}
2021-01-16 22:00:17 +00:00
}()
}
func (s *Server) StopServer() {
ctx, cancel := context.WithTimeout(context.Background(), 5 * time.Second)
defer cancel()
s.httpServer.Shutdown(ctx)
}