英文:
Can't load html file with LoadHTMLGlob in production build. It's working in development
问题
我正在使用Go Gin包来构建我的REST API服务。为了添加一些数据,我使用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()
// 加载HTML模板
r.LoadHTMLGlob("templates/*")
// 运行服务器
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
部署时,我在/lib/systemd/system
目录下创建了一个名为go-web-api.service
的服务。
在go-web-api.service
文件中:
[Unit]
Description=goweb
[Service]
Type=simple
Restart=always
RestartSec=5s
ExecStart={我的项目构建文件路径}
[Install]
WantedBy=multi-user.target
请注意将{my_project_build_file_path}
替换为你的项目构建文件的路径。
英文:
I'm using the Go Gin package in my rest-API service. To add some data I used HTML file to submit the form with data. In development, it's working, but in the production build server not working, if I commented 'LoadHTMLGlob' block server working again. I think 'LoadHTMLGlob' can't load HTML. Please help to solve this issue.
my main.go file:
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)
}
Route file:
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
}
Project Structure:
├── go.mod
├── go.sum
├── main.go
├── README.md
├── Routes
│ ── Routes.go
└── templates
| ── lead-add-response.html
| ── referral.html
For Deployment I Create a service go-web-api.service
in /lib/systemd/system
In go-web-api.service
file:
[Unit]
Description=goweb
[Service]
Type=simple
Restart=always
RestartSec=5s
ExecStart={my_project_build_file_path}
[Install]
WantedBy=multi-user.target
答案1
得分: 1
你需要在系统文件中添加WorkingDirectory
。
[Service]
Type=simple
Restart=always
RestartSec=5s
WorkingDirectory=/path/to/your/project //添加这一行
ExecStart={my_project_build_file_path}
英文:
You need to add WorkingDirectory
to your system file
[Service]
Type=simple
Restart=always
RestartSec=5s
WorkingDirectory=/path/to/your/project //add this line
ExecStart={my_project_build_file_path}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论