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

我的主菜.go文件:

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

路由文件:

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
}

项目 struct :

├── go.mod
├── go.sum
├── main.go
├── README.md
├── Routes
│   ── Routes.go
└── templates
|   ── lead-add-response.html
|   ── referral.html

对于部署,我在/lib/systemd/system中创建了一个服务go-web-api.service

go-web-api.service个文件中:

[Unit]
Description=goweb

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

[Install]
WantedBy=multi-user.target

推荐答案

您需要向系统文件中添加WorkingDirectory

[Service]
Type=simple
Restart=always
RestartSec=5s
WorkingDirectory=/path/to/your/project //add this line
ExecStart={my_project_build_file_path}

Html相关问答推荐

将网格包装在css中

Angular /HTML5不会播放本地文件夹中的音频mpeg

如何在用css使用网格视图时设置宽度?

如何翻转卡片图像的背面

如何显示自定义按钮以将产品添加到WooCommerce购物车?

减小HTML负载中base64字符串的大小

如何在css中旋转块时调整内容宽度、高度

视频重新加载而不是在 Swift 中暂停 WKWebView

如何创建淡出研磨背景

CSS Grid 布局:1 大图和 3 小图

为什么每当我调整图片大小时它都会使我的其他元素移动

如何将 HTML DateTime 转换为 Golang Time 对象

HTML 如何根据屏幕尺寸调整/裁剪视频背景?

是否可以使 huxtable 输出悬停?

无法使用 flexbox 裁剪图像

如何在 svelte 中对静态 html 文件使用href=

在 React (NEXT) 中,当我try 进行动态补充时,我无法使用实例this来引用输入

Css 左属性问题

如何向上移动图像并溢出图像的一部分而另一部分不溢出

有没有办法在 CSS 类子句中指定多个 HTML 元素?