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

64 lines
1.1 KiB
Go
Raw Normal View History

2021-02-11 20:47:42 +00:00
package server
import (
"context"
"net/http"
"time"
2021-02-21 17:31:03 +00:00
"github.com/davidbyttow/govips/v2/vips"
2021-02-11 20:47:42 +00:00
log "github.com/sirupsen/logrus"
"reichard.io/imagini/internal/api"
"reichard.io/imagini/internal/auth"
"reichard.io/imagini/internal/config"
"reichard.io/imagini/internal/db"
)
type Server struct {
API *api.API
Auth *auth.AuthManager
Config *config.Config
Database *db.DBManager
httpServer *http.Server
}
func NewServer() *Server {
c := config.Load()
db := db.NewMgr(c)
auth := auth.NewMgr(db, c)
api := api.NewApi(db, c, auth)
return &Server{
API: api,
Auth: auth,
Config: c,
Database: db,
}
}
func (s *Server) StartServer() {
listenAddr := (":" + s.Config.ListenPort)
s.httpServer = &http.Server{
Handler: s.API.Router,
Addr: listenAddr,
}
go func() {
2021-02-21 17:31:03 +00:00
vips.Startup(nil)
defer vips.Shutdown()
2021-02-11 20:47:42 +00:00
err := s.httpServer.ListenAndServe()
if err != nil {
log.Error("Error starting server ", err)
return
}
}()
}
func (s *Server) StopServer() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
s.httpServer.Shutdown(ctx)
}