• Stars
    star
    518
  • Rank 82,103 (Top 2 %)
  • Language
    Go
  • License
    MIT License
  • Created over 10 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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

More Repositories

1

fresh

Build and (re)start go web apps after saving/creating/deleting source files.
Go
3,704
star
2

web-app-theme

A simple theme for web apps
Ruby
2,452
star
3

radiant-gallery

Photo Gallery Management System based on Radiant CMS (Ruby on Rails).
Ruby
73
star
4

radiant-copy-move

It's an extension for Radiant CMS. It allows for a page to be copied (or moved) under a new parent.
Ruby
41
star
5

musix_match

API wrapper for musixmatch.com API's
Ruby
33
star
6

traffic-chromelogger

Chrome Logger middleware for the Traffic web framework (#Go #Golang)
Go
30
star
7

radiant-iphone-extension

This extension adds an iphone tailored ui to Radiant CMS.
JavaScript
29
star
8

radiant-newsletter

Newsletter system based on Radiant CMS
Ruby
28
star
9

terror

A micro feed aggregator based on sinatra
Ruby
27
star
10

go-base62

base62 conversions fo Go. Useful for url shorteners. #go #golang
Go
25
star
11

active_musicbrainz

Ruby ActiveRecord models for the MusicBrainz database.
Ruby
24
star
12

xrequestid

xrequestid implements an http middleware for Negroni that assigns a random id to each request
Go
21
star
13

matryoshka

an image transformation reverse proxy written in elixir
Elixir
19
star
14

code_reloader

Code reloader Plug
Elixir
18
star
15

couchdb-flex

ActionScript
18
star
16

super-star-rating

A javascript library for rating with Ajax (base on prototype)
JavaScript
13
star
17

musixmatch-node

javascript musiXmatch API library for nodejs
JavaScript
11
star
18

signed_request

signed request encoder/decoder for elixir
Elixir
10
star
19

plate

Project/files generator based on application templates
Go
10
star
20

minify_response

Elixir Plug that minifies response body
Elixir
9
star
21

stickerl

QRCode generator web service written in Go. #golang
Go
8
star
22

nimbus-deploy

Deploy scripts for Nimbus beacon chain.
Makefile
8
star
23

messenger

Facebook messenger API client for Elixir
Elixir
8
star
24

yaml_encoder

YAML encoder for elixir
Elixir
7
star
25

treenotify

golang package to watch a filesystem tree. it watches a path and all subfolders
Go
6
star
26

wordreference-cli

simple wordreference.com CLI
CoffeeScript
6
star
27

matrioska

A thumbnails generator server written in Go.
Go
6
star
28

miniassert

Mini assert testing framework for the Go language
Go
6
star
29

traffic-airbrake

Traffic Airbrake Middleware #go, #golang
Go
6
star
30

code-reload-example

Example Elixir app using https://github.com/pilu/code_reloader
Elixir
5
star
31

musicbrainz_cli

Command Line Interface for the MusicBrainz database
Ruby
5
star
32

globnotify

golang glob watcher. it watches a filesystem tree and notifies on file changes based on a glob pattern
Go
4
star
33

robin

a JSON configurable scraper built in Go #golang
Go
4
star
34

etherscan-go

etherscan.io API client in Go
Go
4
star
35

abramo

Simple in-memory HTTP document based database made in nodejs
JavaScript
3
star
36

racko

Enables the Racco's power
Ruby
3
star
37

.dotfiles

dot files
Lua
3
star
38

karaokeeapp

Ruby
3
star
39

coderwall-go

A Go client for the Coderwall API
Go
3
star
40

tap-wallet

JavaScript
3
star
41

style-rule

Made in CoffeeScript, it adds the ability to manage css style rules from javascript.
JavaScript
2
star
42

exy

simple program that shows exif data from pictures
C
2
star
43

i18n-go

i18n for Go #golang
2
star
44

mbslave

Fork of https://bitbucket.org/lalinsky/mbslave
Python
2
star
45

reasonml-examples

JavaScript
2
star
46

stravatt

Create Time Trial Challenges with ruby and the Strava API
Ruby
2
star
47

cerebellum

small implementation of the Musicbrainz Web Service in Go
Go
2
star
48

gravatar

Gravatar URLs module for Elixir
Elixir
2
star
49

config

Config file parser for Go
Go
2
star
50

feedfinder

Discovers feed links on web pages (#go #golang)
Go
2
star
51

wagmi-rainbow-example

JavaScript
2
star
52

metagraph

Go
2
star
53

go-jsmin

Douglas Crockford's JSMin in Go. Package and command. #golang
Go
2
star
54

microphone

Go
1
star
55

status-bot-rb

Ruby
1
star
56

docker-postgres

Shell
1
star
57

bloody-mary

Python
1
star
58

lyricfind

Lyricfind client for the Go language
Go
1
star
59

upc2020

Shell
1
star
60

mxm-cocoa

musixmatch cocoa lib
Objective-C
1
star
61

kojiro

Objective-C
1
star
62

test-embark

JavaScript
1
star
63

ads-market

JavaScript
1
star
64

upc-status-workshop

Makefile
1
star
65

webassets

Go
1
star
66

app_hosts

updates host address on /etc/hosts based on app config
Ruby
1
star
67

react-redux-typescript-app-base

CSS
1
star
68

liked

notification with growl everytime your facebook page has a new like
JavaScript
1
star
69

scala99

Ninety-Nine Scala Problems
Scala
1
star
70

vimux-elixir

run elixir tests in vim
Vim Script
1
star
71

gravityblast.com

gravitylast.com
CSS
1
star
72

superlivio

it's a long story.
Objective-C
1
star
73

boudra.github.io

CSS
1
star
74

mini-strava

A ruby client and CLI for the Strava API
Ruby
1
star
75

girneys

Ruby
1
star
76

timetrialcc

timetrial.cc rails project
JavaScript
1
star
77

idle-batched-mint

JavaScript
1
star
78

metamask-approval-bug

JavaScript
1
star
79

keycard-react-native-ios-test

Java
1
star
80

tap-wallet-pos

tap wallet point of sale dapp
JavaScript
1
star
81

scrapesburys

HTML
1
star
82

test-issues

1
star
83

bip39-elixir

BIP39 - Mnemonic code for generating deterministic keys in elixir
Elixir
1
star
84

cortado

A URL shortener written in Go and base on Traffic.
Go
1
star
85

remarkable-scripts

remarkable-scripts
Ruby
1
star
86

3words-miner

JavaScript
1
star
87

component.js

JavaScript
1
star