An example of using the Gin framework in Go to create a JSON api with most of the core functionality a JSON api needs.
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
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
- 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
- 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
- Client side query language - a la GraphQL/Falkor/Datomic