traffic

Sinatra inspired regexp/pattern mux and web framework for Go [NOT MAINTAINED]

Github星跟蹤圖

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

主要指標

概覽
名稱與所有者gravityblast/traffic
主編程語言Go
編程語言Go (語言數: 2)
平台
許可證MIT License
所有者活动
創建於2013-08-09 15:39:44
推送於2019-10-22 06:25:14
最后一次提交2015-11-26 21:31:07
發布數7
最新版本名稱v0.5.3 (發布於 2014-07-11 10:43:44)
第一版名稱v0.2.0 (發布於 2013-11-17 05:04:22)
用户参与
星數521
關注者數23
派生數34
提交數137
已啟用問題?
問題數4
打開的問題數0
拉請求數6
打開的拉請求數1
關閉的拉請求數0
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?