Skip to content

Repository files navigation

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"
)
funcmain() {
app:=simplapi.New("fiber")
// Add routesapp.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:

funcHandlerFunc(inputInputType) (*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

typeInputTypestruct {
PageNumint`query:"page_num"`PageSizeint`query:"page_size"`
}

Path Params

// Assuming path is `/books/:book_id`typeInputTypestruct {
BookIdstring`path:"book_id"`
}

Headers

typeInputTypestruct {
ApiKeystring`header:"X-API-Key"`
}

JSON Body

typeInputTypestruct {
Bodystruct {
Titlestring`json:"title"`// ... JSON fields here
} `body:"json"`
}

URL Encoded from

typeInputTypestruct {
Formstruct {
Emailstring`form:"email"`Passwordstring`form:"password"`
} `body:"urlencoded"`
}

Multipart form

typeInputTypestruct {
Formstruct {
UploadedFile*multipart.FileHeader`form:"file"`
} `body:"multipart"`
}

About

No description, website, or topics provided.

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages