fastapi

package module
v0.0.0-...-50b05b1 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

README

go-fastapi

Go Reference Go Report Card

go-fastapi is a library to quickly build APIs. It is inspired by Python's popular FastAPI library.

Features:

  • Auto-generated OpenAPI/Swagger schema without any markup
  • Declare handlers using types, not just Context
  • Based on gin framework

Installation: go get github.com/sashabaranov/go-fastapi

Example

package main

import (
	"github.com/gin-gonic/gin"
	"github.com/sashabaranov/go-fastapi"
)

type EchoInput struct {
	Phrase string `json:"phrase"`
}

type EchoOutput struct {
	OriginalInput EchoInput `json:"original_input"`
}

func EchoHandler(ctx *gin.Context, in EchoInput) (out EchoOutput, err error) {
	out.OriginalInput = in
	return
}

func main() {
	r := gin.Default()

	myRouter := fastapi.NewRouter()
	myRouter.AddCall("/echo", EchoHandler)

	r.POST("/api/*path", myRouter.GinHandler) // must have *path parameter
	r.Run()
}

// Try it:
//     $ curl -H "Content-Type: application/json" -X POST --data '{"phrase": "hello"}' localhost:8080/api/echo
//     {"response":{"original_input":{"phrase":"hello"}}}

To generate OpenAPI/Swagger schema:

myRouter := fastapi.NewRouter()
myRouter.AddCall("/echo", EchoHandler)

swagger := myRouter.EmitOpenAPIDefinition()
swagger.Info.Title = "My awesome API"
jsonBytes, _ := json.MarshalIndent(swagger, "", "    ")
fmt.Println(string(jsonBytes))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Router

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

func NewRouter

func NewRouter() *Router

func (*Router) AddCall

func (r *Router) AddCall(path string, handler interface{})

func (*Router) EmitOpenAPIDefinition

func (r *Router) EmitOpenAPIDefinition() openapi.Swagger

func (*Router) GinHandler

func (r *Router) GinHandler(c *gin.Context)

Jump to

Keyboard shortcuts

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