Giter Site home page Giter Site logo

goat's Introduction

Goat Web Framework

Goat is a small web framework for Go that makes the following assumptions about your application:

  • It is using a Mongodb database and it uses mgo as a driver
  • It has a User model that has a username and password

Goat wraps the stock Go net/http library to provide a productive API for building routes for your application. Routes can have universally configured middleware that will execute on each request, or an interceptor that will only fire on specific requests.

You can find detailed documentation here: http://godoc.org/github.com/scottferg/goat

Basic usage

Add Goat to your project by importing the package

    import "github.com/scottferg/goat"

Then initialize it within your application

    g := goat.NewGoat()

You'll probably want sessions and a database in your app, so configure the middleware as well

    g.RegisterMiddleware(g.NewSessionMiddleware("mysessionstore"))
    g.RegisterMiddleware(g.NewDatabaseMiddleware("localhost", "database_name"))

Now that you're middleware is configured you can specify your routes, as well as any interceptors that you want to use

    g.RegisterRoute("/login", goat.GET|goat.POST, "login", Login)
    g.RegisterRoute("/", goat.GET, "index", goat.NewAuthSessionInterceptor(GetUserDetails, Login))
    g.RegisterStaticFileHandler("/static/")

And you're ready to start serving your app

    g.ListenAndServe("8080")

Routes

Routes can be considered controllers in the MVC sense, and adhere to the Handler type:

    func(w http.ResponseWriter, r *http.Request, c *goat.Context) error

Responses to a request can happen through the traditional methods with http.ResponseWriter. The context attached to each request is how you can access your database, session, or user from within a view handler

    type Context struct {
        Database *mgo.Database
        Session  *sessions.Session
        User     *User
    }

Note that Goat uses the Gorilla Web Toolkit under the hood for a number of functions, including session management. If you need to manipulate the session directly, you'll need to import "github.com/gorilla/sessions" or a compatible fork.

Middleware

Middleware is a higher-order function that returns a function with the following signature:

    func(r *http.Request, c *goat.Context)

Creating your own middleware is easy:

    func NewMyCoolMiddleware(awesometown string) Middleware {
        return func(r *http.Request, c *Context) error {
            // Middleware that adds a string to a users session
            c.Session.Values["coolkey"] = awesometown
            return err
        }
    }

All middleware will run in the order in which it is registered, before a handler is called on your route. Out of the box, Goat provides the following middleware:

    // Enables session management on your requests
    NewSessionMiddleware(storename string) Middleware

    // Configures a database connection and clones that connect onto each request
    NewDatabaseMiddleware(host, name string) Middleware

Interceptor

Sometimes you may want requests to perform an action that other requests shouldn't do. Since middleware isn't a viable solution in this case, Goat provides interceptors. An interceptor returns a function with a Handler type:

    func(w http.ResponseWriter, r *http.Request, c *goat.Context) error

Like middleware, interceptors are also easy to write:

    func NewMyCoolInterceptor(route Handler) Interceptor {
        // Informs the user they aren't cool if the request did not include the X-Cool-Header
        // otherwise the route will function as normal
        return func(w http.ResponseWriter, r *http.Request, c *Context) Handler {
            if h := r.Header.Get("X-Cool-Header"); h == "" {
                return func(w http.ResponseWriter, r *http.Request, c *Context) error {
                    http.Error(w, "You ain't cool!", http.StatusUnauthorized)
                    return nil
                }
            }

            return route
        }
    }

Goat provides the following interceptors for you:

    // Authenticates a request via basic auth
    NewBasicAuthInterceptor(normal Handler) Interceptor

    // Verifies that the session associated with this request has a goat.User associated with it,
    // otherwise it redirects to unauthorized
    NewAuthSessionInterceptor(normal, unauthorized Handler) Interceptor

Templates

Goat provides some conveniences for the built-in html/template package, provided that you parse your templates through the framework:

tFuncs = map[string]interface{}{
	"truncate":   truncate,
	"prettyDate": prettyDate,
}
ts = goat.ParseTemplates("templates", "templates/*.html", tFuncs, []string{"{%", "%}"})

Convenience template functions can be found in the package documentation.

goat's People

Contributors

scottferg avatar

Stargazers

 avatar

Watchers

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