feat: update framework
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package fiber
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"backend/providers/http"
|
||||
@@ -9,9 +10,9 @@ import (
|
||||
"git.ipao.vip/rogeecn/atom/utils/opt"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/logger"
|
||||
"github.com/gofiber/fiber/v2/middleware/recover"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/gofiber/fiber/v3/middleware/logger"
|
||||
"github.com/gofiber/fiber/v3/middleware/recover"
|
||||
)
|
||||
|
||||
func DefaultProvider() container.ProviderContainer {
|
||||
@@ -37,14 +38,32 @@ func (e *Service) GetEngine() interface{} {
|
||||
}
|
||||
|
||||
func (e *Service) Close() {
|
||||
e.Engine.Shutdown()
|
||||
if err := e.Engine.Shutdown(); err != nil {
|
||||
log.Error("http server shutdown error: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Service) Serve() error {
|
||||
if e.conf.Tls != nil {
|
||||
return e.Engine.ListenTLS(e.conf.PortString(), e.conf.Tls.Cert, e.conf.Tls.Key)
|
||||
listenConfig := fiber.ListenConfig{
|
||||
EnablePrintRoutes: true,
|
||||
OnShutdownSuccess: func() {
|
||||
log.Info("http server shutdown success")
|
||||
},
|
||||
OnShutdownError: func(err error) {
|
||||
log.Error("http server shutdown error: ", err)
|
||||
},
|
||||
DisableStartupMessage: true,
|
||||
}
|
||||
return e.Engine.Listen(e.conf.PortString())
|
||||
|
||||
if e.conf.Tls != nil {
|
||||
if e.conf.Tls.Cert == "" || e.conf.Tls.Key == "" {
|
||||
return errors.New("tls cert or key is empty")
|
||||
}
|
||||
listenConfig.CertFile = e.conf.Tls.Cert
|
||||
listenConfig.CertKeyFile = e.conf.Tls.Key
|
||||
}
|
||||
|
||||
return e.Engine.Listen(e.conf.PortString(), listenConfig)
|
||||
}
|
||||
|
||||
func Provide(opts ...opt.Option) error {
|
||||
@@ -56,8 +75,7 @@ func Provide(opts ...opt.Option) error {
|
||||
|
||||
return container.Container.Provide(func(l *log.Logger) (http.Service, error) {
|
||||
engine := fiber.New(fiber.Config{
|
||||
EnablePrintRoutes: true,
|
||||
StrictRouting: true,
|
||||
StrictRouting: true,
|
||||
})
|
||||
engine.Use(recover.New())
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ type Claims struct {
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
const TOKEN_PREFIX = "Bearer "
|
||||
const TokenPrefix = "Bearer "
|
||||
|
||||
type JWT struct {
|
||||
singleflight *singleflight.Group
|
||||
@@ -86,7 +86,7 @@ func (j *JWT) CreateTokenByOldToken(oldToken string, claims *Claims) (string, er
|
||||
|
||||
// 解析 token
|
||||
func (j *JWT) ParseToken(tokenString string) (*Claims, error) {
|
||||
tokenString = strings.TrimPrefix(tokenString, TOKEN_PREFIX)
|
||||
tokenString = strings.TrimPrefix(tokenString, TokenPrefix)
|
||||
token, err := jwt.ParseWithClaims(tokenString, &Claims{}, func(token *jwt.Token) (i interface{}, e error) {
|
||||
return j.SigningKey, nil
|
||||
})
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package database
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -32,8 +32,19 @@ type Config struct {
|
||||
MaxOpenConns int // 打开到数据库的最大连接数
|
||||
}
|
||||
|
||||
func (m *Config) EmptyDsn() string {
|
||||
dsnTpl := "host=%s user=%s password=%s port=%d dbname=%s sslmode=%s TimeZone=%s"
|
||||
func (m *Config) checkDefault() {
|
||||
if m.MaxIdleConns == 0 {
|
||||
m.MaxIdleConns = 10
|
||||
}
|
||||
|
||||
if m.MaxOpenConns == 0 {
|
||||
m.MaxOpenConns = 100
|
||||
}
|
||||
|
||||
if m.Username == "" {
|
||||
m.Username = "postgres"
|
||||
}
|
||||
|
||||
if m.SslMode == "" {
|
||||
m.SslMode = "disable"
|
||||
}
|
||||
@@ -42,6 +53,15 @@ func (m *Config) EmptyDsn() string {
|
||||
m.TimeZone = "Asia/Shanghai"
|
||||
}
|
||||
|
||||
if m.Port == 0 {
|
||||
m.Port = 5432
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Config) EmptyDsn() string {
|
||||
dsnTpl := "host=%s user=%s password=%s port=%d dbname=%s sslmode=%s TimeZone=%s"
|
||||
m.checkDefault()
|
||||
|
||||
return fmt.Sprintf(dsnTpl, m.Host, m.Username, m.Password, m.Port, m.Database, m.SslMode, m.TimeZone)
|
||||
}
|
||||
|
||||
@@ -49,12 +69,5 @@ func (m *Config) EmptyDsn() string {
|
||||
func (m *Config) DSN() string {
|
||||
dsnTpl := "host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s"
|
||||
|
||||
if m.SslMode == "" {
|
||||
m.SslMode = "disable"
|
||||
}
|
||||
|
||||
if m.TimeZone == "" {
|
||||
m.TimeZone = "Asia/Shanghai"
|
||||
}
|
||||
return fmt.Sprintf(dsnTpl, m.Host, m.Username, m.Password, m.Database, m.Port, m.SslMode, m.TimeZone)
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package database
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
Reference in New Issue
Block a user