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

View File

@@ -1,56 +0,0 @@
package config
import (
"atom/container"
"atom/utils"
"atom/utils/fs"
"log"
"github.com/pkg/errors"
"github.com/rogeecn/fabfile"
"github.com/spf13/viper"
)
type Config struct {
App App
Captcha Captcha
Http Http
Log Log
Database Database
Storage Storage
}
func init() {
if err := container.Container.Provide(Load); err != nil {
log.Fatal(err)
}
}
func Load() (*Config, error) {
var err error
confFile := utils.ShareConfigFile
if confFile == "" {
confFile, err = fabfile.Find("config.toml")
if err != nil {
return nil, err
}
}
path, name, _ := fs.FilePathInfo(confFile)
viper.SetConfigName(name) // name of config file (without extension)
viper.SetConfigType("toml") // REQUIRED if the config file does not have the extension in the name
viper.AddConfigPath("$HOME/") // call multiple times to add many search paths
viper.AddConfigPath(path) // optionally look for config in the working directory
viper.AddConfigPath(".") // optionally look for config in the working directory
// Find and read the config file
if err := viper.ReadInConfig(); err != nil { // Handle errors reading the config file
return nil, errors.Wrapf(err, "read config failed, %s", confFile)
}
config := &Config{}
if err := viper.Unmarshal(&config); err != nil {
return nil, errors.Wrapf(err, "unmarshal data failed, %s", confFile)
}
return config, nil
}

View File

@@ -1,18 +0,0 @@
package config
import "atom/utils"
type App struct {
Mode string
}
func (a App) IsDebug() bool {
return a.Mode == "debug"
}
func (a App) IsRelease() bool {
return a.Mode == "release"
}
func (a App) IsTesting() bool {
return a.Mode == "testing" || utils.IsInTesting()
}

View File

@@ -1,22 +0,0 @@
package config
import (
"log"
"time"
)
type Captcha struct {
KeyLong uint // 验证码长度
ImgWidth uint // 验证码宽度
ImgHeight uint // 验证码高度
OpenCaptcha uint // 防爆破验证码开启此数0代表每次登录都需要验证码其他数字代表错误密码此数如3代表错误三次后出现验证码
OpenCaptchaTimeOut string // 防爆破验证码超时时间单位s(秒)
}
func (c *Captcha) OpenCaptchaTimeOutDuration() time.Duration {
d, err := time.ParseDuration(c.OpenCaptchaTimeOut)
if err != nil {
log.Panic(err)
}
return d
}

View File

@@ -1,109 +0,0 @@
package config
import (
"fmt"
)
// Database database config
type Database struct {
Driver string
MySQL *MySQL
SQLite *SQLite
Redis *Redis
PostgreSQL *PostgreSQL
}
// MySQL database config
type MySQL struct {
Host string
Port uint
Database string
Username string
Password string
Prefix string // 表前缀
Singular bool // 是否开启全局禁用复数true表示开启
MaxIdleConns int // 空闲中的最大连接数
MaxOpenConns int // 打开到数据库的最大连接数
Engine string // 数据库引擎默认InnoDB
}
func (m *MySQL) CreateDatabaseSql() string {
return fmt.Sprintf("CREATE DATABASE IF NOT EXISTS `%s` DEFAULT CHARACTER SET utf8mb4 DEFAULT COLLATE utf8mb4_general_ci;", m.Database)
}
func (m *MySQL) EmptyDsn() string {
dsnTpl := "%s@tcp(%s:%d)/"
authString := func() string {
if len(m.Password) > 0 {
return m.Username + ":" + m.Password
}
return m.Username
}
return fmt.Sprintf(dsnTpl, authString(), m.Host, m.Port)
}
// DSN connection dsn
func (m *MySQL) DSN() string {
dsnTpl := "%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local"
authString := func() string {
if len(m.Password) > 0 {
return m.Username + ":" + m.Password
}
return m.Username
}
return fmt.Sprintf(dsnTpl, authString(), m.Host, m.Port, m.Database)
}
type PostgreSQL struct {
Username string
Password string
Database string
Host string
Port uint
SslMode string
TimeZone string
Prefix string // 表前缀
Singular bool // 是否开启全局禁用复数true表示开启
MaxIdleConns int // 空闲中的最大连接数
MaxOpenConns int // 打开到数据库的最大连接数
}
func (m *PostgreSQL) EmptyDsn() string {
dsnTpl := "host=%s user=%s password=%s port=%d dbname=postgres sslmode=disable TimeZone=Asia/Shanghai"
return fmt.Sprintf(dsnTpl, m.Host, m.Username, m.Password, m.Port)
}
// DSN connection dsn
func (m *PostgreSQL) DSN() string {
dsnTpl := "host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s"
return fmt.Sprintf(dsnTpl, m.Host, m.Username, m.Password, m.Database, m.Port, m.SslMode, m.TimeZone)
}
type Redis struct {
Host string
Port uint
Database uint
Username string
Password string
}
// DSN connection dsn
func (m *Redis) DSN() string {
dsnTpl := "%s:%d"
return fmt.Sprintf(dsnTpl, m.Host, m.Port)
}
type SQLite struct {
File string
}
func (m *SQLite) CreateDatabaseSql() string {
return ""
}
func (m *SQLite) EmptyDsn() string {
return m.File
}

View File

@@ -1,51 +0,0 @@
package config
import (
"fmt"
"log"
"time"
)
type Http struct {
Static string
Host string
Port uint
Https bool
HttpsCert string
HttpKey string
Cors struct {
Mode string
Whitelist []Whitelist
}
JWT JWT
}
type JWT struct {
SigningKey string // jwt签名
ExpiresTime string // 过期时间
Issuer string // 签发者
}
func (j JWT) ExpiresTimeDuration() time.Duration {
d, err := time.ParseDuration(j.ExpiresTime)
if err != nil {
log.Fatal(err)
}
return d
}
type Whitelist struct {
AllowOrigin string
AllowHeaders string
AllowMethods string
ExposeHeaders string
AllowCredentials bool
}
func (h *Http) Address() string {
return fmt.Sprintf("%s:%d", h.Host, h.Port)
}
func (h *Http) PortString() string {
return fmt.Sprintf(":%d", h.Port)
}

View File

@@ -1,5 +0,0 @@
package config
type Log struct {
Level string
}

View File

@@ -1,29 +0,0 @@
package config
type Storage struct {
Driver string
AliYunOSS AliYunOSS
AwsS3 AwsS3
}
type AliYunOSS struct {
Bucket string
Region string
Endpoint string
AccessKeyID string
AccessKeySecret string
BaseURL string
Path string
}
type AwsS3 struct {
Bucket string
Region string
Endpoint string
DisableSSL bool
SecretID string
SecretKey string
BaseURL string
Path string
S3ForcePathStyle bool
}