Last active
April 30, 2020 18:41
-
-
Save thanhdatvo/251b9a96bc950117fa3cfd43ac024d88 to your computer and use it in GitHub Desktop.
Gofiber start function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"gofiber/starter/routes" | |
"github.com/gofiber/fiber" | |
"github.com/gofiber/logger" | |
"github.com/gofiber/template" | |
) | |
func main() { | |
app := fiber.New() | |
// view engine setup | |
app.Settings.TemplateFolder = "./views" | |
app.Settings.TemplateEngine = template.Pug() | |
app.Settings.TemplateExtension = ".jade" | |
app.Use(logger.New()) | |
app.Static("/", "./public") | |
routes.SetupIndexRouter(app.Group("/")) | |
routes.SetupUsersRouter(app.Group("/users")) | |
// catch 404 error handler | |
app.Use("/*", func(c *fiber.Ctx) { | |
bind := fiber.Map{ | |
"title": "Page not found", | |
"message": "Page not found", | |
"error": map[string]interface{}{ | |
"status": 400, | |
"stack": "", | |
}, | |
} | |
// render the error page | |
if err := c.Render("./error", bind); err != nil { | |
c.Status(500).Send(err.Error()) | |
} | |
}) | |
app.Listen(3000) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment