restructure

This commit is contained in:
yanghao05
2023-04-20 12:11:34 +08:00
parent 6757e00d73
commit 5b8eca5d87
120 changed files with 546 additions and 7303 deletions

34
providers/http/config.go Normal file
View File

@@ -0,0 +1,34 @@
package http
import (
"fmt"
)
type Config struct {
Static string
Host string
Port uint
Https bool
HttpsCert string
HttpKey string
Cors struct {
Mode string
Whitelist []Whitelist
}
}
type Whitelist struct {
AllowOrigin string
AllowHeaders string
AllowMethods string
ExposeHeaders string
AllowCredentials bool
}
func (h *Config) Address() string {
return fmt.Sprintf("%s:%d", h.Host, h.Port)
}
func (h *Config) PortString() string {
return fmt.Sprintf(":%d", h.Port)
}

View File

@@ -0,0 +1,9 @@
package http
type Route interface {
Register()
}
type Service interface {
Serve() error
}

View File

@@ -1,61 +0,0 @@
package http
import (
"atom/container"
"atom/providers/config"
"atom/providers/log"
"fmt"
"time"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
func init() {
if err := container.Container.Provide(NewService); err != nil {
log.Fatal(err)
}
}
type Service struct {
Engine *gin.Engine
conf *config.Config
}
func (e *Service) Serve() error {
if e.conf.Http.Https {
return e.Engine.RunTLS(e.conf.Http.PortString(), e.conf.Http.HttpsCert, e.conf.Http.HttpKey)
}
return e.Engine.Run(e.conf.Http.PortString())
}
func NewService(cfg *config.Config, logger *log.Logger) *Service {
gin.DefaultWriter = logger.LevelWriter(zap.InfoLevel)
gin.DefaultErrorWriter = logger.LevelWriter(zap.ErrorLevel)
if cfg.App.IsDebug() {
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
}
engine := gin.New()
engine.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
return fmt.Sprintf(`%s - [%s] "%s %s %s %d %s '%q' %s"\n`,
param.ClientIP,
param.TimeStamp.Format(time.RFC1123),
param.Method,
param.Path,
param.Request.Proto,
param.StatusCode,
param.Latency,
param.Request.UserAgent(),
param.ErrorMessage,
)
}))
engine.Use(gin.Recovery())
return &Service{Engine: engine, conf: cfg}
}

View File

@@ -0,0 +1,54 @@
package gin
import (
"fmt"
"time"
"github.com/rogeecn/atom/container"
"github.com/rogeecn/atom/providers/http"
"github.com/rogeecn/atom/providers/log"
"go.uber.org/dig"
"github.com/gin-gonic/gin"
)
type Service struct {
conf *http.Config
Engine *gin.Engine
}
func (e *Service) Use(middleware ...gin.HandlerFunc) gin.IRoutes {
return e.Engine.Use(middleware...)
}
func (e *Service) Serve() error {
if e.conf.Https {
return e.Engine.RunTLS(e.conf.PortString(), e.conf.HttpsCert, e.conf.HttpKey)
}
return e.Engine.Run(e.conf.PortString())
}
func Provide(cfg *http.Config, opts ...dig.ProvideOption) error {
return container.Container.Provide(func() (http.Service, error) {
gin.DefaultWriter = log.LevelWriter{Level: log.InfoLevel}
gin.DefaultErrorWriter = log.LevelWriter{Level: log.ErrorLevel}
engine := gin.New()
engine.Use(gin.Recovery())
engine.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
return fmt.Sprintf(`%s - [%s] "%s %s %s %d %s '%q' %s"\n`,
param.ClientIP,
param.TimeStamp.Format(time.RFC1123),
param.Method,
param.Path,
param.Request.Proto,
param.StatusCode,
param.Latency,
param.Request.UserAgent(),
param.ErrorMessage,
)
}))
return &Service{Engine: engine, conf: cfg}, nil
}, opts...)
}