Skip to content

Commit 2dfd927

Browse files
authored
chore(api): export and document for more clear interface (#2375)
* chore(api): export types for more clear interface * docs
1 parent 58b56c3 commit 2dfd927

File tree

4 files changed

+33
-22
lines changed

4 files changed

+33
-22
lines changed

api/api.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"github.com/sirupsen/logrus"
1414
)
1515

16+
// API represents the main HTTP API server for the Embedded Cluster application.
17+
//
1618
// @title Embedded Cluster API
1719
// @version 0.1
1820
// @description This is the API for the Embedded Cluster project.
@@ -31,7 +33,7 @@ import (
3133

3234
// @externalDocs.description OpenAPI
3335
// @externalDocs.url https://swagger.io/resources/open-api/
34-
type api struct {
36+
type API struct {
3537
cfg types.APIConfig
3638

3739
logger logrus.FieldLogger
@@ -44,40 +46,47 @@ type api struct {
4446
handlers handlers
4547
}
4648

47-
type apiOption func(*api)
49+
// Option is a function that configures the API.
50+
type Option func(*API)
4851

49-
func WithAuthController(authController auth.Controller) apiOption {
50-
return func(a *api) {
52+
// WithAuthController configures the auth controller for the API.
53+
func WithAuthController(authController auth.Controller) Option {
54+
return func(a *API) {
5155
a.authController = authController
5256
}
5357
}
5458

55-
func WithConsoleController(consoleController console.Controller) apiOption {
56-
return func(a *api) {
59+
// WithConsoleController configures the console controller for the API.
60+
func WithConsoleController(consoleController console.Controller) Option {
61+
return func(a *API) {
5762
a.consoleController = consoleController
5863
}
5964
}
6065

61-
func WithLinuxInstallController(linuxInstallController linuxinstall.Controller) apiOption {
62-
return func(a *api) {
66+
// WithLinuxInstallController configures the linux install controller for the API.
67+
func WithLinuxInstallController(linuxInstallController linuxinstall.Controller) Option {
68+
return func(a *API) {
6369
a.linuxInstallController = linuxInstallController
6470
}
6571
}
6672

67-
func WithLogger(logger logrus.FieldLogger) apiOption {
68-
return func(a *api) {
73+
// WithLogger configures the logger for the API. If not provided, a default logger will be created.
74+
func WithLogger(logger logrus.FieldLogger) Option {
75+
return func(a *API) {
6976
a.logger = logger
7077
}
7178
}
7279

73-
func WithMetricsReporter(metricsReporter metrics.ReporterInterface) apiOption {
74-
return func(a *api) {
80+
// WithMetricsReporter configures the metrics reporter for the API.
81+
func WithMetricsReporter(metricsReporter metrics.ReporterInterface) Option {
82+
return func(a *API) {
7583
a.metricsReporter = metricsReporter
7684
}
7785
}
7886

79-
func New(cfg types.APIConfig, opts ...apiOption) (*api, error) {
80-
api := &api{
87+
// New creates a new API instance.
88+
func New(cfg types.APIConfig, opts ...Option) (*API, error) {
89+
api := &API{
8190
cfg: cfg,
8291
}
8392

api/controllers/linux/install/hostpreflight.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ func (c *InstallController) RunHostPreflights(ctx context.Context, opts RunHostP
4343
IsUI: opts.IsUI,
4444
})
4545
if err != nil {
46-
return fmt.Errorf("failed to prepare host preflights: %w", err)
46+
return fmt.Errorf("prepare host preflights: %w", err)
4747
}
4848

4949
err = c.stateMachine.Transition(lock, StatePreflightsRunning)
5050
if err != nil {
51-
return fmt.Errorf("failed to transition states: %w", err)
51+
return fmt.Errorf("transition states: %w", err)
5252
}
5353

5454
go func() (finalErr error) {
@@ -78,7 +78,7 @@ func (c *InstallController) RunHostPreflights(ctx context.Context, opts RunHostP
7878
HostPreflightSpec: hpf,
7979
})
8080
if err != nil {
81-
return fmt.Errorf("failed to run host preflights: %w", err)
81+
return fmt.Errorf("run host preflights: %w", err)
8282
}
8383

8484
return nil

api/handlers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type handlers struct {
1616
linux *linuxhandler.Handler
1717
}
1818

19-
func (a *api) initHandlers() error {
19+
func (a *API) initHandlers() error {
2020
// Auth handler
2121
authHandler, err := authhandler.New(
2222
a.cfg.Password,

api/routes.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import (
88
httpSwagger "github.com/swaggo/http-swagger/v2"
99
)
1010

11-
func (a *api) RegisterRoutes(router *mux.Router) {
11+
// RegisterRoutes registers the routes for the API. A router is passed in to allow for the routes
12+
// to be registered on a subrouter.
13+
func (a *API) RegisterRoutes(router *mux.Router) {
1214
router.HandleFunc("/health", a.handlers.health.GetHealth).Methods("GET")
1315

1416
// Hack to fix issue
@@ -30,7 +32,7 @@ func (a *api) RegisterRoutes(router *mux.Router) {
3032
a.registerConsoleRoutes(authenticatedRouter)
3133
}
3234

33-
func (a *api) registerLinuxRoutes(router *mux.Router) {
35+
func (a *API) registerLinuxRoutes(router *mux.Router) {
3436
linuxRouter := router.PathPrefix("/linux").Subrouter()
3537

3638
installRouter := linuxRouter.PathPrefix("/install").Subrouter()
@@ -50,11 +52,11 @@ func (a *api) registerLinuxRoutes(router *mux.Router) {
5052
installRouter.HandleFunc("/status", a.handlers.linux.PostSetStatus).Methods("POST")
5153
}
5254

53-
func (a *api) registerKubernetesRoutes(router *mux.Router) {
55+
func (a *API) registerKubernetesRoutes(router *mux.Router) {
5456
// kubernetesRouter := router.PathPrefix("/kubernetes").Subrouter()
5557
}
5658

57-
func (a *api) registerConsoleRoutes(router *mux.Router) {
59+
func (a *API) registerConsoleRoutes(router *mux.Router) {
5860
consoleRouter := router.PathPrefix("/console").Subrouter()
5961
consoleRouter.HandleFunc("/available-network-interfaces", a.handlers.console.GetListAvailableNetworkInterfaces).Methods("GET")
6062
}

0 commit comments

Comments
 (0)