Using



Users design by filling in the schema for a Hof Generator.

Server, @gen(server)

This is the block which defines an entrypoint to hof gen, using @gen(tags...) and unifying with our generator gen.#HofGenerator.

examples/gen.cue

package examples

import (
	"hof.io/docs/example/gen"
)

Server: gen.#Generator & {
	@gen(server)

	Outdir:   "./output"

	// We write the design in a separate file 
	Server: ServerDesign

	// Needed because we are using the generator from within it's directory
	// Users who import your generator as a module will not need to set this
	// It's a temporary requirement until CUE supports embedded files
	PackageName: ""
}

ServerDesign, with the schema

This is the user created design for our server generator.

examples/server.cue

package examples

import (
	"hof.io/docs/example/schema"
)

// A concrete value of the Server schem
ServerDesign: schema.#Server & {
	GoModule: "hof.io/docs/example"

	Name:        "Example"
	Description: "An example server"

	Routes: [{
		Name:   "EchoQ"
		Path:   "/echo"
		Method: "GET"
		Query: ["msg"]
		Body: """
			c.String(http.StatusOK, msg)
			"""
	}, {
		Name:   "EchoP"
		Path:   "/echo"
		Method: "GET"
		Params: ["msg"]
		Body: """
			c.String(http.StatusOK, msg)
			"""
	}, {
		Name:   "Hello"
		Path:   "/hello"
		Method: "GET"
		Query: ["msg"]
		Body: """
			if msg == "" {
				msg = "hello world"
			}
			c.String(http.StatusOK, msg)
			"""
	}]

	Prometheus: true
}

Generate the Code

From the root of our module, run

hof gen ./examples

You should now have an ./output directory with the generated code.

Running the Server

With our code in place, we can build and run the server

cd ./output
go mod tidy
go build -o server
./server

Call the endpoints with curl

curl localhost:8080/hello
curl localhost:8080/echo/moo
curl localhost:8080/internal/metrics
2023 Hofstadter, Inc