simplapi

package module
v0.0.0-...-11d1d31 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2025 License: MIT Imports: 5 Imported by: 0

README ΒΆ

go-simpl/api - Simple yet powerful API framework for GO (with swagger spec generation)

Coverage

A simple, developer-friendly API framework for Go, featuring automatic Swagger (OpenAPI) spec generation.

πŸš€ Features

  • Intuitive handler definitions using Go structs
  • Automatic request parsing (JSON, form, multipart, etc.)
  • Zero-boilerplate route registration for all HTTP methods
  • Instant Swagger docs generated from your handlers

πŸ› οΈ Quick Start

Create a new app

import (
    "github.com/go-simpl/simplapi"

    _ "github.com/go-simpl/simplapi/pkg/framework/fiberframework"
)

func main() {
    app := simplapi.New("fiber")

    // Add routes

    app.ListenAndServe(":8000")
}

Swagger UI will be instantly available on http://localhost:8000/try and the swagger (OpenAPI) spec on http://localhost:8000/openapi.json

Adding routes

To add routes, call the appropriate methods on the app.

app.GET(path, handlers...)
app.POST(path, handlers...)
app.PUT(path, handlers...)
// and so on

path: string - path to handle

handlers: []interface{} - functions that handle the request

Handler

Handler function is the important building block that controls how the endpoints are processed and the specs are generated. The function should be of the following format:

func HandlerFunc(input InputType) (*OutputType1, *OutputType2, error) {
    // Handler code
}

The function must return one of the output types or error. The function must, at the least, return error.

Output type will be returned as JSON response by marshalling the struct.

Additionally, if the Output type defines GetStatusCode function, that will be used to set the status of the response.

If you provide multiple handler functions for a route, they are called one after another in the order you specify. Each handler is executed until one of them returns a non-nil output or an error. As soon as a handler returns a result (any non-nil output or error), the chain stops and no further handlers are called. If a handler returns only nil values, the next handler in the list will be executed.

InputType definition

Input for the API can be defined by struct field tags.

Query Params

type InputType struct {
    PageNum int `query:"page_num"`
    PageSize int `query:"page_size"`
}

Path Params

// Assuming path is `/books/:book_id`
type InputType struct {
    BookId string `path:"book_id"`
}

Headers

type InputType struct {
    ApiKey string `header:"X-API-Key"`
}

JSON Body

type InputType struct {
    Body struct {
        Title string `json:"title"`
        // ... JSON fields here
    } `body:"json"`
}

URL Encoded from

type InputType struct {
    Form struct {
        Email    string `form:"email"`
        Password string `form:"password"`
    } `body:"urlencoded"`
}

Multipart form

type InputType struct {
    Form struct {
        UploadedFile *multipart.FileHeader `form:"file"`
    } `body:"multipart"`
}

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

This section is empty.

Types ΒΆ

type App ΒΆ

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

func New ΒΆ

func New(opts ...AppOption) *App

func (*App) DELETE ΒΆ

func (s *App) DELETE(path string, handlers ...interface{}) *Endpoint

func (*App) GET ΒΆ

func (s *App) GET(path string, handlers ...interface{}) *Endpoint

func (*App) GetApp ΒΆ

func (s *App) GetApp() framework.Framework

func (*App) ListenAndServe ΒΆ

func (s *App) ListenAndServe(addr string) error

func (*App) PATCH ΒΆ

func (s *App) PATCH(path string, handlers ...interface{}) *Endpoint

func (*App) POST ΒΆ

func (s *App) POST(path string, handlers ...interface{}) *Endpoint

func (*App) PUT ΒΆ

func (s *App) PUT(path string, handlers ...interface{}) *Endpoint

func (*App) Sync ΒΆ

func (s *App) Sync()

type AppConfig ΒΆ

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

type AppOption ΒΆ

type AppOption func(*AppConfig)

func WithAutoOpenAPISpec ΒΆ

func WithAutoOpenAPISpec(basePkgName string) AppOption

func WithCreateFramework ΒΆ

func WithCreateFramework(frameworkName string) AppOption

func WithFramework ΒΆ

func WithFramework(framework framework.Framework) AppOption

type Endpoint ΒΆ

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

func (*Endpoint) WithDescription ΒΆ

func (e *Endpoint) WithDescription(description string) *Endpoint

func (*Endpoint) WithOperationId ΒΆ

func (e *Endpoint) WithOperationId(operationId string) *Endpoint

func (*Endpoint) WithSummary ΒΆ

func (e *Endpoint) WithSummary(summary string) *Endpoint

func (*Endpoint) WithTag ΒΆ

func (e *Endpoint) WithTag(tag string) *Endpoint

func (*Endpoint) WithoutSpec ΒΆ

func (e *Endpoint) WithoutSpec() *Endpoint

type Q ΒΆ

type Q[T any] struct {
	Value T
}

Directories ΒΆ

Path Synopsis
pkg
tests

Jump to

Keyboard shortcuts

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