use config class

This commit is contained in:
w-mj 2023-02-28 10:07:55 +08:00
parent 45b0d2f388
commit 3fcc8d392d
No known key found for this signature in database
GPG Key ID: 3A2CB5BE2F835897
5 changed files with 68 additions and 29 deletions

17
main.go
View File

@ -21,21 +21,10 @@ import (
//go:embed template/* static/*
var f embed.FS
var jwtKey string
var aseKey string
func main() {
m.InitConfig()
m.InitClient()
jwtKey = os.Getenv("JWT_KEY")
aseKey = os.Getenv("ASE_KEY")
if jwtKey == "" {
panic("No jwt key")
}
if aseKey == "" {
panic("No ase key")
}
r := gin.Default()
fp, _ := fs.Sub(f, "static")
@ -76,10 +65,10 @@ func ParseCGToken(token string) (string, error) {
return "", err
}
jwtString := AesDecryptECB(aesEncrypt, []byte(aseKey))
jwtString := AesDecryptECB(aesEncrypt, []byte(m.GetConfig().AseKey))
jwtResult, err := jwt.ParseWithClaims(string(jwtString), &CGJWTClaim{}, func(token *jwt.Token) (i interface{}, err error) {
return []byte(jwtKey), nil
return []byte(m.GetConfig().JwtKey), nil
// return hmac.New(sha256.New, []byte(jwtKey)), nil
})
if err != nil {

60
src/config.go Normal file
View File

@ -0,0 +1,60 @@
package k8s_manager
import (
"fmt"
"os"
"reflect"
)
type Config struct {
JwtKey string
AseKey string
DBHost string
DBUser string
DBPassword string
NFSHost string
NFSPath string
}
var config *Config
func getField(v *Config, field string) string {
r := reflect.ValueOf(v)
f := reflect.Indirect(r).FieldByName(field)
return f.String()
}
func setField(v *Config, field string, value string) {
r := reflect.ValueOf(v)
f := reflect.Indirect(r).FieldByName(field)
f.SetString(value)
}
func InitConfig() {
config = &Config{}
config.JwtKey = os.Getenv("JWT_KEY")
config.AseKey = os.Getenv("ASE_KEY")
config.DBHost = os.Getenv("DB_HOST")
config.DBUser = os.Getenv("DB_USER")
config.DBPassword = os.Getenv("DB_PASS")
config.NFSHost = os.Getenv("NFS_HOST")
config.NFSPath = os.Getenv("NFS_PATH")
mustFields := []string{"JwtKey", "AseKey", "DBHost", "DBUser", "DBPassword"}
optionalFields := map[string]string{}
for _, f := range mustFields {
if getField(config, f) == "" {
panic(fmt.Sprintf("field %s must be set.", f))
}
}
for f, v := range optionalFields {
if getField(config, f) == "" {
setField(config, f, v)
}
}
}
func GetConfig() *Config {
return config
}

View File

@ -362,7 +362,7 @@ func (c *K8sClient) CreatePersistentVolume(app *AppModel) {
StorageClassName: app.StorageClassName(),
PersistentVolumeSource: v1.PersistentVolumeSource{
NFS: &v1.NFSVolumeSource{
Server: "192.168.1.1",
Server: "192.168.252.250",
Path: app.NFSPath(),
ReadOnly: false,
},

View File

@ -5,7 +5,6 @@ import (
"github.com/sirupsen/logrus"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"os"
"sync"
)
@ -16,18 +15,9 @@ func opendb() *gorm.DB {
if instance == nil {
lock.Lock()
if instance == nil {
db_host, ok := os.LookupEnv("DB_HOST")
if !ok {
panic("DB_HOST NOT SET")
}
db_user, ok := os.LookupEnv("DB_USER")
if !ok {
panic("DB_USER NOT SET")
}
db_pass, ok := os.LookupEnv("DB_PASS")
if !ok {
panic("DB_PASS NOT SET")
}
db_host := GetConfig().DBHost
db_user := GetConfig().DBUser
db_pass := GetConfig().DBPassword
dsn := fmt.Sprintf("%s:%s@tcp(%s:3306)/k8s?charset=utf8mb4&parseTime=True&loc=Local", db_user, db_pass, db_host)
var err error
instance, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})

View File

@ -112,7 +112,7 @@
<div class="row">
<label for="app-ports" class="form-label">端口号</label>
<input type="text" id="app-ports" class="form-control" v-model="selectApp.Ports"
placeholder="端口号,多个端口以逗号分隔"/>
placeholder="端口号"/>
</div>
<div class="row">