Learn how to configure Parka servers using YAML configuration files to define routes, handlers, and their settings
Parka servers can be configured using YAML configuration files that define routes, handlers, and their settings. This document explains how to use config files to set up your Parka server, with a focus on integrating Glazed commands and other handlers.
A Parka config file allows you to:
The configuration file has this basic structure:
defaults:
useParkaStaticFiles: true
renderer:
useDefaultParkaRenderer: true
templateDirectory: "./templates"
markdownBaseTemplateName: "base.tmpl.html"
routes:
- path: "/api"
commandDirectory:
repositories:
- "./commands"
includeDefaultRepositories: true
templateLookup:
directories:
- "./templates"
indexTemplateName: "commands/index.tmpl.html"
defaults:
flags:
limit: 100
The defaults section configures global settings for the server:
defaults:
# Whether to use Parka's built-in static files (CSS, JS, etc.)
useParkaStaticFiles: true
# Renderer configuration for templates
renderer:
# Use Parka's default renderer (includes markdown support)
useDefaultParkaRenderer: true
# Directory containing templates
templateDirectory: "./templates"
# Base template for markdown rendering
markdownBaseTemplateName: "base.tmpl.html"
Routes define the URL paths and their corresponding handlers. Each route can use one of several handler types:
The Command Directory handler serves multiple Glazed commands from a repository:
routes:
- path: "/commands"
commandDirectory:
# List of directories containing command definitions
repositories:
- "./commands"
- "./more-commands"
# Include repositories from environment variables
includeDefaultRepositories: true
# Template configuration
templateLookup:
directories:
- "./templates/commands"
indexTemplateName: "index.tmpl.html"
# Default parameter values
defaults:
flags:
limit: 100
format: "table"
# Parameter overrides
overrides:
layers:
glazed:
filter:
- id
- name
# Additional data passed to templates
additionalData:
title: "Command Repository"
For serving individual Glazed commands:
routes:
- path: "/hello"
command:
name: "hello"
templateName: "command.tmpl.html"
defaults:
flags:
greeting: "Hello"
Serves a directory of templates with support for both HTML and Markdown:
routes:
- path: "/docs"
templateDirectory:
localDirectory: "./templates"
indexTemplateName: "index.tmpl.html"
markdownBaseTemplateName: "base.tmpl.html"
alwaysReload: true
For serving a single template:
routes:
- path: "/"
template:
templateFile: "index.tmpl.html"
alwaysReload: true
Serves static files from a directory:
routes:
- path: "/static"
static:
localPath: "./static"
Serves a single static file:
routes:
- path: "/favicon.ico"
staticFile:
localPath: "./static/favicon.ico"
When integrating Glazed commands, you can configure various aspects of their behavior through the config file:
Control which parameters are exposed and their default values:
commandDirectory:
defaults:
flags:
limit: 100
format: "json"
layers:
sql-connection:
host: "localhost"
port: 5432
overrides:
layers:
glazed:
filter:
- id
- name
Configure how commands are rendered in the web interface:
commandDirectory:
templateLookup:
directories:
- "./templates/commands"
indexTemplateName: "commands/index.tmpl.html"
defaultTemplateName: "commands/view.tmpl.html"
Development mode can be enabled through the configuration file or programmatically. It affects various aspects of the server:
Example configuration with development settings:
defaults:
renderer:
alwaysReload: true
routes:
- path: "/api"
commandDirectory:
devMode: true
alwaysReload: true
Here's an example of how to use a config file in your Parka server:
func main() {
// Read config file
configData, err := os.ReadFile("config.yaml")
if err != nil {
log.Fatal(err)
}
// Parse config
configFile, err := config.ParseConfig(configData)
if err != nil {
log.Fatal(err)
}
// Create server
server, err := server.NewServer(
server.WithPort(8080),
server.WithAddress("localhost"),
)
if err != nil {
log.Fatal(err)
}
// Create config file handler
cfh := handlers.NewConfigFileHandler(
configFile,
handlers.WithDevMode(true),
handlers.WithRepositoryFactory(myRepositoryFactory),
handlers.WithAppendCommandDirHandlerOptions(
command_dir.WithDevMode(true),
),
)
// Serve
if err := cfh.Serve(server); err != nil {
log.Fatal(err)
}
// Run server with config file watching
ctx := context.Background()
if err := runConfigFileHandler(ctx, server, cfh); err != nil {
log.Fatal(err)
}
}