import sources
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"certmanager/internal/wrpc/handler"
|
||||
"certmanager/pkg/auxgin"
|
||||
"certmanager/pkg/logger"
|
||||
|
||||
"certmanager/pkg/auxhttp"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const (
|
||||
httpTimeout = 360
|
||||
)
|
||||
|
||||
type ServiceConfig struct {
|
||||
Handler *handler.Handler
|
||||
PortNum int
|
||||
Hostname string
|
||||
X509Cert []byte
|
||||
X509Key []byte
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
hand *handler.Handler
|
||||
hsrv *http.Server
|
||||
log *logger.Logger
|
||||
engine *gin.Engine
|
||||
|
||||
portnum int
|
||||
hostname string
|
||||
x509Cert []byte
|
||||
x509Key []byte
|
||||
}
|
||||
|
||||
func NewService(conf *ServiceConfig) (*Service, error) {
|
||||
var err error
|
||||
svc := &Service{
|
||||
hand: conf.Handler,
|
||||
portnum: conf.PortNum,
|
||||
hostname: conf.Hostname,
|
||||
x509Cert: conf.X509Cert,
|
||||
x509Key: conf.X509Key,
|
||||
}
|
||||
svc.log = logger.NewLogger("wservice")
|
||||
return svc, err
|
||||
}
|
||||
|
||||
func (svc *Service) Build() error {
|
||||
var err error
|
||||
svc.log.Debugf("Build service")
|
||||
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
gin.DisableConsoleColor()
|
||||
|
||||
svc.engine = gin.New()
|
||||
svc.engine.Use(gin.Recovery())
|
||||
svc.engine.Use(auxgin.CorsMiddleware())
|
||||
svc.engine.Use(auxgin.LogMiddleware())
|
||||
svc.engine.Use(auxgin.RequestLogMiddleware())
|
||||
|
||||
apiGroup := svc.engine.Group("api")
|
||||
v1Group := apiGroup.Group("v1")
|
||||
{
|
||||
statusGroup := v1Group.Group("status")
|
||||
statusGroup.POST("get", svc.hand.GetStatus)
|
||||
/*
|
||||
forwardingGroup := v1Group.Group("forwarding")
|
||||
forwardingGroup.POST("create", svc.hand.CreateForwarding)
|
||||
forwardingGroup.POST("list", svc.hand.ListForwardings)
|
||||
forwardingGroup.POST("delete", svc.hand.DeleteForwarding)
|
||||
|
||||
defaultsGroup := v1Group.Group("defaults")
|
||||
defaultsGroup.POST("set", svc.hand.SetDefaults)
|
||||
defaultsGroup.POST("get", svc.hand.GetDefaults)
|
||||
|
||||
proxyGroup := v1Group.Group("proxy")
|
||||
proxyGroup.POST("reset", svc.hand.ResetProxy)
|
||||
*/
|
||||
}
|
||||
noRouteFunc := func(gctx *gin.Context) {
|
||||
err := fmt.Errorf("No route")
|
||||
auxhttp.SendError(gctx, err)
|
||||
}
|
||||
svc.engine.NoRoute(noRouteFunc)
|
||||
|
||||
tlsCert, err := tls.X509KeyPair(svc.x509Cert, svc.x509Key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tlsConfig := tls.Config{
|
||||
Certificates: []tls.Certificate{tlsCert},
|
||||
ClientAuth: tls.NoClientCert,
|
||||
InsecureSkipVerify: true,
|
||||
}
|
||||
listenAddr := fmt.Sprintf(":%d", svc.portnum)
|
||||
svc.hsrv = &http.Server{
|
||||
Addr: listenAddr,
|
||||
Handler: svc.engine,
|
||||
TLSConfig: &tlsConfig,
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (svc *Service) Run() error {
|
||||
var err error
|
||||
for _, route := range svc.engine.Routes() {
|
||||
svc.log.Debugf("The route is registered: %s %s", route.Method, route.Path)
|
||||
}
|
||||
svc.log.Infof("Service listening at %d port", svc.portnum)
|
||||
err = svc.hsrv.ListenAndServeTLS("", "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (svc *Service) Stop() {
|
||||
svc.log.Infof("Stopping service")
|
||||
if svc.hsrv != nil {
|
||||
downWaiting := 5 * time.Second
|
||||
ctx, _ := context.WithTimeout(context.Background(), downWaiting)
|
||||
svc.hsrv.Shutdown(ctx)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user