GithubHelp home page GithubHelp logo

glennneiger / go-rest-api-boilerplate Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jtbonhomme/go-rest-api-boilerplate

0.0 1.0 0.0 170 KB

Go production ready REST API starting kit, with Swagger documentation

License: MIT License

Go 100.00%

go-rest-api-boilerplate's Introduction

Go REST API Boilerplate

This repository is a skeleton for building production ready Golang REST API, with Swagger documentation.

We will use gorilla/mux and go-swagger packages.

Installation

Assuming you have a working Go environment and GOPATH/bin is in your PATH.

Dependencies

Install gorilla/mux:

$ go get github.com/gorilla/mux

Install CORS middleware rs/cors:

$ go get github.com/rs/cors

Live reload

gin is a simple command line utility for live-reloading Go web applications. Just run gin in your app directory and your web app will be served with gin as a proxy. gin will automatically recompile your code when it detects a change. Your app will be restarted the next time it receives an HTTP request.

gin adheres to the "silence is golden" principle, so it will only complain if there was a compiler error or if you succesfully compile after an error.

gin is a breeze to install:

$ go get github.com/codegangsta/gin

Then verify that gin was installed correctly:

$ gin -h

Start gin proxy to listen on port 3000 and send request to proxied app listening on port 8000:

$ cd cmd/app
$ gin run -p 3000 -a 8000 main.go

Then verify that gin act correctly as a proxy to the api:

$ curl -X GET "http://localhost:3000/v1/people" -H "accept: application/json"

Swagger

See the go-swagger page for full installation instructions.

go-swagger releases are distributed as binaries that are built from signed tags. It is published as github release, rpm, deb and docker image.

Debian

$ echo "deb https://dl.bintray.com/go-swagger/goswagger-debian ubuntu main" | sudo tee -a /etc/apt/sources.list

Homebrew

$ brew tap go-swagger/go-swagger
$ brew install go-swagger

Generate swagger

$ swagger generate spec -o swagger-api/v1/swagger.json && swagger validate swagger-api/v1/swagger.json

Serve swagger documentation

Swagger Flavor

$ swagger serve --flavor=swagger --port=6060 swagger-api/v1/swagger.json

This command will open your browser on the URL petstore.swagger.io (the official swagger live-demo exploration tool). This server allow you to explore any swagger json file.

You can host your own swagger-ui server too: https://swagger.io/docs/swagger-tools/#download-33

Redoc flavor

$ swagger serve --flavor=redoc --port=6060 swagger-api/v1/swagger.json

This command will open your browser on the URL localhost:6060/docs, no need of an internet connexion to use it.

REST API

Content of the package

├── main.go
├── router
│   ├── logger.go
│   ├── routes.go
│   └── router.go
├── db
│   └── people.go
├── handlers
│   ├── people.go
│   └── response.go
├── model
│   └── people.go
└── swagger-api
    └── v1
        └── swagger.json

main.go

router

handlers

db

models

swagger-api/v1

References

The following references helped me :

Todo

  • Error handling for any system call
  • Add correct Content-type headers
  • Handle correctly Accept headers
  • Handle concurrency
  • Add pagination for collections/models getters
  •  Http StatusCode handling with consistent http codes implementation
  • Versionning with version.go or local json file (pathPrefix)
  • Full swagger documentation
  • Aggregate multiple miroservices API in a single Swagger end point
  • Authentication
  • Handle eTags
  • Add environment variable such as listening port
  • Production ready logs
  • Unitary and system Tests
  • Rate limit
  • gRPC example/version

go-rest-api-boilerplate's People

Contributors

jtbonhomme avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.