Skip to content

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

Notifications You must be signed in to change notification settings

sbecker/gin-api-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages