Compare commits
4 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
65d34e17f2 | |
|
|
e7ae12b939 | |
|
|
8ed0a4c936 | |
|
|
3aecbceef8 |
|
|
@ -2,6 +2,7 @@ package gin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
@ -23,10 +24,6 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func Service() {
|
func Service() {
|
||||||
rootPath = os.Getenv("GIN_PATH")
|
|
||||||
if rootPath == "" {
|
|
||||||
rootPath = "/"
|
|
||||||
}
|
|
||||||
addr = os.Getenv("GIN_ADDR")
|
addr = os.Getenv("GIN_ADDR")
|
||||||
if addr == "" {
|
if addr == "" {
|
||||||
addr = "0.0.0.0"
|
addr = "0.0.0.0"
|
||||||
|
|
@ -41,8 +38,9 @@ func Service() {
|
||||||
sslKey = os.Getenv("GIN_SSL_KEY")
|
sslKey = os.Getenv("GIN_SSL_KEY")
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
gin.DefaultWriter = io.Discard
|
||||||
router := gin.New()
|
router := gin.New()
|
||||||
routerSetup(router, &rootPath)
|
routerSetup(router)
|
||||||
|
|
||||||
if ssl == "true" {
|
if ssl == "true" {
|
||||||
router.Use(tlsHandler())
|
router.Use(tlsHandler())
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,14 @@
|
||||||
package gin
|
package gin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 路由配置
|
// 路由配置
|
||||||
func routerSetup(router *gin.Engine, rootpath *string) {
|
func routerSetup(router *gin.Engine) {
|
||||||
router.Use(gin.Recovery())
|
router.Use(gin.Recovery())
|
||||||
|
|
||||||
r := router.Group(fmt.Sprintf("/%s", *rootpath))
|
r := router.Group(`/`)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,11 @@ import (
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
|
注意,当天文件放项目根目录下
|
||||||
|
|
||||||
|
env LOGLEVEL=debug
|
||||||
|
*/
|
||||||
func init() {
|
func init() {
|
||||||
//日志初始化
|
//日志初始化
|
||||||
level := os.Getenv("LOGLEVEL")
|
level := os.Getenv("LOGLEVEL")
|
||||||
|
|
|
||||||
|
|
@ -15,11 +15,11 @@ import (
|
||||||
/*
|
/*
|
||||||
docker配置
|
docker配置
|
||||||
|
|
||||||
MYSQL_DSN=root:root@tcp(mysql:3306)/sample?charset=utf8mb4&parseTime=True&loc=Local \
|
env MYSQL_DSN=root:root@tcp(mysql:3306)/sample?charset=utf8mb4&parseTime=True&loc=Local
|
||||||
MYSQL_MAXLIFETIME=2\
|
env MYSQL_MAXLIFETIME=2
|
||||||
MYSQL_MAXIDLECONNS=2\
|
env MYSQL_MAXIDLECONNS=2
|
||||||
MYSQL_MAXOPENCONNS=200\
|
env MYSQL_MAXOPENCONNS=200
|
||||||
MYSQL_INIT=true \
|
env MYSQL_INIT=true
|
||||||
*/
|
*/
|
||||||
var (
|
var (
|
||||||
_db *gorm.DB
|
_db *gorm.DB
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,96 @@
|
||||||
|
package postgres
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gorm.io/driver/postgres"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"gorm.io/gorm/logger"
|
||||||
|
"gorm.io/gorm/schema"
|
||||||
|
"gorm.io/plugin/dbresolver"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
_db *gorm.DB
|
||||||
|
)
|
||||||
|
|
||||||
|
// 创建实例
|
||||||
|
func newDB() (*gorm.DB, error) {
|
||||||
|
if _db != nil {
|
||||||
|
return _db, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
dsn := os.Getenv("POSTGRES_DSN")
|
||||||
|
if dsn == "" {
|
||||||
|
dsn = "host=localhost user=postgres password=postgres dbname=postgres port=5432 sslmode=disable TimeZone=Asia/Shanghai"
|
||||||
|
}
|
||||||
|
maxLifetime := func() int {
|
||||||
|
c := os.Getenv("MAXLIFETIME")
|
||||||
|
cc, err := strconv.Atoi(c)
|
||||||
|
if err != nil {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
if cc <= 0 {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
if cc >= 1000 {
|
||||||
|
cc = 1000
|
||||||
|
}
|
||||||
|
return cc
|
||||||
|
}()
|
||||||
|
maxIdleConns := func() int {
|
||||||
|
c := os.Getenv("MAXIDLECONNS")
|
||||||
|
cc, err := strconv.Atoi(c)
|
||||||
|
if err != nil {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
if cc < 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
if cc >= 1000 {
|
||||||
|
cc = 1000
|
||||||
|
}
|
||||||
|
return cc
|
||||||
|
}()
|
||||||
|
maxOpenConns := func() int {
|
||||||
|
c := os.Getenv("MAXOPENCONNS")
|
||||||
|
cc, err := strconv.Atoi(c)
|
||||||
|
if err != nil {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
if cc < 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
if cc >= 1000 {
|
||||||
|
cc = 1000
|
||||||
|
}
|
||||||
|
return cc
|
||||||
|
}()
|
||||||
|
|
||||||
|
var err error
|
||||||
|
_db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{
|
||||||
|
SkipDefaultTransaction: true,
|
||||||
|
Logger: logger.Default.LogMode(logger.Silent),
|
||||||
|
NamingStrategy: schema.NamingStrategy{
|
||||||
|
SingularTable: true,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
_db.Use(
|
||||||
|
dbresolver.Register(dbresolver.Config{
|
||||||
|
Sources: []gorm.Dialector{postgres.Open(dsn)},
|
||||||
|
Replicas: []gorm.Dialector{postgres.Open(dsn)},
|
||||||
|
Policy: dbresolver.RandomPolicy{},
|
||||||
|
}).SetConnMaxIdleTime(time.Hour).
|
||||||
|
SetConnMaxLifetime(time.Duration(maxLifetime) * time.Hour).
|
||||||
|
SetMaxIdleConns(maxIdleConns).
|
||||||
|
SetMaxOpenConns(maxOpenConns))
|
||||||
|
return _db, nil
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
package postgres
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"myschools.me/community/community-api/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
if os.Getenv("POSTGRES_INIT") != "true" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
db, err := newDB()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if err := db.AutoMigrate(&model.User{}); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if err := db.AutoMigrate(&model.Premises{}); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if err := db.AutoMigrate(&model.Application{}, &model.ApplicationMenu{}); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -10,9 +10,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
REDIS_DSN=127.0.0.1:6379 \
|
env REDIS_DSN=127.0.0.1:6379
|
||||||
REDIS_PWD=eYX7EwxKPCDmwMtyKVge8oLd2t81 \
|
env REDIS_PWD=
|
||||||
REDIS_DB=0 \
|
env REDIS_DB=0
|
||||||
*/
|
*/
|
||||||
var pool *redigo.Pool
|
var pool *redigo.Pool
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue