• Stars
    star
    427
  • Rank 98,225 (Top 2 %)
  • Language
    Go
  • License
    MIT License
  • Created about 9 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A complete REST API server written in golang / go

slumber Build Status Coverage Status

A complete example of a REST-ful API server in written in Go (golang).


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
    • Uses dependency-inversion principle (DI) to reduce complexity and manage package dependencies.
  • Not a framework
    • More like a project to quickly kick-start your own REST API server, customized to your own needs.
    • Easily extend the project with your own REST resources, in addition to the built-in users and sessions resources.
  • Each REST resource is a separate package
    • Modular approach
    • Separation between model, controller and data layers
    • Clear abstraction from server package
    • Take a look at built-in resources for examples: users and sessions
    • More example projects coming soon!
  • Batteries come included
    • API versioning using using Accept header, for e.g: Accept=application/json;version=1.0,*/*
    • Default resources for users and sessions
    • Access control using activity-based access control (ABAC)
    • Authentication and session management using JWT token
    • Context middleware using gorilla/context for per-request context
    • JSON response rendering using unrolled/render; extensible to XML or other formats for response
    • MongoDB middleware for database; extensible for other database drivers
  • Highly-testable code base
    • Unit-tested server; 100% code coverage
    • Easily test REST resources routes
    • Parallelizable test suite
    • Uses ginkgo for test framework; optional.

Changelog

See CHANGELOG.md for changes

Quick start

To run an instance of a server example:

# get go-package and put it in your go-workspace
go get github.com/sogko/slumber

# go to package root folder
cd $GOPATH/src/github.com/sogko/slumber

# 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 gopkg.in/tylerb/graceful.v1      # graceful server shutdown
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 --randomizeAllSpecs -p -nodes=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

Coveralls.io link: [https://coveralls.io/r/sogko/slumber]

To generate coverage profile

cd $GOPATH/src/github.com/sogko/slumber

# run test recursively and generate coverage data for each package
$GOPATH/bin/ginkgo -r -cover -p

# join coverage data for all packages into a single profile (for coveralls.io)
$GOPATH/bin/gover . slumber.coverprofile

To view coverage

go tool cover -html=$GOPATH/src/github.com/sogko/slumber/slumber.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 

Architecture

<img src="http://i.imgur.com/HwIhPz7.png"/ height="750"/>

TODO

  • Add open-source license (MIT)
  • 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
  • User documentation (create resources routes, ACL, tests, middlewares etc)

More Repositories

1

graphql-schema-language-cheat-sheet

GraphQL Shorthand Notation Cheat Sheet
1,328
star
2

gulp-recipes

gulp-recipes
JavaScript
399
star
3

golang-relay-starter-kit

Barebones starting point for a Relay application with Golang GraphQL server
Go
136
star
4

go-wordpress

Golang client library for WP-API (Wordpress REST API)
Go
126
star
5

todomvc-relay-go

React/Relay TodoMVC app, driven by a Golang GraphQL backend
JavaScript
66
star
6

node-osm-stream

Fast and flexible NodeJS-based streaming parser for OpenStreetMap (.osm) files powered by node-expat - @sogko
JavaScript
17
star
7

graphql-schemas

A collection of common and useful GraphQL Object schemas
15
star
8

slumber-books-api-example

An example REST-ful API server using slumber package written in golang
Go
11
star
9

gosg-graphql-go-demo

Demo and slides for GoSG Meetup #15 - May 2016
CSS
10
star
10

fossasia-2016-google-calendar

A quick and dirty little thing that takes FOSSASIA 2016 schedule and creates Google Calendars (and iCal .ics) out of it.
Go
9
star
11

data-gov-sg-graphql-go

A GraphQL endpoint for https://data.gov.sg real-time APIs (Go/Golang)
Go
8
star
12

arangojs-extended

Extended APIs for ArangoJS client library
JavaScript
6
star
13

slumber-users

slumber-users is a slumber-resource that implements slumber.IUser
Go
4
star
14

node-osm-tags-analysis

Analyse .osm tagged data using NodeJS and osmfilter
JavaScript
4
star
15

node-ckan-crawler

A simple and fast NodeJS based crawler for sites powered by CKAN http://ckan.org
JavaScript
3
star
16

react-starter

Pragmatic starter kit for React + ES6 + Babel + Webpack + Tape.
JavaScript
3
star
17

graphql-geojson-go

Go
2
star
18

pecker

Pecker is a modern assets management framework for your web application development.
JavaScript
2
star
19

humanable-json

JavaScript
1
star
20

generator-leaner

yoeman generator for LEANER stack (Lightweight & Leaner express-angular-node-requirejs full web application stack)
JavaScript
1
star
21

fossasia-2016-graphql-demo

Demo used to showcase GraphQL (on Go / Golang)
CSS
1
star
22

go-ratelimit

Golang package to ratelimit messages/connections per unit time
Go
1
star