| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- package api
- import (
- "context"
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure/logger"
- "git.sxidc.com/go-tools/utils/strutils"
- "github.com/gin-gonic/gin"
- "github.com/pkg/errors"
- "net/http"
- "runtime/debug"
- "slices"
- "strconv"
- "time"
- )
- type Api struct {
- options Options
- server *http.Server
- rootRouter *RootRouter
- prefixRouter *PrefixRouter
- }
- func New(opts ...Option) *Api {
- options := new(Options)
- for _, opt := range opts {
- opt(options)
- }
- if strutils.IsStringEmpty(options.port) {
- options.port = "8080"
- }
- engine := gin.New()
- server := &http.Server{
- Addr: ":" + options.port,
- Handler: engine,
- }
- engine.Use(
- // 日志中间件
- func(c *gin.Context) {
- start := time.Now()
- c.Next()
- path := c.FullPath()
- if slices.Contains(options.skipPaths, path) {
- return
- }
- end := time.Now()
- logger.GetInstance().Info(" %d | %s | %s \"%s\"",
- c.Writer.Status(),
- strconv.Itoa(int(end.Sub(start).Milliseconds()))+"ms",
- c.Request.Method,
- path,
- )
- },
- // recover
- func(c *gin.Context) {
- err := recover()
- if err != nil {
- logger.GetInstance().Error("%s", debug.Stack())
- }
- })
- api := &Api{
- options: *options,
- server: server,
- rootRouter: newRootRouter(engine),
- }
- if strutils.IsStringNotEmpty(options.urlPrefix) {
- api.prefixRouter = newPrefixRouter(engine.Group(options.urlPrefix))
- }
- return api
- }
- func NewWithEngine(server *http.Server, engine *gin.Engine, opts ...Option) *Api {
- options := new(Options)
- for _, opt := range opts {
- opt(options)
- }
- if strutils.IsStringEmpty(options.port) {
- options.port = "8080"
- }
- api := &Api{
- options: *options,
- server: server,
- rootRouter: newRootRouter(engine),
- }
- if strutils.IsStringNotEmpty(options.urlPrefix) {
- api.prefixRouter = newPrefixRouter(engine.Group(options.urlPrefix))
- }
- return api
- }
- func (api *Api) Start() error {
- err := api.server.ListenAndServe()
- if err != nil && !errors.Is(err, http.ErrServerClosed) {
- return errors.New(err.Error())
- }
- return nil
- }
- func (api *Api) Finish() error {
- err := api.server.Shutdown(context.Background())
- if err != nil {
- return errors.New(err.Error())
- }
- return nil
- }
- func (api *Api) Options() Options {
- return api.options
- }
- func (api *Api) RootRouter() Router {
- return api.rootRouter
- }
- func (api *Api) PrefixRouter() Router {
- if api.prefixRouter == nil {
- return api.rootRouter
- }
- return api.prefixRouter
- }
- const (
- RouterRoot = "root"
- RouterPrefix = "prefix"
- )
- func (api *Api) ChooseRouter(routerType string, version string) Router {
- var router Router
- switch routerType {
- case RouterRoot:
- router = api.RootRouter()
- case RouterPrefix:
- router = api.PrefixRouter()
- default:
- router = api.PrefixRouter()
- }
- if strutils.IsStringNotEmpty(version) {
- router = router.VersionedRouter(version)
- }
- return router
- }
|