I have made a go rest API server using the Fiber
framework. I am using the default html template engine provided by Fiber to render .html
files placed within a directory called internal/ui/templates
.
My project structure is as follows:
├── bin
│ └── gwpl-api
├── cmd
│ └── main.go
├── go.mod
├── go.sum
├── internal
│ ├── app.go
│ ├── database
│ ├── plugins
│ │ ├── auth
│ │ │ └── auth.go
│ │ ├── customers
│ │ │ └── customers.go
│ │ └── plugins.go
│ └── ui
│ └── templates
│ └── index.html
├── Makefile
└── README.md
This is the code that executes the server and renders the template -
import (
"os"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/template/html/v2"
"github.com/preetiman-misra/gwpl-api/internal/plugins"
"go.uber.org/zap"
)
// App defines the main backend application.
type App struct {
port string
server *fiber.App
log *zap.Logger
}
// NewApp creates a new `App` struct.
func NewApp(port string) App {
return App{
port: port,
server: fiber.New(fiber.Config{
Views: html.New("internal/ui/templates", ".html"),
}),
log: zap.NewExample(),
}
}
// ServerHTTP Starts the fiber HTTP Server defined within `App`.
func (app *App) ServeHTTP() {
// Render index template
app.server.Get("/", func(c *fiber.Ctx) error {
data := map[string]string{
"Region": os.Getenv("FLY_REGION"),
}
return c.Render("index", data)
})
app.log.Info("Starting Fiber server.", zap.String("port", app.port))
app.server.Listen(":" + app.port)
}
These functions are called in the cmd/main.go
file, within the main()
function.
When I execute the binary built with the go build
command, it works perfectly on my local machine, regardless of where I move the binary, but as soon as I deploy the app to fly.io, I get a [Warn] failed to load views: lstat internal/ui/templates: no such file or directory
. when checking the Monitoring section on the application details page.
How do I remedy the situation?