为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 https 中以生产模式运行 Beego 二进制文件?

如何在 https 中以生产模式运行 Beego 二进制文件?

Go
守着一只汪 2022-05-05 17:51:44
我的 Go 应用程序在 https 模式下运行。我正在使用 Beego 框架。我的 app.conf 看起来像:appname = myprojecthttpsport = 10555httpport = 8082runmode = prodEnableHTTP = falseEnableHTTPS = trueHTTPSCertFile = server.crtHTTPSKeyFile = server.keycopyrequestbody = true当我在本地运行它时,它运行得非常好,GoLang 服务器在 10555 上使用 https 运行。但是当我构建二进制文件并尝试在另一台服务器上运行它时,它只尝试在 http 端口 8082 上运行。
查看完整描述

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


查看完整回答
反对 回复 2022-05-05
?
慕无忌1623718

TA贡献1744条经验 获得超4个赞

我认为这意味着 httpport 是服务器/本地服务器中的一个端口,与 http 或 https 需要什么不同。

您可以对该环境使用拆分方法,例如用于环境httpport=8082暂存服务器的 development.conf () 和用于环境httpport=10555生产服务器的 production.conf (),然后您可以在部署时将环境替换为 app.conf


查看完整回答
反对 回复 2022-05-05
  • 2 回答
  • 0 关注
  • 105 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信