-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
160 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
server: | ||
port: 8088 | ||
config: | ||
user: | ||
score: 100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package internal | ||
|
||
import ( | ||
"gopkg.in/yaml.v2" | ||
"io/ioutil" | ||
"os" | ||
) | ||
|
||
type ServerConfig struct { | ||
Port int32 | ||
Name string | ||
} | ||
|
||
type UserConfig map[string]interface{} | ||
|
||
type SysConfig struct { | ||
Server *ServerConfig | ||
Config UserConfig | ||
} | ||
|
||
func NewSysConfig() *SysConfig { | ||
return &SysConfig{Server: &ServerConfig{Port: 8080, Name: "tgin"}} | ||
} | ||
|
||
func InitConfig() *SysConfig { | ||
config := NewSysConfig() | ||
if b := LoadConfigFile(); b != nil { | ||
err := yaml.Unmarshal(b, config) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
return config | ||
} | ||
|
||
func LoadConfigFile() []byte { | ||
dir, _ := os.Getwd() | ||
file := dir + "/application.yaml" | ||
b, err := ioutil.ReadFile(file) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package internal | ||
|
||
import ( | ||
"gorm.io/driver/mysql" | ||
"gorm.io/gorm" | ||
"log" | ||
"time" | ||
) | ||
|
||
type GormAdapter struct { | ||
*gorm.DB | ||
} | ||
|
||
func NewGormAdapter() *GormAdapter { | ||
dsn := "root:root@tcp(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=True&loc=Local" | ||
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
sqlDB, err := db.DB() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
if err := sqlDB.Ping(); err == nil { | ||
log.Println("数据库连接成功") | ||
} | ||
// SetMaxIdleConns 用于设置连接池中空闲连接的最大数量。 | ||
sqlDB.SetMaxIdleConns(10) | ||
// SetMaxOpenConns 设置打开数据库连接的最大数量。 | ||
sqlDB.SetMaxOpenConns(100) | ||
// SetConnMaxLifetime 设置了连接可复用的最大时间。 | ||
sqlDB.SetConnMaxLifetime(time.Hour) | ||
return &GormAdapter{DB: db} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters