Skip to main content

Command Server Options

Developer Advanced

consoleserver.NewCommandServer(serviceName, version, description, options ...WithServerOption) builds the stock Cobra root command for your application. This page documents the public server options you pass as variadic arguments: what each helper does, and when it runs relative to the built-in Gin stack.

For route-level and group-level middleware patterns, see Middleware.

WithServerOption

Each option is a WithServerOption: a func(*ServerOption) applied in order when the command server is constructed. The helpers below append hooks to that internal configuration.

WithEngineOptions

WithEngineOptions registers one or more func(*gin.Engine) callbacks. They run after the framework creates the Gin engine and attaches its default middleware (recovery, OpenTelemetry, Prometheus, request logging, CORS, delay), and before the service layer is initialized and API controllers are registered.

Use this for:

  • Global middleware that should run for every route (after the stock stack unless you replace engine behavior in your own fork).
  • Extra top-level routes or other engine-level setup.

You can pass multiple functions; they run in the order you list them.

Example: custom routes

package main

import (
"github.com/gin-gonic/gin"
consoleserver "github.com/sven-victor/ez-console/server"
)

func customRoutes(engine *gin.Engine) {
engine.GET("/custom", func(c *gin.Context) {
c.JSON(200, gin.H{"message": "Custom route"})
})
}

const VERSION = "1.0.0"

var rootCmd = consoleserver.NewCommandServer(
"my-app",
VERSION,
"My Application",
consoleserver.WithEngineOptions(customRoutes),
)

func main() {
rootCmd.Execute()
}

Example: global middleware

package main

import (
"time"

"github.com/gin-gonic/gin"
consoleserver "github.com/sven-victor/ez-console/server"
)

func rateLimitOnEngine(engine *gin.Engine) {
engine.Use(RateLimitMiddleware(100, time.Minute))
}

const VERSION = "1.0.0"

var rootCmd = consoleserver.NewCommandServer(
"my-app",
VERSION,
"My Application",
consoleserver.WithEngineOptions(rateLimitOnEngine),
)

func main() {
rootCmd.Execute()
}

RateLimitMiddleware here stands in for your own gin.HandlerFunc factory.

WithCommandOptions

WithCommandOptions registers one or more func(*cobra.Command) callbacks. They run on the root command after Use, Short, Version, and Run are wired, and before EZ-Console adds the built-in --config persistent flag and runs initFlags. Use this for extra flags, subcommands, or other CLI customization.

package main

import (
"github.com/spf13/cobra"
consoleserver "github.com/sven-victor/ez-console/server"
)

const VERSION = "1.0.0"

var rootCmd = consoleserver.NewCommandServer(
"my-app",
VERSION,
"My Application",
consoleserver.WithCommandOptions(func(cmd *cobra.Command) {
cmd.PersistentFlags().String("extra", "", "optional extra flag")
}),
)

func main() {
rootCmd.Execute()
}
  • Middleware — applying middleware on groups and routes, and using WithEngineOptions for global handlers
  • Architecture — where the Gin engine fits in the stack
  • Hooks & Events — service-level hooks (different extension point than engine options)