/* * Copyright 2026 Oleg Borodin */ package service import ( "context" "net" "net/http" "time" "mbase/app/handler" "mbase/app/logger" "mbase/app/router" ) type ServiceParams struct { Handler *handler.Handler Listener net.Listener } type Service struct { hand *handler.Handler rout *router.Router logg *logger.Logger listen net.Listener hsrv *http.Server } func NewService(params *ServiceParams) (*Service, error) { var err error svc := &Service{ hand: params.Handler, listen: params.Listener, } svc.logg = logger.NewLoggerWithSubject("service") return svc, err } func (svc *Service) Build() error { var err error svc.logg.Infof("Service build ") svc.rout = router.NewRouter() svc.rout.Use(router.NewRecoveryMiddleware(svc.logg.Errorf)) svc.rout.Use(router.NewLoggingMiddleware(svc.logg.Infof)) svc.rout.Use(router.NewCorsMiddleware()) svc.rout.Use(svc.hand.AuthMiddleware) svc.rout.Get(`/v3/api/service/hello`, svc.hand.SendHello) svc.rout.Post(`/v3/api/account/create`, svc.hand.CreateAccount) svc.rout.Post(`/v3/api/account/get`, svc.hand.GetAccount) svc.rout.Post(`/v3/api/account/update`, svc.hand.UpdateAccount) svc.rout.Post(`/v3/api/account/delete`, svc.hand.DeleteAccount) svc.rout.Post(`/v3/api/accounts/list`, svc.hand.ListAccounts) svc.rout.Post(`/v3/api/grant/create`, svc.hand.CreateGrant) svc.rout.Post(`/v3/api/grant/get`, svc.hand.GetGrant) svc.rout.Post(`/v3/api/grant/update`, svc.hand.UpdateGrant) svc.rout.Post(`/v3/api/grant/delete`, svc.hand.DeleteGrant) svc.rout.Post(`/v3/api/grants/list`, svc.hand.ListGrants) svc.rout.NotFound(svc.hand.NotFound) selector := svc.rout.Selector() for _, item := range selector.Routes { svc.logg.Infof("%s\t%s", item.Method, item.RawPath) } svc.logg.Infof("Service listening at %v", svc.listen.Addr()) svc.hsrv = &http.Server{ Handler: svc.rout, } return err } func (svc *Service) Run() error { var err error svc.logg.Infof("Service run") err = svc.hsrv.Serve(svc.listen) if err == http.ErrServerClosed { svc.logg.Warningf("Service Closed") err = nil } if err != nil { return err } return err } func (svc *Service) Stop() { if svc.hsrv != nil { svc.logg.Infof("Service stop") downWaiting := 10 * time.Second ctx, cancel := context.WithTimeout(context.Background(), downWaiting) defer cancel() err := svc.hsrv.Shutdown(ctx) if err != nil { svc.logg.Errorf("Error service shutdown: %v", err) } } }