1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| package app
import ( "cmn" _ "github.com/joho/godotenv/autoload" "github.com/go-xorm/xorm" "fmt" "runtime" "strings" "github.com/joho/godotenv" )
var ( Debug bool Name string Version string HttpPort string DB *xorm.Engine Log *cmn.Logger )
const VERSION = "1.0"
func init() { _, filename, _, _ := runtime.Caller(0) path := strings.Split(filename, "/") env := fmt.Sprintf("%s/.env", strings.Join(path[:len(path)-2], "/")) godotenv.Load(env, ".env") if debug := cmn.GetEnv("APP_DEBUG", "false"); debug == "true" { Debug = true } Name = cmn.GetEnv("APP_NAME", "example") Version = VERSION HttpPort = cmn.GetEnv("HTTP_PORT", "8002") LogOption := &cmn.LogOption{ Name: "example", Debug: Debug, } var err error Log, err = cmn.NewLogger(cmn.LogAdapterStdout, LogOption) if err != nil { panic(err) }
DBOption := &cmn.DBOption{ Driver: cmn.GetEnv("DB_DRIVER", "mysql"), Host: cmn.GetEnv("DB_HOST", "127.0.0.1"), Port: cmn.GetEnv("DB_PORT", "3306"), Database: cmn.GetEnv("DB_DATABASE", "test"), User: cmn.GetEnv("DB_USERNAME", "root"), Password: cmn.GetEnv("DB_PASSWORD", ""), Timeout: cmn.GetEnv("DB_TIMEOUT", "3s"), Debug: Debug, Logger: Log, } DB, err = cmn.NewDB(DBOption) }
|