Help us shape the future of Go! Take the 2026 Go Developer Survey

hclapi

package module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 18, 2026 License: MIT Imports: 8 Imported by: 0

README

hclapi

Go Reference Release CI

hclapi is a lightweight, declarative API runtime powered by HashiCorp HCL.

Define HTTP endpoints, input validation schemas, database queries, caching, sandboxed data transformations, real-time streams, and OpenAPI 3.1 specifications in human-readable manifests without writing boilerplate backend routing code.

Manifests are verified and precompiled at startup, then executed sequentially at request time.

Documentation · Quickstart · Examples

Example

server {
  host = "0.0.0.0"
  port = 8080
}

connection "sql" "main" {
  engine = "postgres"
  source = env("DATABASE_URL")
}

route "POST /users" {
  request {
    body {
      field "email" {
        type     = string
        format   = "email"
        required = true
      }

      field "name" {
        type     = string
        required = true
      }
    }
  }

  sql "create" {
    connection = "main"

    query = <<-SQL
      INSERT INTO users (email, name)
      VALUES (@email, @name)
      RETURNING id, email, name
    SQL

    args = {
      email = ctx.request.body.email
      name  = ctx.request.body.name
    }

    catch {
      code   = "23505"
      status = 409
      body   = problem(409, "Email is already registered")
    }
  }

  respond {
    status = 201
    body   = steps.create.row
  }
}

Go integration

hclapi is also distributed as an embeddable Go library:

package main

import (
 "log"
 "net/http"

 "github.com/ju4n97/hclapi"
)

func main() {
  manifest, err := hclapi.Load("routes/*.hcl")
  if err != nil {
    log.Fatal(err)
  }

  app, err := hclapi.New(manifest)
  if err != nil {
    log.Fatal(err)
  }
  defer app.Close()

  http.ListenAndServe(":8080", app)
}

Custom Go behavior can be registered with hclapi.WithStep. See the Go integration guide for details.

Install

Install the standalone CLI daemon via go install:

go install github.com/ju4n97/hclapi/cmd/hclapi@latest

Or use precompiled binary archives, Linux packages (.deb, .rpm, .apk, .pkg.tar.zst), and container images documented in the installation guide.

Documentation

See the full hclapi documentation.

Contributing

See CONTRIBUTING.md.

License

MIT

Documentation

Overview

Package hclapi provides a type-safe, declarative HTTP API engine powered by HashiCorp HCL. It compiles HCL manifests into instant HTTP services backed by relational databases, Valkey/Redis caching, sandboxed Starlark transformations, and zero-drift OpenAPI 3.1 specifications.

Index

Constants

This section is empty.

Variables

View Source
var ErrPipelineHalted = manifest.ErrPipelineHalted

ErrPipelineHalted signals that a terminal step or error catch completed the response.

Functions

This section is empty.

Types

type Args added in v0.1.2

type Args = manifest.Args

Args represents evaluated key-value arguments supplied to a native Go step handler.

type ByteSize

type ByteSize = manifest.ByteSize

ByteSize represents a quantity of bytes.

type Connection added in v0.1.2

type Connection = manifest.Connection

Connection defines external database connection pools and cache instances.

type Duration

type Duration = manifest.Duration

Duration wraps time.Duration to represent human-configured intervals.

type Engine

type Engine = engine.Engine

Engine coordinates HTTP routing, connection pool lifecycles, and request execution. It implements the standard http.Handler interface.

func New added in v0.1.3

func New(m *Manifest, opts ...Option) (*Engine, error)

New initializes an Engine from a validated Manifest, opening connection pools, configuring telemetry, precompiling OpenAPI specifications, and binding routes.

type Expr added in v0.2.1

type Expr = manifest.Expr

Expr represents a compiled HCL expression ready for evaluation against an execution scope.

func NewExpr added in v0.2.1

func NewExpr(raw hcl.Expression, funcs map[string]function.Function) Expr

NewExpr compiles an HCL expression and optional function registry into an Expr.

type Field added in v0.1.2

type Field = manifest.Field

Field specifies validation constraints and documentation metadata for a model attribute.

type GoHandler added in v0.2.1

type GoHandler = manifest.GoHandler

GoHandler defines the function signature for custom Go step callbacks registered on the runtime.

type GoRequest added in v0.2.1

type GoRequest = manifest.GoRequest

GoRequest encapsulates the input arguments and active HTTP request passed to a GoHandler.

type InvalidParam

type InvalidParam = problem.InvalidParam

InvalidParam describes a single schema constraint violation for 422 Unprocessable Entity responses.

type Manifest added in v0.2.1

type Manifest = manifest.Manifest

Manifest represents the compiled, validated AST for an entire API service.

func Load added in v0.2.0

func Load(patterns ...string) (*Manifest, error)

Load discovers, reads, merges, and compiles HCL files matching the supplied patterns into a validated Manifest using default built-in steps.

func Parse added in v0.2.0

func Parse(source string) (*Manifest, error)

Parse compiles an in-memory HCL manifest string into a validated Manifest using default built-in steps.

type OpenAPI added in v0.2.1

type OpenAPI = manifest.OpenAPI

OpenAPI holds document metadata exported into OpenAPI 3.1 specifications.

type Option added in v0.2.0

type Option = engine.Option

Option configures an Engine during initialization.

func WithGoHandler added in v0.2.1

func WithGoHandler(name string, h GoHandler) Option

WithGoHandler registers a custom native Go callback by its identifier.

func WithHTTPClient added in v0.2.1

func WithHTTPClient(client *http.Client) Option

WithHTTPClient overrides the default HTTP client used for outbound requests.

func WithStep added in v0.2.0

func WithStep(name string, h GoHandler) Option

WithStep is an alias for WithGoHandler.

func WithTelemetry added in v0.2.1

func WithTelemetry(t *TelemetryService) Option

WithTelemetry overrides the default telemetry instance configured by the manifest.

type Parser added in v0.2.1

type Parser = manifest.Parser

Parser coordinates file discovery, HCL parsing, and AST compilation using a configurable StepRegistry.

func NewParser added in v0.2.1

func NewParser(registry *StepRegistry) *Parser

NewParser constructs a Parser configured with the supplied StepRegistry.

type Problem added in v0.1.2

type Problem = problem.Problem

Problem represents an RFC 9457 compliant HTTP error payload.

type RecordSeq added in v0.2.1

type RecordSeq = manifest.RecordSeq

RecordSeq defines the standard Go iterator yielding record items and potential errors.

type Request added in v0.2.1

type Request = manifest.Request

Request defines validation constraints across incoming HTTP request coordinates.

type Route added in v0.2.1

type Route = manifest.Route

Route binds an HTTP method and URL pattern to an ordered pipeline of steps.

type Schema added in v0.1.2

type Schema = manifest.Schema

Schema represents a reusable domain model for payload validation and egress filtering.

type Server

type Server = manifest.Server

Server defines listener addresses, transport limits, and network timeouts.

type Spec added in v0.2.1

type Spec = openapi.Spec

Spec represents a compiled, validated OpenAPI 3.1 specification ready for serving.

func CompileSpec added in v0.2.1

func CompileSpec(m *Manifest) (*Spec, error)

CompileSpec builds a validated OpenAPI 3.1 specification from a compiled Manifest, serializing both JSON and YAML documents with calculated SHA-256 ETags.

type Step added in v0.1.2

type Step = manifest.Step

Step defines the minimal AST node contract for a route task.

type StepDecoder added in v0.2.1

type StepDecoder = manifest.StepDecoder

StepDecoder decodes an HCL block into a concrete Step implementation.

type StepExecutionContext added in v0.2.1

type StepExecutionContext = manifest.StepExecutionContext

StepExecutionContext supplies runtime dependencies and request state to steps during execution.

type StepExecutor added in v0.2.1

type StepExecutor = manifest.StepExecutor

StepExecutor is implemented by steps that execute runtime request logic.

type StepRegistry added in v0.2.1

type StepRegistry = manifest.StepRegistry

StepRegistry manages step decoders and generates dynamic HCL block schemas without global state.

func DefaultStepRegistry added in v0.2.1

func DefaultStepRegistry() *StepRegistry

DefaultStepRegistry returns a pre-configured registry containing all standard built-in steps (sql, http, valkey, starlark, go, stream, respond, docs, spec).

func NewStepRegistry added in v0.2.1

func NewStepRegistry() *StepRegistry

NewStepRegistry returns an empty, isolated step registry for custom language extensions.

type StepValidator added in v0.2.1

type StepValidator = manifest.StepValidator

StepValidator is an optional interface implemented by AST step definitions that require semantic verification against the compiled manifest topology.

type Telemetry added in v0.2.1

type Telemetry = manifest.Telemetry

Telemetry configures structured logging output, log level thresholds, and key redaction.

type TelemetryService added in v0.2.1

type TelemetryService = telemetry.Telemetry

TelemetryService coordinates runtime logging, OpenTelemetry tracing, and panic recovery middleware.

Directories

Path Synopsis
cmd
hclapi command
examples
05_go_embedded command
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL