首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在生产生成中使用LoadHTMLGlob加载html文件。它正在发展中

无法在生产生成中使用LoadHTMLGlob加载html文件。它正在发展中
EN

Stack Overflow用户
提问于 2022-03-15 08:59:17
回答 1查看 84关注 0票数 2

我在rest-API服务中使用Go Gin包。为了添加一些数据,我使用HTML文件提交带有数据的表单。在开发中,它是工作的,但是如果我评论'LoadHTMLGlob‘块服务器再次工作,那么在生产构建服务器中就不能工作了。我认为'LoadHTMLGlob‘不能加载HTML。请帮助解决这个问题。

我的main.go文件:

代码语言:javascript
复制
package main

import (
    "ct-merchant-api/Config"
    "ct-merchant-api/Routes"
    "fmt"
    "github.com/jinzhu/gorm"
)
var err error

func 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)
}

路由文件:

代码语言:javascript
复制
package Routes

import (
    "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
}

项目结构:

代码语言:javascript
复制
├── go.mod
├── go.sum
├── main.go
├── README.md
├── Routes
│   ── Routes.go
└── templates
|   ── lead-add-response.html
|   ── referral.html

为了进行部署,我在/lib/systemd/system中创建了一个服务/lib/systemd/system

go-web-api.service文件中:

代码语言:javascript
复制
[Unit]
Description=goweb

[Service]
Type=simple
Restart=always
RestartSec=5s
ExecStart={my_project_build_file_path}

[Install]
WantedBy=multi-user.target
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-15 11:00:54

需要将WorkingDirectory添加到系统文件中。

代码语言:javascript
复制
[Service]
Type=simple
Restart=always
RestartSec=5s
WorkingDirectory=/path/to/your/project //add this line
ExecStart={my_project_build_file_path}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71479447

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档