Giter Club home page Giter Club logo

golang-rest-api-server-example's Introduction

golang-rest-api-server-example

Build Status

A complete example of a REST-ful API server in Go

Features

  • Simple, flexible and testable architecture -- Light-weight server component -- Easy to replace components with your own library of choice (router, database driver, etc) -- Guaranteed thread-safety for each request: uses gorilla/context for per-request context
  • Not a framework -- More like a project to quickly kick-start your own REST API server, customized to your own needs.
  • Each REST resource is a separate package -- Modular approach -- Separation between model, controller and data layers -- Clear abstraction from server package
  • Highly-testable code base -- Unit-tested server; 100% code coverage -- Easily test REST resources routes -- Parallelizable test suite

Quick start

# get go-package and put it in your go-workspace
go get github.com/sogko/golang-rest-api-server-example

# go to package root folder
cd $GOPATH/src/github.com/sogko/golang-rest-api-server-example

# install dependencies
go get

# run Server
go run main.go

# http://localhost:3001

Dependencies

  • Golang v1.4+
  • MongoDB
  • External Go packages dependencies
# production
go get github.com/codegangsta/negroni   # HTTP server library
go get github.com/gorilla/mux           # HTTP router
go get github.com/gorilla/context       # Per-request context registry utility
go get github.com/unrolled/render       # JSON response renderer
go get gopkg.in/mgo.v2                  # Golang MongoDB driver

# development / test
go get github.com/onsi/ginkgo           # Golang BDD test framework, complements `go test`
go get github.com/onsi/gomega           # Ginkgo's preferred matcher library
go get github.com/modocache/gory        # `factory_girl` for Go

Test

Install all Go package dependencies and run either one of the following command

go test

# or

$GOPATH/bin/ginkgo -r -p -node=4

# "-r" watches recursively (including test suites for sub-packages)
# "-p -nodes=4" parallelize test execution with 4 worker nodes

TDD

$GOPATH/bin/ginkgo watch -r -p -nodes=4

# "-r" watches recursively (including test suites for sub-packages)
# "-p -nodes=4" parallelize test execution with 4 worker nodes

Code coverage

To generate coverage profile

cd $GOPATH/src/github.com/sogko/golang-rest-api-server-example/server
$GOPATH/bin/ginkgo -cover

To view coverage

go tool cover -html=$GOPATH/src/github.com/sogko/golang-rest-api-server-example/server/server.coverprofile

Sessions Management

To generate key pair for signing JWT claims

$ openssl genrsa -out demo.rsa 1024 # the 1024 is the size of the key we are generating
$ openssl rsa -in demo.rsa -pubout > demo.rsa.pub 

TODO

  • API versioning using Accept header, for e.g: Accept=application/json;version=1.0,*/*
  • User and roles management
  • Session management using JWT tokens
  • Activity-based access control (ABAC)
  • Refactor using Dependency Inversion
  • Task scheduler
  • Load test using vegeta
  • i18n (internationalisation)
  • Implement another router library for kicks
  • Consolidate util libraries and publish as separate package
  • Abstract away negroni middlewares to a generic http.HandlerFunc
  • Create a REST API server project using this package as a boilerplate without changing this package

golang-rest-api-server-example's People

Contributors

sogko avatar

Stargazers

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