/* * Copyright 2026 Oleg Borodin */ package service import ( "context" "crypto/tls" "fmt" "net" "net/http" "time" "mproxy/app/handler" "mproxy/app/logger" "mproxy/app/router" ) type ServiceParams struct { Handler *handler.Handler Address string Portnum uint32 X509cert string X509key string } type Service struct { hand *handler.Handler rout *router.Router logg *logger.Logger hsrv *http.Server listen net.Listener address string portnum uint32 x509cert string x509key string protocol string } func NewService(params *ServiceParams) (*Service, error) { var err error svc := &Service{ hand: params.Handler, address: params.Address, portnum: params.Portnum, x509cert: params.X509cert, x509key: params.X509key, protocol: "tcp", } 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.Post(`/v3/api/service/hello`, svc.hand.GetHello) svc.rout.Connect(``, svc.hand.ConnectTo) svc.rout.Delete(`{uri:.*}`, svc.hand.PlainCall) svc.rout.Get(`{uri:.*}`, svc.hand.PlainCall) svc.rout.Head(`{uri:.*}`, svc.hand.PlainCall) svc.rout.Patch(`{uri:.*}`, svc.hand.PlainCall) svc.rout.Post(`{uri:.*}`, svc.hand.PlainCall) 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) } const useTLS = true if useTLS { tlsCert, err := tls.X509KeyPair([]byte(svc.x509cert), []byte(svc.x509key)) if err != nil { return err } tlsConfig := tls.Config{ Certificates: []tls.Certificate{tlsCert}, ClientAuth: tls.NoClientCert, InsecureSkipVerify: true, } listenAddress := fmt.Sprintf("%s:%d", svc.address, svc.portnum) svc.listen, err = tls.Listen(svc.protocol, listenAddress, &tlsConfig) if err != nil { return err } } else { listenAddress := fmt.Sprintf("%s:%d", svc.address, svc.portnum) svc.listen, err = net.Listen(svc.protocol, listenAddress) if err != nil { return err } } 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) } } }