All Projects → mikkeloscar → gin-swagger

mikkeloscar / gin-swagger

Licence: Apache-2.0 License
DRY templates for go-swagger

Programming Languages

go
31211 projects - #10 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to gin-swagger

go-gin-logrus
Gin Web Framework for using Logrus as the Gin logger with Tracing middleware
Stars: ✭ 38 (-51.9%)
Mutual labels:  gin, opentracing, gin-gonic
oas2
OpenAPI 2.0 (aka Swagger) utils for Golang.
Stars: ✭ 19 (-75.95%)
Mutual labels:  swagger, openapi, go-swagger
tssg-syntax-parser
Parser to generate AST from TSSG Syntax
Stars: ✭ 17 (-78.48%)
Mutual labels:  swagger, openapi
shipengine-openapi
The official OpenAPI 3.0 definitions for ShipEngine™
Stars: ✭ 13 (-83.54%)
Mutual labels:  swagger, openapi
openapi-generator-for-spring
Open API v3 Generator for Spring Boot applications
Stars: ✭ 54 (-31.65%)
Mutual labels:  swagger, openapi
go-12factor-example
Example the 12factor app using golang
Stars: ✭ 20 (-74.68%)
Mutual labels:  gin, gin-gonic
fhir-fuel.github.io
Place to prepare proposal to FHIR about JSON, JSON-Schema, Swagger/OpenAPI, JSON native databases and other JSON-frendly formats (yaml, edn, avro, protobuf etc) and technologies
Stars: ✭ 20 (-74.68%)
Mutual labels:  swagger, openapi
fastify-openapi-glue
A plugin for Fastify to autogenerate a configuration based on a OpenApi(v2/v3) specification.
Stars: ✭ 94 (+18.99%)
Mutual labels:  swagger, openapi
logger
Gin middleware/handler to logger url path using rs/zerolog
Stars: ✭ 119 (+50.63%)
Mutual labels:  gin, gin-gonic
go-gin-web-server
Deploy Go Gin on Render
Stars: ✭ 23 (-70.89%)
Mutual labels:  gin, gin-gonic
openapi-schema-validator
OpenAPI schema validator for Python
Stars: ✭ 35 (-55.7%)
Mutual labels:  swagger, openapi
openapi-schemas
JSON Schemas for every version of the OpenAPI Specification
Stars: ✭ 22 (-72.15%)
Mutual labels:  swagger, openapi
geo-smart-system
Open Source Realtime Tracking System
Stars: ✭ 36 (-54.43%)
Mutual labels:  gin, gin-gonic
website
Official website and document for Gin
Stars: ✭ 88 (+11.39%)
Mutual labels:  gin, gin-gonic
api
🚀 Automatic SDK generation from an OpenAPI definition
Stars: ✭ 127 (+60.76%)
Mutual labels:  swagger, openapi
go-fastapi
Create an API and get Swagger definition for free
Stars: ✭ 76 (-3.8%)
Mutual labels:  openapi, gin
gin-rest-api
Example golang using gin framework everything you need, i create this tutorial special for beginner.
Stars: ✭ 56 (-29.11%)
Mutual labels:  gin, gin-gonic
ginhelper
gin framework helper
Stars: ✭ 16 (-79.75%)
Mutual labels:  gin, gin-gonic
kompendium
Ktor OpenAPI Spec Generator
Stars: ✭ 46 (-41.77%)
Mutual labels:  swagger, openapi
Go-Gin-Api
基于golang开源框架 gin封装的api框架
Stars: ✭ 42 (-46.84%)
Mutual labels:  swagger, gin

gin-swagger - DRY templates for go-swagger

gin-swagger is a tool assisting in writing golang REST APIs based on a API First Principle. Given a swagger spec of your REST API, gin-swagger can generate all the boring boilerplate stuff for you, so you only have to implement the core business logic of the API.

It is based on go-swagger which is used for the code generation.

Install

You can simply run go get to install gin-swagger.

$ go get github.com/mikkeloscar/gin-swagger
$ gin-swagger --help
usage: gin-swagger --application=APPLICATION [<flags>]

Flags:
      --help                     Show context-sensitive help (also try --help-long and --help-man).
  -A, --application=APPLICATION  Name of the application (passed directly to swagger).
  -f, --spec="./swagger.json"    the spec file to use.

How it works

gin-swagger generates a gin based REST API given a swagger spec.

Assuming the following swagger spec:

swagger: '2.0'
info:
  version: "0.0.1"
  title: My API
schemes:
    - "https"
basePath: /
produces:
  - application/json
consumes:
  - application/json
paths:
  '/persons/{name}':
    get:
      summary: get Person
      tags:
        - Person
      operationId: getPerson
      responses:
        200:
          description: Person by name.
          schema:
            '$ref': '#/definitions/Person'
parameters:
  name:
    name: name
    in: path
    type: string
    required: true
    pattern: "^[a-z][a-z0-9]$"
definitions:
  Person:
    type: object
    properties:
      name:
        type: string

You can generate the REST API using the following command:

$ gin-swagger -A my-api -f swagger.yaml

This will generate two folders models/ and restapi/. models/ contains model structs based on the definitions defined in your swagger file, including model validation, this is generated by go-swagger. restapi/ will contain everything generated by gin-swagger and can be overwritten when updating and regenerating your swagger spec. restapi/api.go will contain a generated Service interface which is all you have to implement in order to add logic to your REST api.

Given the above swagger spec, the Service interface will look like this:

type Service interface {
    Healthy() bool
    GetPerson(ctx *gin.Context, params *persons.GetPersonParams) *api.Response
}

The Healthy() bool method should return true if the service is considered healthy. The return value is used by the default health endpoint /healthz provided by gin-swagger.

The GetPerson() method should implement the business logic of the /persons/{name} path of your REST API.

A simple service implementation looks like this:

type mySvc struct {
    health bool
}

func (m *mySvc) GetPerson(ctx *gin.Context, params *persons.GetPersonParams) *api.Response {
    return &api.Response{
        Code: http.StatusOK,
        Body: &models.Person{Name: params.Name},
    }
}

func (m *mySvc) Healthy() bool {
    return m.health
}

With the service implemented the only thing left to do is plug it into gin-swagger and run it from your main.go (or wherever you like):

package main

import ...

var apiConfig restapi.Config

func main() {
    err := apiConfig.Parse()
    if err != nil {
        log.Fatal(err)
    }
    svc := &mySvc{health: true}
    api := restapi.NewServer(svc, &apiConfig)
    err = api.RunWithSigHandler()
    if err != nil {
        log.Fatal(err)
    }
}

restapi.Config is a default config object defined by gin-swagger it lets you configure default options through commandline flags when starting the server. For instance you can tell the server to serve HTTP only with the --insecure-http flag (default is to serve HTTPS).

For a full example see the example folder.

Features

  • Validate + bind input to gin ctx.
    • bind body input.
      • [Nice to have] custom input Models with required fields.
    • bind params input.
    • bind query params input.
    • consume more than application/json
  • Security.
    • basic
    • apiKey
    • OAuth2
      • password (Bearer token)
      • accessCode
      • application
      • implicit
    • Auth chain
    • Custom authorize on individual routes.
  • Set custom middleware on each router Pre/post 'main' handler.
    • Use case pre: custom authorization pre handler.
    • Use case post: audit report events post handler.
  • Ginize generated code.
  • Set and get user info (uid, realm) from gin context.
  • Response helper functions
  • Default metrics (prometheus)
  • OpenTracing support
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].