Getting Started
entrest is an EntGo extension for generating compliant OpenAPI specs and an HTTP handler implementation that matches that spec. It expands upon the approach used by entoas, with additional functionality, and pairs the generated specification with a fully-functional HTTP handler implementation.
- ✨ Generates OpenAPI specs for your EntGo schema.
- ✨ Generates a fully functional HTTP handler implementation that matches the OpenAPI spec.
- ✨ Supports automatic pagination (where applicable).
- ✨ Supports advanced filtering (using query parameters,
AND
/OR
predicates, etc). - ✨ Supports eager-loading edges, so you don't have to make additional calls unnecessarily.
- ✨ Supports various forms of sorting.
- ✨ And more!
Project Structure
We recommend a project structure that looks similar to this:
Directoryinternal
- ...
Directorydatabase
Directoryent/ contains all entgo-generated code
- ...
Directoryrest/ HTTP handler, OpenAPI spec & entrest code
- …
Directoryschema/ entgo schemas (w/ entrest annotations)
- ...
- schema_foo.go
- schema_bar.go
- entc.go entc logic (where to load entrest)
- generate.go used to trigger all internal codegen logic, including pinning codegen dependencies.
- main.go main entrypoint for the application, mount HTTP handler, custom endpoints, etc
Why?
There are a few reasons why we recommend this structure:
- Generated code is isolated in its own folder. In most cases, you can simply ignore the folder in you editor, so it's less distracting.
- Schemas aren't buried compared to the standard entgo setup.
- All database-related code is explicitly in an
internal/
folder, which is best practice for logic that shouldn't be imported by external packages.
Future Growth
As the complexity of your project grows, it may be worth separating out logic even further. Some examples:
- HTTP logic --
main.go -> http.go
. - Initializing the DB client/migrations --
main.go -> internal/database/client.go
- Multiple "main" entrypoints (API backend, CLI, worker(s), etc) --
main.go -> cmd/http-server/main.go
Initialize Project
You can find a full example project that matches this guide here.
-
Start by initializing a new Go project:
-
Setup subfolder for schema files:
-
Add a few schema files:
Example
Pet
schema, which also has a few edge relationships, one of which we eager load. Take note of some of of the attached annotations:Example
User
schema, which also has a single edge relationship, which we eager load. Take note of some of of the attached annotations: -
Add an
internal/database/entc.go
file, which is used to configure the extension, and run code generation. Make sure to replace the github.com/example/my-project module references with your module name. -
Add a
internal/generate.go
file, where thego:generate
comment is used to trigger all internal codegen logic. This file is also used to pin codegen dependencies: -
Add a
main.go
file, which mounts the HTTP handler, and initializes the database client: -
Lastly, run code generation:
That's it! You should now have a fully-functional project that uses entrest. This includes:
- A managed database schema using EntGo.
- A fully-functional HTTP handler implementation using chi (or any
other HTTP router that supports
net/http
). - A fully-functional OpenAPI spec, which can be used to generate client libraries, or to validate
requests (see
internal/database/ent/rest/openapi.json
).
Running The Server
To run the application, simply run the following commands to download all dependencies, then run the application:
Next Steps
Now that you've got a basic HTTP API setup, you can start making API requests to it, or further customizing it.