feat: init repo
This commit is contained in:
18
templates/project/providers/app/app.go.tpl
Normal file
18
templates/project/providers/app/app.go.tpl
Normal file
@@ -0,0 +1,18 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
"git.ipao.vip/rogeecn/atom/utils/opt"
|
||||
)
|
||||
|
||||
func Provide(opts ...opt.Option) error {
|
||||
o := opt.New(opts...)
|
||||
var config Config
|
||||
if err := o.UnmarshalConfig(&config); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return container.Container.Provide(func() (*Config, error) {
|
||||
return &config, nil
|
||||
}, o.DiOptions()...)
|
||||
}
|
||||
179
templates/project/providers/app/config.gen.go.tpl
Normal file
179
templates/project/providers/app/config.gen.go.tpl
Normal file
@@ -0,0 +1,179 @@
|
||||
// Code generated by go-enum DO NOT EDIT.
|
||||
// Version: -
|
||||
// Revision: -
|
||||
// Build Date: -
|
||||
// Built By: -
|
||||
|
||||
package app
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
// AppModeDevelopment is a AppMode of type development.
|
||||
AppModeDevelopment AppMode = "development"
|
||||
// AppModeRelease is a AppMode of type release.
|
||||
AppModeRelease AppMode = "release"
|
||||
// AppModeTest is a AppMode of type test.
|
||||
AppModeTest AppMode = "test"
|
||||
)
|
||||
|
||||
var ErrInvalidAppMode = fmt.Errorf("not a valid AppMode, try [%s]", strings.Join(_AppModeNames, ", "))
|
||||
|
||||
var _AppModeNames = []string{
|
||||
string(AppModeDevelopment),
|
||||
string(AppModeRelease),
|
||||
string(AppModeTest),
|
||||
}
|
||||
|
||||
// AppModeNames returns a list of possible string values of AppMode.
|
||||
func AppModeNames() []string {
|
||||
tmp := make([]string, len(_AppModeNames))
|
||||
copy(tmp, _AppModeNames)
|
||||
return tmp
|
||||
}
|
||||
|
||||
// AppModeValues returns a list of the values for AppMode
|
||||
func AppModeValues() []AppMode {
|
||||
return []AppMode{
|
||||
AppModeDevelopment,
|
||||
AppModeRelease,
|
||||
AppModeTest,
|
||||
}
|
||||
}
|
||||
|
||||
// String implements the Stringer interface.
|
||||
func (x AppMode) String() string {
|
||||
return string(x)
|
||||
}
|
||||
|
||||
// IsValid provides a quick way to determine if the typed value is
|
||||
// part of the allowed enumerated values
|
||||
func (x AppMode) IsValid() bool {
|
||||
_, err := ParseAppMode(string(x))
|
||||
return err == nil
|
||||
}
|
||||
|
||||
var _AppModeValue = map[string]AppMode{
|
||||
"development": AppModeDevelopment,
|
||||
"release": AppModeRelease,
|
||||
"test": AppModeTest,
|
||||
}
|
||||
|
||||
// ParseAppMode attempts to convert a string to a AppMode.
|
||||
func ParseAppMode(name string) (AppMode, error) {
|
||||
if x, ok := _AppModeValue[name]; ok {
|
||||
return x, nil
|
||||
}
|
||||
return AppMode(""), fmt.Errorf("%s is %w", name, ErrInvalidAppMode)
|
||||
}
|
||||
|
||||
var errAppModeNilPtr = errors.New("value pointer is nil") // one per type for package clashes
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (x *AppMode) Scan(value interface{}) (err error) {
|
||||
if value == nil {
|
||||
*x = AppMode("")
|
||||
return
|
||||
}
|
||||
|
||||
// A wider range of scannable types.
|
||||
// driver.Value values at the top of the list for expediency
|
||||
switch v := value.(type) {
|
||||
case string:
|
||||
*x, err = ParseAppMode(v)
|
||||
case []byte:
|
||||
*x, err = ParseAppMode(string(v))
|
||||
case AppMode:
|
||||
*x = v
|
||||
case *AppMode:
|
||||
if v == nil {
|
||||
return errAppModeNilPtr
|
||||
}
|
||||
*x = *v
|
||||
case *string:
|
||||
if v == nil {
|
||||
return errAppModeNilPtr
|
||||
}
|
||||
*x, err = ParseAppMode(*v)
|
||||
default:
|
||||
return errors.New("invalid type for AppMode")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (x AppMode) Value() (driver.Value, error) {
|
||||
return x.String(), nil
|
||||
}
|
||||
|
||||
// Set implements the Golang flag.Value interface func.
|
||||
func (x *AppMode) Set(val string) error {
|
||||
v, err := ParseAppMode(val)
|
||||
*x = v
|
||||
return err
|
||||
}
|
||||
|
||||
// Get implements the Golang flag.Getter interface func.
|
||||
func (x *AppMode) Get() interface{} {
|
||||
return *x
|
||||
}
|
||||
|
||||
// Type implements the github.com/spf13/pFlag Value interface.
|
||||
func (x *AppMode) Type() string {
|
||||
return "AppMode"
|
||||
}
|
||||
|
||||
type NullAppMode struct {
|
||||
AppMode AppMode
|
||||
Valid bool
|
||||
}
|
||||
|
||||
func NewNullAppMode(val interface{}) (x NullAppMode) {
|
||||
err := x.Scan(val) // yes, we ignore this error, it will just be an invalid value.
|
||||
_ = err // make any errcheck linters happy
|
||||
return
|
||||
}
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (x *NullAppMode) Scan(value interface{}) (err error) {
|
||||
if value == nil {
|
||||
x.AppMode, x.Valid = AppMode(""), false
|
||||
return
|
||||
}
|
||||
|
||||
err = x.AppMode.Scan(value)
|
||||
x.Valid = (err == nil)
|
||||
return
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (x NullAppMode) Value() (driver.Value, error) {
|
||||
if !x.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
// driver.Value accepts int64 for int values.
|
||||
return string(x.AppMode), nil
|
||||
}
|
||||
|
||||
type NullAppModeStr struct {
|
||||
NullAppMode
|
||||
}
|
||||
|
||||
func NewNullAppModeStr(val interface{}) (x NullAppModeStr) {
|
||||
x.Scan(val) // yes, we ignore this error, it will just be an invalid value.
|
||||
return
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (x NullAppModeStr) Value() (driver.Value, error) {
|
||||
if !x.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
return x.AppMode.String(), nil
|
||||
}
|
||||
45
templates/project/providers/app/config.go.tpl
Normal file
45
templates/project/providers/app/config.go.tpl
Normal file
@@ -0,0 +1,45 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
"git.ipao.vip/rogeecn/atom/utils/opt"
|
||||
)
|
||||
|
||||
const DefaultPrefix = "App"
|
||||
|
||||
func DefaultProvider() container.ProviderContainer {
|
||||
return container.ProviderContainer{
|
||||
Provider: Provide,
|
||||
Options: []opt.Option{
|
||||
opt.Prefix(DefaultPrefix),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// swagger:enum AppMode
|
||||
// ENUM(development, release, test)
|
||||
type AppMode string
|
||||
|
||||
type Config struct {
|
||||
Mode AppMode
|
||||
Cert *Cert
|
||||
BaseURI *string
|
||||
}
|
||||
|
||||
func (c *Config) IsDevMode() bool {
|
||||
return c.Mode == AppModeDevelopment
|
||||
}
|
||||
|
||||
func (c *Config) IsReleaseMode() bool {
|
||||
return c.Mode == AppModeRelease
|
||||
}
|
||||
|
||||
func (c *Config) IsTestMode() bool {
|
||||
return c.Mode == AppModeTest
|
||||
}
|
||||
|
||||
type Cert struct {
|
||||
CA string
|
||||
Cert string
|
||||
Key string
|
||||
}
|
||||
23
templates/project/providers/hashids/config.go.tpl
Normal file
23
templates/project/providers/hashids/config.go.tpl
Normal file
@@ -0,0 +1,23 @@
|
||||
package hashids
|
||||
|
||||
import (
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
"git.ipao.vip/rogeecn/atom/utils/opt"
|
||||
)
|
||||
|
||||
const DefaultPrefix = "HashIDs"
|
||||
|
||||
func DefaultProvider() container.ProviderContainer {
|
||||
return container.ProviderContainer{
|
||||
Provider: Provide,
|
||||
Options: []opt.Option{
|
||||
opt.Prefix(DefaultPrefix),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Alphabet string
|
||||
Salt string
|
||||
MinLength uint
|
||||
}
|
||||
35
templates/project/providers/hashids/hashids.go.tpl
Normal file
35
templates/project/providers/hashids/hashids.go.tpl
Normal file
@@ -0,0 +1,35 @@
|
||||
package hashids
|
||||
|
||||
import (
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
"git.ipao.vip/rogeecn/atom/utils/opt"
|
||||
|
||||
"github.com/speps/go-hashids/v2"
|
||||
)
|
||||
|
||||
func Provide(opts ...opt.Option) error {
|
||||
o := opt.New(opts...)
|
||||
var config Config
|
||||
if err := o.UnmarshalConfig(&config); err != nil {
|
||||
return err
|
||||
}
|
||||
return container.Container.Provide(func() (*hashids.HashID, error) {
|
||||
data := hashids.NewData()
|
||||
data.MinLength = int(config.MinLength)
|
||||
if data.MinLength == 0 {
|
||||
data.MinLength = 10
|
||||
}
|
||||
|
||||
data.Salt = config.Salt
|
||||
if data.Salt == "" {
|
||||
data.Salt = "default-salt-key"
|
||||
}
|
||||
|
||||
data.Alphabet = config.Alphabet
|
||||
if config.Alphabet == "" {
|
||||
data.Alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
}
|
||||
|
||||
return hashids.NewWithData(data)
|
||||
}, o.DiOptions()...)
|
||||
}
|
||||
38
templates/project/providers/http/config.go.tpl
Normal file
38
templates/project/providers/http/config.go.tpl
Normal file
@@ -0,0 +1,38 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const DefaultPrefix = "Http"
|
||||
|
||||
type Config struct {
|
||||
StaticPath *string
|
||||
StaticRoute *string
|
||||
BaseURI *string
|
||||
Port uint
|
||||
Tls *Tls
|
||||
Cors *Cors
|
||||
}
|
||||
|
||||
type Tls struct {
|
||||
Cert string
|
||||
Key string
|
||||
}
|
||||
|
||||
type 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(":%d", h.Port)
|
||||
}
|
||||
92
templates/project/providers/http/engine.go.tpl
Normal file
92
templates/project/providers/http/engine.go.tpl
Normal file
@@ -0,0 +1,92 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"runtime/debug"
|
||||
"time"
|
||||
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
"git.ipao.vip/rogeecn/atom/utils/opt"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/gofiber/fiber/v3/middleware/logger"
|
||||
"github.com/gofiber/fiber/v3/middleware/recover"
|
||||
)
|
||||
|
||||
func DefaultProvider() container.ProviderContainer {
|
||||
return container.ProviderContainer{
|
||||
Provider: Provide,
|
||||
Options: []opt.Option{
|
||||
opt.Prefix(DefaultPrefix),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
conf *Config
|
||||
Engine *fiber.App
|
||||
}
|
||||
|
||||
func (svc *Service) Serve() error {
|
||||
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,
|
||||
}
|
||||
|
||||
if svc.conf.Tls != nil {
|
||||
if svc.conf.Tls.Cert == "" || svc.conf.Tls.Key == "" {
|
||||
return errors.New("tls cert or key is empty")
|
||||
}
|
||||
listenConfig.CertFile = svc.conf.Tls.Cert
|
||||
listenConfig.CertKeyFile = svc.conf.Tls.Key
|
||||
}
|
||||
container.AddCloseAble(func() {
|
||||
svc.Engine.Shutdown()
|
||||
})
|
||||
|
||||
return svc.Engine.Listen(svc.conf.Address(), listenConfig)
|
||||
}
|
||||
|
||||
func Provide(opts ...opt.Option) error {
|
||||
o := opt.New(opts...)
|
||||
var config Config
|
||||
if err := o.UnmarshalConfig(&config); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return container.Container.Provide(func() (*Service, error) {
|
||||
engine := fiber.New(fiber.Config{
|
||||
StrictRouting: true,
|
||||
})
|
||||
engine.Use(recover.New(recover.Config{
|
||||
EnableStackTrace: true,
|
||||
StackTraceHandler: func(c fiber.Ctx, e any) {
|
||||
log.WithError(e.(error)).Error(fmt.Sprintf("panic: %v\n%s\n", e, debug.Stack()))
|
||||
},
|
||||
}))
|
||||
|
||||
if config.StaticRoute != nil && config.StaticPath != nil {
|
||||
engine.Use(config.StaticRoute, config.StaticPath)
|
||||
}
|
||||
|
||||
engine.Use(logger.New(logger.Config{
|
||||
Format: `[${ip}:${port}] - [${time}] - ${method} - ${status} - ${path} ${latency} "${ua}"` + "\n",
|
||||
TimeFormat: time.RFC1123,
|
||||
TimeZone: "Asia/Shanghai",
|
||||
}))
|
||||
|
||||
return &Service{
|
||||
Engine: engine,
|
||||
conf: &config,
|
||||
}, nil
|
||||
}, o.DiOptions()...)
|
||||
}
|
||||
35
templates/project/providers/jwt/config.go.tpl
Normal file
35
templates/project/providers/jwt/config.go.tpl
Normal file
@@ -0,0 +1,35 @@
|
||||
package jwt
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
"git.ipao.vip/rogeecn/atom/utils/opt"
|
||||
)
|
||||
|
||||
const DefaultPrefix = "JWT"
|
||||
|
||||
func DefaultProvider() container.ProviderContainer {
|
||||
return container.ProviderContainer{
|
||||
Provider: Provide,
|
||||
Options: []opt.Option{
|
||||
opt.Prefix(DefaultPrefix),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
SigningKey string // jwt签名
|
||||
ExpiresTime string // 过期时间
|
||||
Issuer string // 签发者
|
||||
}
|
||||
|
||||
func (c *Config) ExpiresTimeDuration() time.Duration {
|
||||
d, err := time.ParseDuration(c.ExpiresTime)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return d
|
||||
}
|
||||
118
templates/project/providers/jwt/jwt.go.tpl
Normal file
118
templates/project/providers/jwt/jwt.go.tpl
Normal file
@@ -0,0 +1,118 @@
|
||||
package jwt
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
"git.ipao.vip/rogeecn/atom/utils/opt"
|
||||
|
||||
jwt "github.com/golang-jwt/jwt/v4"
|
||||
"golang.org/x/sync/singleflight"
|
||||
)
|
||||
|
||||
const (
|
||||
CtxKey = "claims"
|
||||
HttpHeader = "Authorization"
|
||||
)
|
||||
|
||||
type BaseClaims struct {
|
||||
OpenID string `json:"open_id,omitempty"`
|
||||
Tenant string `json:"tenant,omitempty"`
|
||||
UserID int64 `json:"user_id,omitempty"`
|
||||
TenantID int64 `json:"tenant_id,omitempty"`
|
||||
}
|
||||
|
||||
// Custom claims structure
|
||||
type Claims struct {
|
||||
BaseClaims
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
const TokenPrefix = "Bearer "
|
||||
|
||||
type JWT struct {
|
||||
singleflight *singleflight.Group
|
||||
config *Config
|
||||
SigningKey []byte
|
||||
}
|
||||
|
||||
var (
|
||||
TokenExpired = errors.New("Token is expired")
|
||||
TokenNotValidYet = errors.New("Token not active yet")
|
||||
TokenMalformed = errors.New("That's not even a token")
|
||||
TokenInvalid = errors.New("Couldn't handle this token:")
|
||||
)
|
||||
|
||||
func Provide(opts ...opt.Option) error {
|
||||
o := opt.New(opts...)
|
||||
var config Config
|
||||
if err := o.UnmarshalConfig(&config); err != nil {
|
||||
return err
|
||||
}
|
||||
return container.Container.Provide(func() (*JWT, error) {
|
||||
return &JWT{
|
||||
singleflight: &singleflight.Group{},
|
||||
config: &config,
|
||||
SigningKey: []byte(config.SigningKey),
|
||||
}, nil
|
||||
}, o.DiOptions()...)
|
||||
}
|
||||
|
||||
func (j *JWT) CreateClaims(baseClaims BaseClaims) *Claims {
|
||||
ep, _ := time.ParseDuration(j.config.ExpiresTime)
|
||||
claims := Claims{
|
||||
BaseClaims: baseClaims,
|
||||
RegisteredClaims: jwt.RegisteredClaims{
|
||||
NotBefore: jwt.NewNumericDate(time.Now().Add(-time.Second * 10)), // 签名生效时间
|
||||
ExpiresAt: jwt.NewNumericDate(time.Now().Add(ep)), // 过期时间 7天 配置文件
|
||||
Issuer: j.config.Issuer, // 签名的发行者
|
||||
},
|
||||
}
|
||||
return &claims
|
||||
}
|
||||
|
||||
// 创建一个token
|
||||
func (j *JWT) CreateToken(claims *Claims) (string, error) {
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
return token.SignedString(j.SigningKey)
|
||||
}
|
||||
|
||||
// CreateTokenByOldToken 旧token 换新token 使用归并回源避免并发问题
|
||||
func (j *JWT) CreateTokenByOldToken(oldToken string, claims *Claims) (string, error) {
|
||||
v, err, _ := j.singleflight.Do("JWT:"+oldToken, func() (interface{}, error) {
|
||||
return j.CreateToken(claims)
|
||||
})
|
||||
return v.(string), err
|
||||
}
|
||||
|
||||
// 解析 token
|
||||
func (j *JWT) Parse(tokenString string) (*Claims, error) {
|
||||
tokenString = strings.TrimPrefix(tokenString, TokenPrefix)
|
||||
token, err := jwt.ParseWithClaims(tokenString, &Claims{}, func(token *jwt.Token) (i interface{}, e error) {
|
||||
return j.SigningKey, nil
|
||||
})
|
||||
if err != nil {
|
||||
if ve, ok := err.(*jwt.ValidationError); ok {
|
||||
if ve.Errors&jwt.ValidationErrorMalformed != 0 {
|
||||
return nil, TokenMalformed
|
||||
} else if ve.Errors&jwt.ValidationErrorExpired != 0 {
|
||||
// Token is expired
|
||||
return nil, TokenExpired
|
||||
} else if ve.Errors&jwt.ValidationErrorNotValidYet != 0 {
|
||||
return nil, TokenNotValidYet
|
||||
} else {
|
||||
return nil, TokenInvalid
|
||||
}
|
||||
}
|
||||
}
|
||||
if token != nil {
|
||||
if claims, ok := token.Claims.(*Claims); ok && token.Valid {
|
||||
return claims, nil
|
||||
}
|
||||
return nil, TokenInvalid
|
||||
} else {
|
||||
return nil, TokenInvalid
|
||||
}
|
||||
}
|
||||
79
templates/project/providers/postgres/config.go.tpl
Executable file
79
templates/project/providers/postgres/config.go.tpl
Executable file
@@ -0,0 +1,79 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
"git.ipao.vip/rogeecn/atom/utils/opt"
|
||||
)
|
||||
|
||||
const DefaultPrefix = "Database"
|
||||
|
||||
func DefaultProvider() container.ProviderContainer {
|
||||
return container.ProviderContainer{
|
||||
Provider: Provide,
|
||||
Options: []opt.Option{
|
||||
opt.Prefix(DefaultPrefix),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Username string
|
||||
Password string
|
||||
Database string
|
||||
Schema string
|
||||
Host string
|
||||
Port uint
|
||||
SslMode string
|
||||
TimeZone string
|
||||
Prefix string // 表前缀
|
||||
Singular bool // 是否开启全局禁用复数,true表示开启
|
||||
MaxIdleConns int // 空闲中的最大连接数
|
||||
MaxOpenConns int // 打开到数据库的最大连接数
|
||||
}
|
||||
|
||||
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"
|
||||
}
|
||||
|
||||
if m.TimeZone == "" {
|
||||
m.TimeZone = "Asia/Shanghai"
|
||||
}
|
||||
|
||||
if m.Port == 0 {
|
||||
m.Port = 5432
|
||||
}
|
||||
|
||||
if m.Schema == "" {
|
||||
m.Schema = "public"
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// DSN connection dsn
|
||||
func (m *Config) DSN() string {
|
||||
dsnTpl := "host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s"
|
||||
m.checkDefault()
|
||||
|
||||
return fmt.Sprintf(dsnTpl, m.Host, m.Username, m.Password, m.Database, m.Port, m.SslMode, m.TimeZone)
|
||||
}
|
||||
34
templates/project/providers/postgres/postgres.go.tpl
Normal file
34
templates/project/providers/postgres/postgres.go.tpl
Normal file
@@ -0,0 +1,34 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
"git.ipao.vip/rogeecn/atom/utils/opt"
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func Provide(opts ...opt.Option) error {
|
||||
o := opt.New(opts...)
|
||||
var conf Config
|
||||
if err := o.UnmarshalConfig(&conf); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return container.Container.Provide(func() (*sql.DB, *Config, error) {
|
||||
log.Debugf("connect postgres with dsn: '%s'", conf.DSN())
|
||||
db, err := sql.Open("postgres", conf.DSN())
|
||||
if err != nil {
|
||||
return nil, nil, errors.Wrap(err, "connect database")
|
||||
}
|
||||
|
||||
if err := db.Ping(); err != nil {
|
||||
db.Close()
|
||||
return nil, nil, errors.Wrap(err, "ping database")
|
||||
}
|
||||
|
||||
return db, &conf, err
|
||||
}, o.DiOptions()...)
|
||||
}
|
||||
41
templates/project/providers/uuid/uuid.go.tpl
Normal file
41
templates/project/providers/uuid/uuid.go.tpl
Normal file
@@ -0,0 +1,41 @@
|
||||
package uuid
|
||||
|
||||
import (
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
"git.ipao.vip/rogeecn/atom/utils/opt"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
)
|
||||
|
||||
func DefaultProvider() container.ProviderContainer {
|
||||
return container.ProviderContainer{
|
||||
Provider: Provide,
|
||||
Options: []opt.Option{},
|
||||
}
|
||||
}
|
||||
|
||||
type Generator struct {
|
||||
generator uuid.Generator
|
||||
}
|
||||
|
||||
func Provide(opts ...opt.Option) error {
|
||||
o := opt.New(opts...)
|
||||
return container.Container.Provide(func() (*Generator, error) {
|
||||
return &Generator{
|
||||
generator: uuid.DefaultGenerator,
|
||||
}, nil
|
||||
}, o.DiOptions()...)
|
||||
}
|
||||
|
||||
func (u *Generator) MustGenerate() string {
|
||||
uuid, _ := u.Generate()
|
||||
return uuid
|
||||
}
|
||||
|
||||
func (u *Generator) Generate() (string, error) {
|
||||
uuid, err := u.generator.NewV4()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return uuid.String(), err
|
||||
}
|
||||
Reference in New Issue
Block a user