Learn about the core concepts of the Parka server, its configuration options, and how to extend it with custom routes and middleware
The Parka server is a flexible HTTP server built on top of the Echo web framework that provides both static file serving and dynamic template rendering capabilities. This document explains how the server works and how to extend it.
The Parka server is built around these main concepts:
To create a new Parka server, use the NewServer function with desired options:
server, err := server.NewServer(
server.WithPort(8080),
server.WithAddress("localhost"),
server.WithGzip(),
server.WithDefaultParkaRenderer(),
server.WithDefaultParkaStaticPaths(),
)
The server can be configured using various options:
WithPort(port uint16) - Sets the listening portWithAddress(address string) - Sets the listening addressWithGzip() - Enables Gzip compressionWithDefaultParkaRenderer() - Sets up the default template rendererWithDefaultParkaStaticPaths() - Configures default static file pathsWithStaticPaths(paths ...utils_fs.StaticPath) - Adds custom static file pathsWithDefaultRenderer(r *render.Renderer) - Sets a custom rendererStatic files can be served using the StaticPaths configuration. Each static path consists of:
Example:
staticPath := utils_fs.NewStaticPath(myFS, "/static")
server, err := server.NewServer(
server.WithStaticPaths(staticPath),
)
Parka uses a flexible template rendering system that supports:
The default renderer can be configured using:
options, err := server.GetDefaultParkaRendererOptions()
renderer, err := render.NewRenderer(options...)
server, err := server.NewServer(
server.WithDefaultRenderer(renderer),
)
Since Parka is built on Echo, you can add custom routes using the standard Echo routing system:
s.Router.GET("/api/hello", func(c echo.Context) error {
return c.JSON(http.StatusOK, map[string]string{
"message": "Hello, World!",
})
})
You can organize routes using Echo's group feature:
api := s.Router.Group("/api")
api.GET("/users", handleUsers)
api.POST("/users", createUser)
Parka comes with some default middleware:
Adding custom middleware:
s.Router.Use(myCustomMiddleware)
To start the server:
ctx := context.Background()
err := server.Run(ctx)
The server supports graceful shutdown through context cancellation.
Parka uses Echo's error handling system. You can customize error handling by implementing custom error handlers:
s.Router.HTTPErrorHandler = func(err error, c echo.Context) {
// Custom error handling logic
}
Here's a complete example of setting up a Parka server with custom routes and middleware:
server, err := server.NewServer(
server.WithPort(8080),
server.WithAddress("localhost"),
server.WithGzip(),
server.WithDefaultParkaRenderer(),
server.WithDefaultParkaStaticPaths(),
)
if err != nil {
log.Fatal(err)
}
// Add custom routes
server.Router.GET("/api/status", func(c echo.Context) error {
return c.JSON(http.StatusOK, map[string]string{
"status": "healthy",
})
})
// Add custom middleware
server.Router.Use(middleware.CORS())
// Start the server
ctx := context.Background()
if err := server.Run(ctx); err != nil {
log.Fatal(err)
}