2

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
5
  • Your HTML file is present in your production environment?
    – Prakash P
    Mar 15, 2022 at 10:07
  • There is most likely incorrect path in your production env. Please provide more info on file structure and how are you starting app.
    – medasx
    Mar 15, 2022 at 10:08
  • @medasx file structure added. please check Mar 15, 2022 at 10:16
  • @PrakashP yes, HTML file present in my production env. Mar 15, 2022 at 10:17
  • @MasudMorshed can you share the error message? Also see if this helps: stackoverflow.com/a/65548291/4408364 and github.com/gin-gonic/gin/issues/2444
    – Prakash P
    Mar 15, 2022 at 10:51

1 Answer 1

1

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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.