apiserver

package
v1.0.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 14 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

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

NewServer creates the generic API server from config with the example hooks.

func Run

func Run(opts *Options) error

Run builds the server from options and runs it until a shutdown signal arrives. It mirrors gjallarhorn's Run entrypoint: create config, then PrepareRun().Run().

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.

type UserStore

type UserStore interface {
	Get(id string) (*apiv1.User, error)
	List() ([]*apiv1.User, error)
	Create(name string) (*apiv1.User, error)
}

UserStore defines the storage operations for the user resource.

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL