I am very new to go and fiber and am struggling with creating a server in a package and then sharing the context in another package.
// package main
func init() {
// Init server
globs.InitServer()
}
func main() {
globs.Server.Get("/hello", handlers.HelloWorld)
}
and I am initializing the server as;
// package globs
// global vars
var (
Conf map[string]string
DBPool *pgxpool.Pool
Loggi *zap.Logger
Server *fiber.App
)
func InitServer() {
srv := fiber.New()
srv.Use(logger.New())
Server = srv
Server.Listen(":3000")
}
Finally, I am trying handle the controllers as;
// package handlers
func HelloWorld(c *fiber.Ctx) error {
globs.Loggi.Info("Says Hello")
return c.SendString("Hello, World 👋!")
}
I am not getting any error during compilation and the app starts fine, but it is not recognising any routes. If i goto "/hello", it gives a 404 and show "Cannot GET /hello"
You are calling Server.Listen
in the init
function. Server.Listen
does not return until listening fails, so you have to move that to the end of main, after you setup everything.