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

无法在生产构建中使用 LoadHTMLGlob 加载 html 文件。它正在开发中

无法在生产构建中使用 LoadHTMLGlob 加载 html 文件。它正在开发中

Go
偶然的你 2022-11-23 14:19:30
我在我的 rest-API 服务中使用 Go Gin 包。为了添加一些数据,我使用 HTML 文件提交带有数据的表单。在开发中,它工作正常,但在生产构建服务器中不工作,如果我评论“LoadHTMLGlob”阻止服务器再次工作。我认为“LoadHTMLGlob”无法加载 HTML。请帮助解决这个问题。我的 main.go 文件:package mainimport (    "ct-merchant-api/Config"    "ct-merchant-api/Routes"    "fmt"    "github.com/jinzhu/gorm")var err errorfunc main() {    Config.DB, err = gorm.Open("mysql", Config.DbURL(Config.BuildDBConfig()))    if err != nil {        fmt.Println("Status:", err)    }    defer Config.DB.Close()    r := Routes.SetupRouter()        // Load HTML       r.LoadHTMLGlob("templates/*")        //running    runningPort := Config.GetServerInfo()    _ = r.Run(":" + runningPort.ServerPort)}路由文件:package Routesimport (    "ct-merchant-api/Controllers/Referral"    "github.com/gin-contrib/cors"    "github.com/gin-gonic/gin"    "net/http")func SetupRouter() *gin.Engine {    api := gin.Default()    config := cors.DefaultConfig()    config.AllowAllOrigins = true    config.AllowCredentials = true    config.AddAllowHeaders("authorization")    api.Use(cors.New(config))    api.GET("/", func(c *gin.Context) {        c.String(http.StatusOK, "Welcome to GO-rib Server")    })    api.GET("/referral/:merchantId", Referral.LeadForm)    api.POST("/add-lead", Referral.LeadAdd)    return api}项目结构:├── go.mod├── go.sum├── main.go├── README.md├── Routes│   ── Routes.go└── templates|   ── lead-add-response.html|   ── referral.htmlgo-web-api.service对于部署,我在中创建服务/lib/systemd/system在go-web-api.service文件中:[Unit]Description=goweb[Service]Type=simpleRestart=alwaysRestartSec=5sExecStart={my_project_build_file_path}[Install]WantedBy=multi-user.target
查看完整描述

1 回答

?
慕哥9229398

TA贡献1877条经验 获得超6个赞

你需要添加WorkingDirectory到你的系统文件


[Service]

Type=simple

Restart=always

RestartSec=5s

WorkingDirectory=/path/to/your/project //add this line

ExecStart={my_project_build_file_path}


查看完整回答
反对 回复 2022-11-23
  • 1 回答
  • 0 关注
  • 122 浏览
慕课专栏
更多

添加回答

举报

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