GithubHelp home page GithubHelp logo

gibbon's Introduction

Gibbon

Gibbon is a very simple way to setup middleware, that uses net/http Handler interface.

Getting Started

After installing Go and setting up your GOPATH, create your first .go file. We'll call it server.go.

package main

import (
  "fmt"
  "github.com/claudiuandrei/gibbon"
  "net/http"
)

func main() {

  // Setup the app
  g := gibbon.NewApp()

  // Setup a middleware
  g.Use(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    w.Header().Add("X-Powered-By", "Gibbon")
  }))

  // Setup the router
  mux := http.NewServeMux()
  mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
    fmt.Fprintf(w, "Welcome to Gibbon!")
  })
  g.Use(mux)

  // Run the server
  g.Run(":3000")
}

Then install the Gibbon package (go 1.1 and greater is required):

go get github.com/claudiuandrei/gibbon

Then run your server:

go run server.go

You will now have a Go net/http webserver running on localhost:3000.

Use() - Middleware & Routing

Gibbon Use function adds middleware to the request execution chain. Everything that acts like a net/http Handler can be a middleware and will run through each of them in the order added until the ResponseWriter is flushed.

Gibbon is BYOR (Bring your own Router). Gibbon is fully supporting net/http.

// New Application
g := gibbon.NewApp()

// Middleware
g.Use(FirstMiddleware)

// Setup the router
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
  fmt.Fprintf(w, "Welcome to Gibbon!")

  // Flush the response
  // This will break the execution chain and the remaining middleware is skipped
  w.(http.Flusher).Flush()
})
g.Use(mux)

// More middleware
g.Use(LastMiddleware)

// Run the server
g.Run(":3000")

Run() - Server

Gibbon has a convenience function called Run. Run takes an addr string identical to http.ListenAndServe.

Authors

Claudiu Andrei

gibbon's People

Contributors

claudiuandrei avatar

Stargazers

 avatar

Watchers

 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.