solanum

package module
v0.4.6 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2025 License: MIT Imports: 8 Imported by: 2

README

Solanum — A Modular Gin-Based Framework with Built-In Dependency Injection

Solanum is designed to help Go developers ship clean, maintainable HTTP services and micro-APIs by combining:

  • A clear module/controller structure
  • First-class dependency injection (no global state, minimal reflection)
  • Pluggable middleware (CORS, logging, auth, etc.)
  • Testable, decoupled components

Why Solanum?

When you build a typical Gin application, it’s easy to end up with:

  • One giant main.go wiring routes, middleware, and handlers inline
  • Hard-to-test handlers that call global singletons or package-level variables
  • Tight coupling between controllers, services, and data stores
  • Boilerplate to register dependencies and then invoke them manually

Solanum solves these pain points by:

  1. Inversion of Control & DI container
    Register your services, repositories, and clients once. Solanum will resolve and inject them for you.
  2. Modular structure
    Group routes, middleware, and handlers into self-contained Modules. Each module declares its own dependencies.
  3. Minimal reflection, maximal clarity
    Define constructor functions with explicit parameters; Solanum uses simple reflection only to wire them up.
  4. Test-driven development
    Because your handlers request only the dependencies they need, you can swap in mocks or stubs without spinning up a server.

What Can You Build?

  • RESTful APIs
  • Microservices
  • GraphQL or gRPC gateways
  • CLI tools or batch workers (reuse DI container in non-web context)
  • Web Server for Applications; Health checks, admin dashboards, metrics endpoints, etc.

Whether you need a single /ping health check or a complex set of user, order, and payment services, Solanum keeps your code organized and your dependencies explicit.


Key Features

1. Modular Architecture
// Define a /users module
userModule := solanum.NewModule("/users")
userModule.SetControllers(myUserController)
userModule.SetDependencies(solanum.Dep[UserService]("userSvc"))
2. Controller & Service Layer
type SolaService struct {
    Uri     string
    Method  string
    Handler gin.HandlerFunc
}
3. Dependency Injection Container
solanum.Register("db", ProvideDB, solanum.WithSingleton())
solanum.Register(
  "userSvc",
  ProvideUserService,
  solanum.WithTransient(),
  solanum.As((*UserService)(nil)),
)
4. Flexible CORS & Middleware
app.Cors(
  solanum.WithOrigins("https://example.com"),
  solanum.WithMethods("GET", "POST"),
  solanum.WithAllowCredentials(true),
)

Getting Started

Get Solanum
go get github.com/annuums/solanum
Easy Start
package main

import "github.com/annuums/solanum"

func main() {

   pingModule := solanum.NewModule(
      solanum.WithUri("/ping"),
   )

   ctrl := solanum.NewController()
   ctrl.SetHandlers(
      &solanum.SolaService{
         Uri:    "",
         Method: http.MethodGet,
         Handler: func(c *gin.Context) {
            c.String(http.StatusOK, "pong")
         },
      },
   )
   pingModule.SetControllers(ctrl)

   server := solanum.NewSolanum(
      solanum.WithPort(5050),
   )

   server.SetModules(pingModule)
   server.Run()
}
Examples & More

👉 Learn by Examples

Who Is This For?

  • Go backend teams building REST or microservices
  • Developers who want clear separation between controllers, services, and data layers
  • Projects that require testable, maintainable code without manual wiring in every main.go
  • Engineers seeking a lightweight alternative to heavier frameworks with reflection-heavy DI

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithDependency added in v0.4.4

func WithDependency(dep *container.DependencyConfig) moduleOption

WithDependency registers a DependencyConfig with the module, but only if that key is actually registered in the global container and its type matches. Returns an error if the key is missing in the container or if the types don’t align.

func WithPort added in v0.4.4

func WithPort(port int) option

func WithUri added in v0.4.4

func WithUri(uri string) moduleOption

Types

type Controller

type Controller interface {
	// Handlers returns the slice of service definitions this controller manages.
	Handlers() []*SolaService

	// SetHandlers adds one or more SolaService entries to the controller.
	SetHandlers(handler ...*SolaService)
}

Controller declares a set of service handlers for a logical grouping of routes.

type Module

type Module interface {
	// PreMiddlewares returns middleware to run before each handler.
	PreMiddlewares() []gin.HandlerFunc

	// AddPreMiddleware appends a middleware to the pre-handler chain.
	AddPreMiddleware(middleware gin.HandlerFunc)

	// SetPreMiddlewares replaces the entire pre-handler middleware chain.
	SetPreMiddlewares(middlewares ...gin.HandlerFunc)

	// PostMiddlewares returns middleware to run after each handler.
	PostMiddlewares() []gin.HandlerFunc

	// AddPostMiddleware appends a middleware to the post-handler chain.
	AddPostMiddleware(middleware gin.HandlerFunc)

	// SetPostMiddlewares replaces the entire post-handler middleware chain.
	SetPostMiddlewares(middlewares ...gin.HandlerFunc)

	// Controllers returns the slice of Controller implementations in this module.
	Controllers() []Controller

	// SetControllers registers one or more Controller implementations.
	SetControllers(c ...Controller)

	// Dependencies returns the list of dependencies that this module injects.
	Dependencies() *[]*container.DependencyConfig

	// SetDependencies defines which dependencies to inject via middleware.
	SetDependencies(deps ...container.DependencyConfig)

	// SetRoutes binds the module's controllers, middleware, and routes onto a RouterGroup.
	SetRoutes(router *gin.RouterGroup)

	// Uri returns the base URI path for this module (e.g., "/users").
	Uri() string
}

Module represents a self-contained HTTP module with its own URI prefix, middleware layers, controllers, and dependencies.

type Runner

type Runner interface {
	// InitModules sets up all registered modules and their routes.
	InitModules()

	// InitGlobalMiddlewares registers any application-wide middleware.
	InitGlobalMiddlewares()

	// Modules returns a slice of pointers to registered Modules.
	Modules() []*Module

	// SetModules registers one or more Modules with the Runner.
	SetModules(m ...Module)

	// GinEngine exposes the underlying *gin.Engine for custom setup.
	GinEngine() *gin.Engine

	// Port exposes the configured port for the HTTP server.
	Port() int

	// Cors applies CORS configuration to the Gin engine using functional options.
	Cors(opts ...func(*util.CorsOption))

	// ValidateDependencies checks that all dependencies are registered.
	ValidateDependencies() error

	// Run boots the HTTP server, initializing modules and listening on the configured port.
	Run()
}

Runner is the application entrypoint interface for Solanum. It manages module initialization, global middlewares, CORS, and server start.

var SolanumRunner Runner

SolanumRunner holds the global Runner instance used to configure and start the server.

func NewSolanum

func NewSolanum(opts ...option) Runner

NewSolanum creates (once) and returns the global Runner configured for the given port. It ensures global middlewares are initialized. Subsequent calls return the same Runner.

type SolaController

type SolaController struct {
	// contains filtered or unexported fields
}

SolaController groups one or more SolaService handlers under a logical controller. It implements the Controller interface, managing a list of SolaService entries.

func NewController

func NewController() *SolaController

NewController constructs an empty SolaController ready to receive handlers.

func (*SolaController) Handlers

func (ctr *SolaController) Handlers() []*SolaService

Handlers returns the slice of SolaService entries managed by this controller.

func (*SolaController) SetHandlers

func (ctr *SolaController) SetHandlers(handlers ...*SolaService)

SetHandlers appends one or more SolaService entries to the controller's handler list.

type SolaModule

type SolaModule struct {
	// contains filtered or unexported fields
}

SolaModule encapsulates a self-contained HTTP module with its own URI prefix, controllers, middleware stacks, and dependency configurations.

func NewModule

func NewModule(opts ...moduleOption) *SolaModule

NewModule creates a new SolaModule with the given URI prefix. The module starts with empty controller, middleware, and dependency lists.

func (*SolaModule) AddPostMiddleware added in v0.3.2

func (m *SolaModule) AddPostMiddleware(middleware gin.HandlerFunc)

AddPostMiddleware appends a single middleware to the post-handler chain.

func (*SolaModule) AddPreMiddleware added in v0.3.2

func (m *SolaModule) AddPreMiddleware(middleware gin.HandlerFunc)

AddPreMiddleware appends a single middleware to the pre-handler chain.

func (*SolaModule) Controllers

func (m *SolaModule) Controllers() []Controller

Controllers returns all controllers registered in this module.

func (*SolaModule) Dependencies added in v0.4.0

func (m *SolaModule) Dependencies() *[]*container.DependencyConfig

Dependencies returns the list of DependencyConfig entries for this module.

func (*SolaModule) PostMiddlewares added in v0.3.0

func (m *SolaModule) PostMiddlewares() []gin.HandlerFunc

PostMiddlewares returns the list of middleware to execute after handlers.

func (*SolaModule) PreMiddlewares added in v0.3.0

func (m *SolaModule) PreMiddlewares() []gin.HandlerFunc

PreMiddlewares returns the list of middleware to execute before handlers.

func (*SolaModule) SetControllers

func (m *SolaModule) SetControllers(c ...Controller)

SetControllers registers one or more Controller implementations for this module.

func (*SolaModule) SetDependencies added in v0.4.0

func (m *SolaModule) SetDependencies(deps ...container.DependencyConfig)

SetDependencies replaces the module's dependency list with the provided configs.

func (*SolaModule) SetPostMiddlewares added in v0.4.0

func (m *SolaModule) SetPostMiddlewares(middlewares ...gin.HandlerFunc)

SetPostMiddlewares replaces the post-handler middleware chain with the provided list.

func (*SolaModule) SetPreMiddlewares added in v0.4.0

func (m *SolaModule) SetPreMiddlewares(middlewares ...gin.HandlerFunc)

SetPreMiddlewares replaces the pre-handler middleware chain with the provided list.

func (*SolaModule) SetRoutes

func (m *SolaModule) SetRoutes(router *gin.RouterGroup)

SetRoutes registers the module's routes, middleware, and DI middleware on the given RouterGroup. It applies DI if dependencies are defined, then mounts each SolaService handler with pre- and post-middleware.

func (*SolaModule) Uri

func (m *SolaModule) Uri() string

Uri returns the base URI prefix for this module.

type SolaService

type SolaService struct {
	// Uri the relative path for the service (e.g., "/:id")
	// route URI relative to module prefix
	Uri string

	// Method the HTTP method to bind (GET, POST, etc.)
	Method string

	// Handler the Gin handler function to execute
	Handler gin.HandlerFunc
}

SolaService represents a single HTTP route handler configuration.

Directories

Path Synopsis
docs
examples/simple command

Jump to

Keyboard shortcuts

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