Skip to content

6. Interactive OpenAPI Docs ​

GoForge uses Huma v2 (github.com/danielgtaylor/huma/v2) via server.NewHumaConfig to serve interactive OpenAPI 3.1 documentation UIs and .json schemas per service.

In this chapter, we document both auth (Limen's wildcard routes) and task (your CRUD endpoints) with typed schemas and security schemes (AppKey + SessionCookie).


6.1 Documenting Limen Auth Endpoints (internal/services/auth/docs.go) ​

Because Limen mounts a wildcard Gin route (/api/auth/*action), calling huma.Register on /api/auth/login would collide with Gin's router. Instead, we use api.OpenAPI().AddOperation(...) to publish full OpenAPI 3.1 schemas for Limen's endpoints without registering duplicate Gin handlers:

go
package auth

import (
	"net/http"

	"github.com/acme/taskforge/boot/server"
	"github.com/danielgtaylor/huma/v2"
	"github.com/danielgtaylor/huma/v2/adapters/humagin"
	"github.com/gin-gonic/gin"
)

type AuthDocs struct{}

func NewAuthDocs() *AuthDocs {
	return &AuthDocs{}
}

func (d *AuthDocs) Register(engine *gin.Engine) {
	cfg := server.NewHumaConfig("TaskForge Auth API", "1.0.0", "/api/docs/auth")
	cfg.Components.SecuritySchemes["SessionCookie"] = &huma.SecurityScheme{
		Type: "apiKey",
		In:   "cookie",
		Name: "limen_session",
	}

	api := humagin.New(engine, cfg)
	oapi := api.OpenAPI()

	userSchema := &huma.Schema{
		Type: "object",
		Properties: map[string]*huma.Schema{
			"id":         {Type: "integer"},
			"email":      {Type: "string", Format: "email"},
			"first_name": {Type: "string"},
			"last_name":  {Type: "string"},
		},
	}

	oapi.AddOperation(&huma.Operation{
		OperationID: "auth-register",
		Method:      http.MethodPost,
		Path:        "/api/auth/register",
		Summary:     "Register a new user",
		Tags:        []string{"Auth"},
		RequestBody: &huma.RequestBody{
			Required: true,
			Content: map[string]*huma.MediaType{
				"application/json": {
					Schema: &huma.Schema{
						Type:     "object",
						Required: []string{"email", "password"},
						Properties: map[string]*huma.Schema{
							"email":      {Type: "string", Format: "email"},
							"password":   {Type: "string", MinLength: ptr(8)},
							"first_name": {Type: "string"},
							"last_name":  {Type: "string"},
						},
					},
				},
			},
		},
		Responses: map[string]*huma.Response{
			"201": {
				Description: "User created",
				Content:     map[string]*huma.MediaType{"application/json": {Schema: userSchema}},
			},
		},
	})

	oapi.AddOperation(&huma.Operation{
		OperationID: "auth-login",
		Method:      http.MethodPost,
		Path:        "/api/auth/login",
		Summary:     "Login and receive session cookie",
		Tags:        []string{"Auth"},
		RequestBody: &huma.RequestBody{
			Required: true,
			Content: map[string]*huma.MediaType{
				"application/json": {
					Schema: &huma.Schema{
						Type:     "object",
						Required: []string{"email", "password"},
						Properties: map[string]*huma.Schema{
							"email":    {Type: "string", Format: "email"},
							"password": {Type: "string"},
						},
					},
				},
			},
		},
		Responses: map[string]*huma.Response{
			"200": {Description: "Authenticated (sets session cookie)"},
			"401": {Description: "Invalid credentials"},
		},
	})

	oapi.AddOperation(&huma.Operation{
		OperationID: "auth-me",
		Method:      http.MethodGet,
		Path:        "/api/auth/me",
		Summary:     "Get current authenticated user",
		Tags:        []string{"Auth"},
		Security:    []map[string][]string{{"AppKey": {}, "SessionCookie": {}}},
		Responses: map[string]*huma.Response{
			"200": {
				Description: "Current user",
				Content:     map[string]*huma.MediaType{"application/json": {Schema: userSchema}},
			},
			"401": {Description: "Unauthorized"},
		},
	})

	oapi.AddOperation(&huma.Operation{
		OperationID: "auth-logout",
		Method:      http.MethodPost,
		Path:        "/api/auth/logout",
		Summary:     "Destroy current session",
		Tags:        []string{"Auth"},
		Security:    []map[string][]string{{"AppKey": {}, "SessionCookie": {}}},
		Responses: map[string]*huma.Response{
			"200": {Description: "Logged out"},
		},
	})
}

func ptr(v int) *int { return &v }

6.2 Documenting Task CRUD Endpoints (internal/services/task/docs.go) ​

For task, we register a live Huma health endpoint via huma.Register and document all 6 CRUD operations on /api/tasks*:

go
package task

import (
	"context"
	"net/http"

	"github.com/acme/taskforge/boot/server"
	"github.com/danielgtaylor/huma/v2"
	"github.com/danielgtaylor/huma/v2/adapters/humagin"
	"github.com/gin-gonic/gin"
)

type TaskDocs struct{}

func NewTaskDocs() *TaskDocs {
	return &TaskDocs{}
}

func (d *TaskDocs) Register(engine *gin.Engine) {
	cfg := server.NewHumaConfig("TaskForge Tasks API", "1.0.0", "/api/docs/task")
	cfg.Components.SecuritySchemes["SessionCookie"] = &huma.SecurityScheme{
		Type: "apiKey",
		In:   "cookie",
		Name: "limen_session",
	}

	api := humagin.New(engine, cfg)

	// Live Huma endpoint
	huma.Register(api, huma.Operation{
		OperationID: "task-health",
		Method:      http.MethodGet,
		Path:        "/api/task/health",
		Summary:     "Task service health check",
		Tags:        []string{"Health"},
	}, func(ctx context.Context, _ *struct{}) (*struct{ Body string }, error) {
		return &struct{ Body string }{Body: "OK"}, nil
	})

	oapi := api.OpenAPI()
	sec := []map[string][]string{{"AppKey": {}, "SessionCookie": {}}}
	idParam := []*huma.Param{{Name: "id", In: "path", Required: true, Schema: &huma.Schema{Type: "integer"}}}

	taskSchema := &huma.Schema{
		Type: "object",
		Properties: map[string]*huma.Schema{
			"id":          {Type: "integer"},
			"user_id":     {Type: "integer"},
			"title":       {Type: "string"},
			"description": {Type: "string"},
			"priority":    {Type: "string", Enum: []any{"low", "medium", "high"}},
			"status":      {Type: "string", Enum: []any{"todo", "in_progress", "done"}},
			"due_date":    {Type: "string", Format: "date-time"},
		},
	}

	oapi.AddOperation(&huma.Operation{
		OperationID: "list-tasks",
		Method:      http.MethodGet,
		Path:        "/api/tasks",
		Summary:     "List current user's tasks",
		Tags:        []string{"Tasks"},
		Security:    sec,
		Parameters: []*huma.Param{
			{Name: "status", In: "query", Schema: &huma.Schema{Type: "string"}},
			{Name: "priority", In: "query", Schema: &huma.Schema{Type: "string"}},
			{Name: "limit", In: "query", Schema: &huma.Schema{Type: "integer"}},
			{Name: "offset", In: "query", Schema: &huma.Schema{Type: "integer"}},
		},
		Responses: map[string]*huma.Response{
			"200": {
				Description: "Task list",
				Content: map[string]*huma.MediaType{
					"application/json": {
						Schema: &huma.Schema{
							Type:       "object",
							Properties: map[string]*huma.Schema{"data": {Type: "array", Items: taskSchema}},
						},
					},
				},
			},
			"401": {Description: "Unauthorized"},
		},
	})

	oapi.AddOperation(&huma.Operation{
		OperationID: "create-task",
		Method:      http.MethodPost,
		Path:        "/api/tasks",
		Summary:     "Create a new task",
		Tags:        []string{"Tasks"},
		Security:    sec,
		RequestBody: &huma.RequestBody{
			Required: true,
			Content: map[string]*huma.MediaType{
				"application/json": {
					Schema: &huma.Schema{
						Type:     "object",
						Required: []string{"title"},
						Properties: map[string]*huma.Schema{
							"title":       {Type: "string"},
							"description": {Type: "string"},
							"priority":    {Type: "string", Enum: []any{"low", "medium", "high"}},
							"due_date":    {Type: "string", Format: "date-time"},
						},
					},
				},
			},
		},
		Responses: map[string]*huma.Response{
			"201": {Description: "Created"},
			"422": {Description: "Validation failed"},
		},
	})

	oapi.AddOperation(&huma.Operation{
		OperationID: "get-task",
		Method:      http.MethodGet,
		Path:        "/api/tasks/{id}",
		Summary:     "Get a task by ID",
		Tags:        []string{"Tasks"},
		Security:    sec,
		Parameters:  idParam,
		Responses: map[string]*huma.Response{
			"200": {Description: "Task details"},
			"404": {Description: "Task not found"},
		},
	})

	oapi.AddOperation(&huma.Operation{
		OperationID: "update-task",
		Method:      http.MethodPut,
		Path:        "/api/tasks/{id}",
		Summary:     "Update a task",
		Tags:        []string{"Tasks"},
		Security:    sec,
		Parameters:  idParam,
		Responses: map[string]*huma.Response{
			"200": {Description: "Updated"},
			"404": {Description: "Task not found"},
			"422": {Description: "Validation failed"},
		},
	})

	oapi.AddOperation(&huma.Operation{
		OperationID: "complete-task",
		Method:      http.MethodPatch,
		Path:        "/api/tasks/{id}/complete",
		Summary:     "Mark task as done",
		Tags:        []string{"Tasks"},
		Security:    sec,
		Parameters:  idParam,
		Responses: map[string]*huma.Response{
			"200": {Description: "Completed"},
			"404": {Description: "Task not found"},
		},
	})

	oapi.AddOperation(&huma.Operation{
		OperationID: "delete-task",
		Method:      http.MethodDelete,
		Path:        "/api/tasks/{id}",
		Summary:     "Delete a task",
		Tags:        []string{"Tasks"},
		Security:    sec,
		Parameters:  idParam,
		Responses: map[string]*huma.Response{
			"204": {Description: "Deleted"},
			"404": {Description: "Task not found"},
		},
	})
}

6.3 Accessing Docs & APP_DEBUG Security Gating ​

Start the server (goforge app serve --no-tui) and visit:

  • Auth Docs UI: http://127.0.0.1:3000/api/docs/auth (.../auth.json)
  • Task Docs UI: http://127.0.0.1:3000/api/docs/task (.../task.json)

Production Visibility (APP_DEBUG)

In boot/server/middleware/auth.go, /api/docs*, /openapi*, and /docs* bypass X-App-Key only when APP_DEBUG=true. In production (APP_DEBUG=false), docs endpoints automatically require X-App-Key.


Next: 7. gRPC Service & Typed Clients →

Released under the Apache-2.0 License.