server.go 741 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package http
  2. import (
  3. "kpt-tmr-group/dep"
  4. "kpt-tmr-group/http/middleware"
  5. "kpt-tmr-group/http/route"
  6. "github.com/gin-gonic/gin"
  7. )
  8. type Server struct {
  9. *gin.Engine
  10. }
  11. type Option func(*Server)
  12. func NewServer(options ...Option) *Server {
  13. s := &Server{
  14. Engine: gin.New(),
  15. }
  16. for _, option := range options {
  17. option(s)
  18. }
  19. return s
  20. }
  21. func ExportLogOption() Option {
  22. return func(s *Server) {
  23. s.Engine.Use(gin.LoggerWithConfig(gin.LoggerConfig{SkipPaths: []string{"/metrics", "/check"}}))
  24. }
  25. }
  26. func SetRouteOption() Option {
  27. return func(s *Server) {
  28. route.HTTPServerRoute()(s.Engine)
  29. }
  30. }
  31. func WithDependency(dep *dep.HttpDependency) Option {
  32. return func(s *Server) {
  33. s.Engine.Use(middleware.WithDependency(dep))
  34. }
  35. }