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
CodePudding user response:
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}