Quickstart
Build your first gonest module in a few steps
Define a provider
A Provider builds a single instance for the DI graph. Constructor accepts
only func(), func() (T, error), func(ctx) T, or func(ctx) (T, error) —
never a function with dependency parameters. Dependencies are pulled in with
MustInject inside the builder, before Constructor runs.
type UserService struct{}
func (s *UserService) List() []string { return []string{"Ada", "Grace"} }
var UserProvider = gonest.NewProvider(func(provider *gonest.Provider) {
provider.Constructor(func() *UserService { return &UserService{} })
})Define a controller
A Controller groups routes under a path prefix. Resolve a dependency once
with MustInject, then reference it from any route handler in the same
builder.
var UserController = gonest.NewController(func(controller *gonest.Controller) {
controller.Path("/users")
userService := gonest.MustInject[*UserService](controller)
controller.RouteGet("/", func(route *gonest.Route) {
route.Handler(func(ctx *gonest.RestContext) {
ctx.Json(userService.List())
})
})
})Define a module
A Module wires providers and controllers together. It's the unit you pass to
MustNewApp.
var UserModule = gonest.NewModule(func(module *gonest.Module) {
module.Providers(UserProvider)
module.Controllers(UserController)
})Bootstrap the app
MustNewApp[gonest.FiberApp] runs the three-phase bootstrap (see
Bootstrap) and returns an app ready to
listen.
func main() {
app := gonest.MustNewApp[gonest.FiberApp](UserModule, gonest.AppOptions{})
app.MustListen(":3000")
}Full example
package main
import "github.com/gonest-dev/gonest"
type UserService struct{}
func (s *UserService) List() []string { return []string{"Ada", "Grace"} }
var UserProvider = gonest.NewProvider(func(provider *gonest.Provider) {
provider.Constructor(func() *UserService { return &UserService{} })
})
var UserController = gonest.NewController(func(controller *gonest.Controller) {
controller.Path("/users")
userService := gonest.MustInject[*UserService](controller)
controller.RouteGet("/", func(route *gonest.Route) {
route.Handler(func(ctx *gonest.RestContext) {
ctx.Json(userService.List())
})
})
})
var UserModule = gonest.NewModule(func(module *gonest.Module) {
module.Providers(UserProvider)
module.Controllers(UserController)
})
func main() {
app := gonest.MustNewApp[gonest.FiberApp](UserModule, gonest.AppOptions{})
app.MustListen(":3000")
}Two runnable examples ship in the gonest repo:
.examples/simple-todo
(minimal MVC, no external deps) and
.examples/blog-api
(guards, interceptors, middleware, filters, OpenAPI/Swagger, SQLite, a real 3-module domain).