add some utils

This commit is contained in:
yanghao05
2023-01-30 16:57:48 +08:00
parent 16f80cc297
commit 27bb527373
10 changed files with 384 additions and 6 deletions

View File

@@ -10,13 +10,12 @@ import (
"github.com/spf13/viper"
)
var c *Config
type Config struct {
App App
Http Http
Log Log
Database Database
Storage Storage
}
func init() {

View File

@@ -1,10 +1,14 @@
package config
import "fmt"
import (
"fmt"
)
// Database database config
type Database struct {
Driver string
MySQL MySQL
Redis Redis
PostgreSQL PostgreSQL
}
@@ -17,7 +21,7 @@ type MySQL struct {
Password string
}
// DSN is the mysql connection dsn
// DSN connection dsn
func (m *MySQL) DSN() string {
dsnTpl := "%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local"
@@ -41,8 +45,22 @@ type PostgreSQL struct {
TimeZone string
}
// DSN is the mysql connection dsn
// 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.User, 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)
}

View File

@@ -1,6 +1,10 @@
package config
import "fmt"
import (
"fmt"
"log"
"time"
)
type Http struct {
Static string
@@ -13,6 +17,30 @@ type Http struct {
Mode string
Whitelist []Whitelist
}
JWT JWT
}
type JWT struct {
SigningKey string // jwt签名
ExpiresTime string // 过期时间
BufferTime string // 缓冲时间
Issuer string // 签发者
}
func (j JWT) ExpiresTimeDuration() time.Duration {
d, err := time.ParseDuration(j.ExpiresTime)
if err != nil {
log.Fatal(err)
}
return d
}
func (j JWT) BufferTimeDuration() time.Duration {
d, err := time.ParseDuration(j.BufferTime)
if err != nil {
log.Fatal(err)
}
return d
}
type Whitelist struct {

View File

@@ -0,0 +1,29 @@
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
}