2 回答
TA贡献1865条经验 获得超7个赞
I used it like this :
----app.config
{
appname = firstapp
# httpport = 8080
runmode = dev //when in test comment this line
#runmode = test //when in test uncomment this line
autorender = false
copyrequestbody = true
EnableDocs = true
[dev]
httpport = 8080
# [prod]
# httpport = 8088
[test]
httpport = 8888
}
-----main.go
func init() {
// 1 - Enable debug
orm.Debug = false
// 2 - Register object with Beego ORM
orm.RegisterModel(new(models.User), new(models.Role), new(models.Function), new(models.Role_function))
//orm.RegisterModel(new(models.User), new(models.Role))
if beego.BConfig.RunMode == "dev" {
// 3 - Fetch database properties stored as YAML, decode secrets
connStr, err := util.FetchYAMLDEV()
if err != nil {
log.Fatalf("error: %v", err)
}
// 4 - Register posgres driver and db
orm.RegisterDriver("postgres", orm.DRPostgres)
orm.RegisterDataBase("default", "postgres", connStr)
// 5 - Create and load DB with sample data, can comment after initial load
models.CreateDb()
models.LoadDb()
}
if beego.BConfig.RunMode == "test" {
// 3 - Fetch database properties stored as YAML, decode secrets
fmt.Println("in test runmofe")
connStr, err := util.FetchYAMLTEST()
if err != nil {
log.Fatalf("error: %v", err)
}
// 4 - Register posgres driver and db
orm.RegisterDriver("postgres", orm.DRPostgres)
orm.RegisterDataBase("default", "postgres", connStr)
// 5 - Create and load DB with sample data, can comment after initial load
models.CreateDb()
models.LoadDb()
}
}
----util.FetchYAML.go
package util
import (
"fmt"
"os"
"log"
"github.com/joho/godotenv"
)
func goDotEnvVariable(key string) string {
// load .env file
err := godotenv.Load(".env")
if err != nil {
log.Fatalf("Error loading .env file")
}
return os.Getenv(key)
}
func FetchYAMLDEV() (string, error) {
user := goDotEnvVariable("user_dev")
dbname := goDotEnvVariable("dbname_dev")
password := goDotEnvVariable("password_dev")
host := goDotEnvVariable("host_dev")
port := goDotEnvVariable("port_dev")
return fmt.Sprintf("user=%s dbname=%s password=%s host=%s port=%s sslmode=disable",
user, dbname, password, host, port), nil
}
func FetchYAMLTEST() (string, error) {
user := goDotEnvVariable("user")
dbname := goDotEnvVariable("dbname")
password := goDotEnvVariable("password")
host := goDotEnvVariable("host")
port := goDotEnvVariable("port")
return fmt.Sprintf("user=%s dbname=%s password=%s host=%s port=%s sslmode=disable",
user, dbname, password, host, port), nil
}
----.env
ACCESS_SECRET = vjhbjhjh
user = postgres
dbname = golang
password = password
port = 5432
host = localhost
sslmode = disable
user_dev = devuser
dbname_dev = defdbname
password_dev = devpass
port_dev = 5432
host_dev = devhost
sslmode_dev = disable
user_test = testuser
dbname_test = testdbname
password_test = testpass
port_test = 5432
host_test = testhost
sslmode_test = disable
TA贡献1744条经验 获得超4个赞
我认为这意味着 httpport 是服务器/本地服务器中的一个端口,与 http 或 https 需要什么不同。
您可以对该环境使用拆分方法,例如用于环境httpport=8082
暂存服务器的 development.conf () 和用于环境httpport=10555
生产服务器的 production.conf (),然后您可以在部署时将环境替换为 app.conf
- 2 回答
- 0 关注
- 105 浏览
添加回答
举报