Documentation
¶
Overview ¶
Package apiserver demonstrates how to build an HTTP API server on top of component-base's generic server (pkg/generic_server/server.GenericAPIServer).
It mirrors the structure of the gjallarhorn apiserver, organized as the same five layers used across the framework:
Transport/Controller layer -> controller.go (gin handlers) Endpoint layer -> service methods invoked by controllers Middleware layer -> middleware.go (wraps the service factory) Service layer -> service.go (business logic) Store layer -> store.go (in-memory persistence)
The generic server drives the lifecycle through server.APIServerBizHooks (implemented in hooks.go):
InitStore -> InitService -> RegisteredMiddlewares -> InitController -> Startup -> Shutdown
Configuration is assembled from options exactly like gjallarhorn's config.CreateConfigFromOptions (see server.go):
Options -> Validate/Complete -> ApplyTo -> APIServerConfig -> Complete -> NewAPIServer
Running the server ¶
The package exposes Run(opts) as the production entrypoint. A minimal main:
package main
import (
"log"
"github.com/ClessLi/component-base/examples/apiserver"
)
func main() {
if err := apiserver.Run(apiserver.NewOptions()); err != nil {
log.Fatal(err)
}
}
Run blocks until a shutdown signal (SIGINT/SIGTERM) triggers graceful shutdown.
Trying the endpoints ¶
curl http://127.0.0.1:8080/healthz
curl -X POST http://127.0.0.1:8080/v1/users -d '{"name":"alice"}'
curl http://127.0.0.1:8080/v1/users/1
curl http://127.0.0.1:8080/v1/users
Testing ¶
The accompanying test boots the server via PrepareRun and exercises the gin router with httptest (no network port required):
go test ./examples/apiserver/...
Index ¶
- func CreateConfigFromOptions(opts *Options) (*genericserver.APIServerConfig[ServiceFactory, StoreFactory], error)
- func GetMiddlewares() httpmwv1.Middlewares[ServiceFactory]
- func NewBizHooks() *bizHooks
- func NewServer(ctx context.Context, ...) *genericserver.GenericAPIServer[ServiceFactory, StoreFactory]
- func Run(opts *Options) error
- type Options
- type ServiceFactory
- type StoreFactory
- type UserController
- type UserService
- type UserStore
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CreateConfigFromOptions ¶
func CreateConfigFromOptions(opts *Options) (*genericserver.APIServerConfig[ServiceFactory, StoreFactory], error)
CreateConfigFromOptions builds an APIServerConfig from Options, mirroring gjallarhorn's config.CreateConfigFromOptions: validate, complete, then apply.
func GetMiddlewares ¶
func GetMiddlewares() httpmwv1.Middlewares[ServiceFactory]
GetMiddlewares returns the named middleware registry for the service factory. The generic server installs the middlewares listed in APIServerConfig.Middlewares by looking them up here, wrapping the service factory in order.
func NewBizHooks ¶
func NewBizHooks() *bizHooks
NewBizHooks creates the business lifecycle hooks for the example server.
func NewServer ¶
func NewServer( ctx context.Context, cfg *genericserver.APIServerConfig[ServiceFactory, StoreFactory], ) *genericserver.GenericAPIServer[ServiceFactory, StoreFactory]
NewServer creates the generic API server from config with the example hooks.
Types ¶
type Options ¶
type Options struct {
APIServerRunOptions *genericoptions.APIServerRunOptions[ServiceFactory, StoreFactory]
InsecureServingOptions *genericoptions.InsecureServingOptions[ServiceFactory, StoreFactory]
}
Options holds the options for running the example API server. It mirrors the generic options used by gjallarhorn, limited to what this example needs.
func NewOptions ¶
func NewOptions() *Options
NewOptions creates Options with sensible defaults for the example.
type ServiceFactory ¶
type ServiceFactory interface {
Users() UserService
}
ServiceFactory provides access to all service instances. It is the entry point of the service layer, mirroring gjallarhorn's service/v1.Factory.
func NewServiceFactory ¶
func NewServiceFactory(store StoreFactory) ServiceFactory
NewServiceFactory creates a ServiceFactory backed by the given store.
type StoreFactory ¶
type StoreFactory interface {
Users() UserStore
}
StoreFactory provides access to all store instances. It is the entry point of the store layer, mirroring gjallarhorn's store/v1.Factory.
func NewStoreFactory ¶
func NewStoreFactory() StoreFactory
NewStoreFactory creates an in-memory StoreFactory for demonstration.
type UserController ¶
type UserController struct {
// contains filtered or unexported fields
}
UserController handles HTTP requests for the user resource. It is the transport/controller layer: it binds requests, delegates to the service layer, and writes responses through core.WriteResponse.
func NewUserController ¶
func NewUserController(svc UserService) *UserController
NewUserController creates a UserController backed by the given service.
func (*UserController) Create ¶
func (ctl *UserController) Create(c *gin.Context)
Create handles POST /v1/users.
func (*UserController) Get ¶
func (ctl *UserController) Get(c *gin.Context)
Get handles GET /v1/users/:id.
func (*UserController) List ¶
func (ctl *UserController) List(c *gin.Context)
List handles GET /v1/users.
type UserService ¶
type UserService interface {
Get(ctx context.Context, id string) (*apiv1.User, error)
List(ctx context.Context) ([]*apiv1.User, error)
Create(ctx context.Context, name string) (*apiv1.User, error)
}
UserService defines the business operations for the user resource.
func NewUserService ¶
func NewUserService(store UserStore) UserService
NewUserService creates a UserService backed by the given UserStore.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
api
|
|
|
v1
Package v1 defines the API domain types shared by the example HTTP API server (examples/apiserver) and its client SDK example (examples/httpclient).
|
Package v1 defines the API domain types shared by the example HTTP API server (examples/apiserver) and its client SDK example (examples/httpclient). |