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.Generator.

examples/gen.cue

package examples

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

// A runnable generator (@gen(<name>))
Generator: gen.Generator & {
	@gen(server)

	Outdir: "./output"
	// ModuleName: "hof.io/docs/example"

	// Needed because we are using the generator from within it's directory
	// Normally, users will not see or set this field
	ModuleName: ""

	// We write the details in a separate file 
	"Server": Server
}

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
Server: schema.Server & {
	GitRepo:  "hof.io/docs/example"
	GoModule: "hof.io/docs/example"

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

	Auth: apikey: true
	Prometheus: true

	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)
			"""
	}]

}

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