first compiling version

This commit is contained in:
ston1th 2023-03-15 01:51:45 +01:00
commit 3d54199faa
27 changed files with 908 additions and 243 deletions

View file

@ -24,13 +24,7 @@ type Server struct {
Data *types.ContextData
init chan struct{}
stop chan struct{}
stopKeyReset chan struct{}
listen net.Listener
db *db.DB
}
type notFoundHandler struct {
@ -46,11 +40,7 @@ func NewServer(log logr.Logger, listen string) (*Server, error) {
s := &Server{
mux: mux.NewRouter(),
log: log,
Data: &types.ContextData{},
init: make(chan struct{}),
stop: make(chan struct{}),
stopKeyReset: make(chan struct{}),
Data: &types.ContextData{Approver: types.NewApprover()},
}
l, err := net.Listen("tcp", listen)
if err != nil {
@ -62,18 +52,17 @@ func NewServer(log logr.Logger, listen string) (*Server, error) {
for _, v := range serverv1.Routes {
s.mux.HandleFunc(v.Path, s.contextWrapper(v.Handler)).Methods(v.Methods...)
}
s.start()
return s, nil
}
func (s *Server) start() {
func (s *Server) Start(db *db.DB) {
s.srv = &http.Server{
Handler: s.mux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
s.Data.DB = db
go func() {
<-s.init
err := s.srv.Serve(s.listen)
if err != nil {
s.log.Error(err, "")
@ -89,10 +78,8 @@ func (s *Server) contextWrapper(h types.CtxHandler) http.HandlerFunc {
}
// Stop stops listening for incoming connections and closes currently open connections
func (s *Server) Stop() {
func (s *Server) Shutdown(ctx context.Context) {
s.log.Info("stopping")
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
err := s.srv.Shutdown(ctx)
if err != nil {
s.log.Error(err, "")