Giter VIP home page Giter VIP logo

traffic's Introduction

Traffic

Build Status

Package traffic - a Sinatra inspired regexp/pattern mux for Go.

Installation

go get github.com/pilu/traffic

Features

Development Features

development is the default environment. The above middlewares are loaded only in development.

If you want to run your application in production, export TRAFFIC_ENV with production as value.

TRAFFIC_ENV=production your-executable-name

Installation

Dowload the Traffic code:

go get github.com/pilu/traffic

Build the command line tool:

go get github.com/pilu/traffic/traffic

Create a new project:

traffic new hello

Run your project:

cd hello
go build && ./hello

You can use Fresh if you want to build and restart your application every time you create/modify/delete a file.

Example:

The following code is a simple example, the documentation in still in development. For more examples check the examples folder.

package main

import (
  "net/http"
  "github.com/pilu/traffic"
  "fmt"
)

func rootHandler(w traffic.ResponseWriter, r *traffic.Request) {
  fmt.Fprint(w, "Hello World\n")
}

func pageHandler(w traffic.ResponseWriter, r *traffic.Request) {
  params := r.URL.Query()
  fmt.Fprintf(w, "Category ID: %s\n", params.Get("category_id"))
  fmt.Fprintf(w, "Page ID: %s\n", params.Get("id"))
}

func main() {
  router := traffic.New()

  // Routes
  router.Get("/", rootHandler)
  router.Get("/categories/:category_id/pages/:id", pageHandler)

  router.Run()
}

Before Filters

You can also add "before filters" to all your routes or just to some of them:

router := traffic.New()

// Executed before all handlers
router.AddBeforeFilter(checkApiKey).
       AddBeforeFilter(addAppNameHeader).
       AddBeforeFilter(addTimeHeader)

// Routes
router.Get("/", rootHandler)
router.Get("/categories/:category_id/pages/:id", pageHandler)

// "/private" has one more before filter that checks for a second api key (private_api_key)
router.Get("/private", privatePageHandler).
        AddBeforeFilter(checkPrivatePageApiKey)

Complete example:

func rootHandler(w traffic.ResponseWriter, r *traffic.Request) {
  fmt.Fprint(w, "Hello World\n")
}

func privatePageHandler(w traffic.ResponseWriter, r *traffic.Request) {
  fmt.Fprint(w, "Hello Private Page\n")
}

func pageHandler(w traffic.ResponseWriter, r *traffic.Request) {
  params := r.URL.Query()
  fmt.Fprintf(w, "Category ID: %s\n", params.Get("category_id"))
  fmt.Fprintf(w, "Page ID: %s\n", params.Get("id"))
}

func checkApiKey(w traffic.ResponseWriter, r *traffic.Request) {
  params := r.URL.Query()
  if params.Get("api_key") != "foo" {
    w.WriteHeader(http.StatusUnauthorized)
  }
}

func checkPrivatePageApiKey(w traffic.ResponseWriter, r *traffic.Request) {
  params := r.URL.Query()
  if params.Get("private_api_key") != "bar" {
    w.WriteHeader(http.StatusUnauthorized)
  }
}

func addAppNameHeader(w traffic.ResponseWriter, r *traffic.Request) {
  w.Header().Add("X-APP-NAME", "My App")
}

func addTimeHeader(w traffic.ResponseWriter, r *traffic.Request) {
  t := fmt.Sprintf("%s", time.Now())
  w.Header().Add("X-APP-TIME", t)
}

func main() {
  router := traffic.New()

  // Routes
  router.Get("/", rootHandler)
  router.Get("/categories/:category_id/pages/:id", pageHandler)
  // "/private" has one more before filter that checks for a second api key (private_api_key)
  router.Get("/private", privatePageHandler).
          AddBeforeFilter(checkPrivatePageApiKey)

  // Executed before all handlers
  router.AddBeforeFilter(checkApiKey).
         AddBeforeFilter(addAppNameHeader).
         AddBeforeFilter(addTimeHeader)

  router.Run()
}

Author

More

traffic's People

Contributors

dz1984 avatar elia avatar gravityblast avatar stevenwilkin avatar tomsteele avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

traffic's Issues

Can't set Header on NotFoundHandler

I'm trying to override the content-type for the NotFoundHandler to be in Content-Type: application/json; charset=utf-8 but it seems like its still using Content-Type: text/plain even with beforeFilter setup.

Error when trying "go get"

Hi, first of all I want to thank you for your great work.

I'm using your package in a development workstation based on Mac OSX, everything works great but when trying to install the package on my development server (Ubuntu 12.04 32bits) I get the following output:

go get github.com/pilu/traffic

/usr/lib/go/src/pkg/github.com/pilu/config/config.go:20: commentSplitRegexp.Split undefined (type *regexp.Regexp has no field or method Split)
/usr/lib/go/src/pkg/github.com/pilu/config/config.go:51: keyValueSplitRegexp.Split undefined (type *regexp.Regexp has no field or method Split)

Need an AfterFilter

I need a filter that executes both before and after a route has executed. I'm trying to add miniprofiler support to traffic, and so need to run a finalizer to stop some timers.

Multipe calls to WriteHeader ?

I have a handler which calls WriteHeader(int) on a traffic.ResponseWriter. However I get in the log:

2014/08/21 13:15:11 http: multiple response.WriteHeader calls

I've confirmed the handler itself only calls it once, and looking at the implementation in traffic I don't see why the error is happening. Nothing seems to break but the "error" is logged.

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.