gobstopper

package module
v0.0.0-...-8326e81 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 9, 2016 License: MIT Imports: 9 Imported by: 0

README

gobstopper

An idiomatic framework for creating web stacks in Golang.

Build Status

version 3.0

Goals

  • Well chosen stack of core components
  • Exposed core components for ultimate flexibility
  • Convenience functions for adding routes, middleware and route groups for those looking to get going fast
  • DB connection magically available in all handlers and middleware

Usage

Basic
conf := gobstopper.Config{
  Port: 8000,
  PathPrefix: "/v1",
}

server, err := gobstopper.NewServer(conf)
if err != nil {
  log.Fatal("Unable to start server!")
}


server.Route(gobstopper.Route{
  Method: "GET",
  Path:   "/foo",
  Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "hello")
  }),
})

server.Start()
Route Groups

server.RouteGroup("/admin", func(group *gobstopper.Group) {
  group.Middleware(middleware.TerribleAuthMiddleware)
  group.Route(gobstopper.Route{
    Method: "GET",
    Path:   "",
    Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
      fmt.Fprintln(w, "admin")
    }),
  })
})


Config

For full details of the server configuration.

Database Connection

Gobstopper will make your sqlx DB connection pool available to you in all your middleware and handlers.

// Route handler...
func ServeHTTP(w http.ResponseWriter, r *http.Request) {
	db := database.GetConnection(r)
  // You can use it from here on...
}

Under The Hood

mux - Router

The name mux stands for "HTTP request multiplexer". Like the standard http.ServeMux, mux.Router matches incoming requests against a list of registered routes and calls a handler for the route that matches the URL or other conditions.

negroni - Middleware Manager

Negroni is an idiomatic approach to web middleware in Go. It is tiny, non-intrusive, and encourages use of net/http Handlers.

sqlx - Database (optional)

sqlx is a library which provides a set of extensions on go's standard database/sql library. The sqlx versions of sql.DB, sql.TX, sql.Stmt, et al. all leave the underlying interfaces untouched, so that their interfaces are a superset on the standard ones.

Contributing

See an issue? Submit a ticket.

This project aims to have unit tests with 100% code coverage. All PRs should have full tests included with them. To run the tests...

go test -v ./...

Documentation

Index

Constants

View Source
const DatabaseKey = "database"

DatabaseKey is the key used to set the DB connection in the request context.

Variables

This section is empty.

Functions

func ConnectToSQL

func ConnectToSQL(conn string) (*sqlx.DB, error)

ConnectToSQL is a function that will create a DB connection to your desired DB. Currently only the mysql driver is supported.

func GetConnection

func GetConnection(r *http.Request) *sqlx.DB

GetConnection is used to get the *sqlx.DB connection from the context of a http request object. This only works if the connection has been set in the request context.

func GetDatabaseName

func GetDatabaseName(db *sqlx.DB) (string, error)

GetDatabaseName will retrieve the database name for the currently connected database

func SetConnectionInRequestContext

func SetConnectionInRequestContext(r *http.Request, db *sqlx.DB)

SetConnectionInRequestContext sets a *sqlx.DB connection in the context of the request using gorilla/context.

Types

type Config

type Config struct {
	Port            int              `REQUIRED`
	PathPrefix      string           `OPTIONAL`
	Connection      string           `OPTIONAL`
	Negroni         *negroni.Negroni `OPTIONAL: DEFAULT TO CLASSIC`
	NotFoundHandler http.Handler     `OPTIONAL: DEFAULT TO BUILT IN MUX`
}

type Group

type Group struct {
	PathPrefix string
	Negroni    *negroni.Negroni
	Router     *mux.Router
}

func (*Group) Middleware

func (g *Group) Middleware(ms ...negroni.Handler)

func (*Group) Route

func (g *Group) Route(r Route)

type Route

type Route struct {
	Method  string
	Path    string
	Handler http.Handler
}

type Server

type Server struct {
	*Config
	Router *mux.Router
	DB     *sqlx.DB
}

func NewServer

func NewServer(conf Config) (*Server, error)

func (*Server) DBConnectionMiddleware

func (s *Server) DBConnectionMiddleware(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)

func (*Server) Middleware

func (s *Server) Middleware(ms ...negroni.Handler)

func (*Server) Route

func (s *Server) Route(r Route)

func (*Server) RouteGroup

func (s *Server) RouteGroup(path string, routing func(*Group))

func (*Server) Start

func (s *Server) Start()

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL