我在我的 rest-API 服務中使用 Go Gin 包。為了添加一些資料,我使用 HTML 檔案來提交帶有資料的表單。在開發中,它正在作業,但在生產構建服務器中不起作用,如果我評論“LoadHTMLGlob”塊服務器再次作業。我認為“LoadHTMLGlob”無法加載 HTML。請幫助解決這個問題。
我的 main.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
}
專案結構:
├── go.mod
├── go.sum
├── main.go
├── README.md
├── Routes
│ ── Routes.go
└── templates
| ── lead-add-response.html
| ── referral.html
對于部署,我go-web-api.service在/lib/systemd/system
在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
uj5u.com熱心網友回復:
您需要添加WorkingDirectory到您的系統檔案
[Service]
Type=simple
Restart=always
RestartSec=5s
WorkingDirectory=/path/to/your/project //add this line
ExecStart={my_project_build_file_path}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/444024.html
