Giter VIP home page Giter VIP logo

gin-api-demo's Introduction

Gin API Demo

An example of using the Gin framework in Go to create a JSON api with most of the core functionality a JSON api needs.

Installation

Install Glide

curl https://glide.sh/get | sh
# or, on OSX
brew install glide

Check out the project and install it's dependencies

$ go get github.com/sbecker/gin-api-demo
$ cd $GOPATH/src/github.com/sbecker/gin-api-demo/
$ glide install

How to run

In one terminal, run the server:

$ cd $GOPATH/src/github.com/sbecker/gin-api-demo
$ go run main.go

In another terminal, try making requests:

# Get all users as a non-admin - see only one user, with a subset of json attributes
$ curl -H 'Authorization: Bearer userA' -i http://localhost:8080/users
# Get all users as an admin - see all users, with all json attributes
$ curl -H 'Authorization: Bearer userB' -i http://localhost:8080/users
# Try to get all users as a non-user - get 401 Unauthorized
$ curl -i http://localhost:8080/users
# Get a single user as a non-admin - see a subset of json attributes
$ curl -H 'Authorization: Bearer userA' -i http://localhost:8080/users/1
# Get a single user as an admin - see a subset of json attributes
$ curl -H 'Authorization: Bearer userB' -i http://localhost:8080/users/1
# Try to get a different user than self - get 404 Not Found
$ curl -H 'Authorization: Bearer userA' -i http://localhost:8080/users/2
# Try to get a single user as a non-user, get 401 Unauthorized
$ curl -i http://localhost:8080/users/2

Done:

  • basic restful routing - GET /users, GET /users/:id
  • token authorization middleware
  • multi-level json serialization - display subset of json objects for non-admin users
  • separation of concerns - models, dao, resources, serializers, authorization
  • Glide for dependency management
  • UUIDs instead of integers for primary keys
  • Request-Id headers: Each API request has an associated request identifier
  • Cross-origin-request headers
  • Continuous integration
  • JSON logging
  • Env var configuration

Todo:

  • JWT token authorization
  • Pagination
  • Versioning
  • Standardize error responses
  • Integration with a real database
  • Swagger live documentation
  • Godoc
  • A test suite
  • Database migrations
  • Continuous deployment
  • Command line interface - run server, run migrations, start db console, etc
  • CLI with commands for migration, db drop/create/reset

Research:

  • Client side query language - a la GraphQL/Falkor/Datomic

gin-api-demo's People

Contributors

sbecker avatar

Watchers

 avatar  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.