All Projects → takama → Router

takama / Router

Licence: other
A simple, compact and fast router package to process HTTP requests

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Router

Redux Json Router
Declarative, Redux-first routing for React/Redux browser applications.
Stars: ✭ 37 (-63.73%)
Mutual labels:  json, router
Routes
typed bidirectional routes for OCaml/ReasonML web applications
Stars: ✭ 102 (+0%)
Mutual labels:  router
Rki Covid Api
🦠🇩🇪📈 An API for the spread of covid-19 in Germany. Data from Robert-Koch-Institut.
Stars: ✭ 98 (-3.92%)
Mutual labels:  json
Iso 3166 Countries With Regional Codes
ISO 3166-1 country lists merged with their UN Geoscheme regional codes in ready-to-use JSON, XML, CSV data sets
Stars: ✭ 1,372 (+1245.1%)
Mutual labels:  json
Jsonabc
Sorts JSON object alphabetically. It supports nested objects, arrays and collections. Works offline and beautifies JSON object too.
Stars: ✭ 100 (-1.96%)
Mutual labels:  json
Simplejson
simplejson is a simple, fast, extensible JSON encoder/decoder for Python
Stars: ✭ 1,372 (+1245.1%)
Mutual labels:  json
Dynamo Archive
Archive and Restore DynamoDB Tables, from the Command Line
Stars: ✭ 99 (-2.94%)
Mutual labels:  json
Macholibre
Mach-O & Universal Binary Parser
Stars: ✭ 102 (+0%)
Mutual labels:  json
Xunleikuainiaoinshell
[ 迅雷快鸟 Shell 版 ] A Shell Implementation of Kuainiao, Xunlei
Stars: ✭ 102 (+0%)
Mutual labels:  router
Yar
Light, concurrent RPC framework for PHP & C
Stars: ✭ 1,369 (+1242.16%)
Mutual labels:  json
Eslint Plugin I18n Json
Fully extendable eslint plugin for JSON i18n translation files.
Stars: ✭ 101 (-0.98%)
Mutual labels:  json
Mercury
Simple Android app that sends pre-configured commands to remote servers via SSH.
Stars: ✭ 100 (-1.96%)
Mutual labels:  json
Pc Engines Apu Router Guide
Guide to building a Linux or BSD router on the PC Engines APU platform
Stars: ✭ 101 (-0.98%)
Mutual labels:  router
Wernicke
Redaction for structured data
Stars: ✭ 100 (-1.96%)
Mutual labels:  json
Swagger Express Ts
Generate and serve swagger.json
Stars: ✭ 102 (+0%)
Mutual labels:  json
Parse Google Docs Json
Authenticates with Google API and parse Google Docs to JSON or Markdown
Stars: ✭ 100 (-1.96%)
Mutual labels:  json
Pytkgen
Create Tkinter GUIs from JSON definition files.
Stars: ✭ 100 (-1.96%)
Mutual labels:  json
Jam Api
Parse web pages using CSS query selectors
Stars: ✭ 1,375 (+1248.04%)
Mutual labels:  json
Circe Yaml
YAML parser for circe using SnakeYAML
Stars: ✭ 102 (+0%)
Mutual labels:  json
Lychee
The most complete and powerful data-binding library and persistence infra for Kotlin 1.3, Android & Splitties Views DSL, JavaFX & TornadoFX, JSON, JDBC & SQLite, SharedPreferences.
Stars: ✭ 102 (+0%)
Mutual labels:  json

Go Router

Build Status GoDoc Contributions Welcome Go Report Card codecov.io

A simple, compact and fast router package to process HTTP requests. It has some sugar from framework however still lightweight. The router package is useful to prepare a RESTful API for Go services. It has JSON output, which bind automatically for relevant type of data. The router has timer feature to display duration of request handling in the header

Examples

  • Simplest example (serve static route):
package main

import (
	"github.com/takama/router"
)

func Hello(c *router.Control) {
	c.Body("Hello world")
}

func main() {
	r := router.New()
	r.GET("/hello", Hello)

	// Listen and serve on 0.0.0.0:8888
	r.Listen(":8888")
}
  • Check it:
curl -i http://localhost:8888/hello/

HTTP/1.1 200 OK
Content-Type: text/plain
Date: Sun, 17 Aug 2014 13:25:50 GMT
Content-Length: 11

Hello world
  • Serve dynamic route with parameter:
package main

import (
	"github.com/takama/router"
)

func main() {
	r := router.New()
	r.GET("/hello/:name", func(c *router.Control) {
		c.Body("Hello " + c.Get(":name"))
	})

	// Listen and serve on 0.0.0.0:8888
	r.Listen(":8888")
}
  • Check it:
curl -i http://localhost:8888/hello/John

HTTP/1.1 200 OK
Content-Type: text/plain
Date: Sun, 17 Aug 2014 13:25:55 GMT
Content-Length: 10

Hello John
  • Checks JSON Content-Type automatically:
package main

import (
	"github.com/takama/router"
)

// Data is helper to construct JSON
type Data map[string]interface{}

func main() {
	r := router.New()
	r.GET("/api/v1/settings/database/:db", func(c *router.Control) {
		data := Data{
			"Database settings": Data{
				"database": c.Get(":db"),
				"host":     "localhost",
				"port":     "3306",
			},
		}
		c.Code(200).Body(data)
	})
	// Listen and serve on 0.0.0.0:8888
	r.Listen(":8888")
}
  • Check it:
curl -i http://localhost:8888/api/v1/settings/database/testdb

HTTP/1.1 200 OK
Content-Type: application/json
Date: Sun, 17 Aug 2014 13:25:58 GMT
Content-Length: 102

{
  "Database settings": {
    "database": "testdb",
    "host": "localhost",
    "port": "3306"
  }
}
  • Use timer to calculate duration of request handling:
package main

import (
	"github.com/takama/router"
)

// Data is helper to construct JSON
type Data map[string]interface{}

func main() {
	r := router.New()
	r.GET("/api/v1/settings/database/:db", func(c *router.Control) {
		c.UseTimer()

		// Do something

		data := Data{
			"Database settings": Data{
				"database": c.Get(":db"),
				"host":     "localhost",
				"port":     "3306",
			},
		}
		c.Code(200).Body(data)
	})
	// Listen and serve on 0.0.0.0:8888
	r.Listen(":8888")
}
  • Check it:
curl -i http://localhost:8888/api/v1/settings/database/testdb

HTTP/1.1 200 OK
Content-Type: application/json
Date: Sun, 17 Aug 2014 13:26:05 GMT
Content-Length: 143

{
  "duration": 5356123
  "took": "5.356ms",
  "data": {
    "Database settings": {
      "database": "testdb",
      "host": "localhost",
      "port": "3306"
    }
  }
}
  • Custom handler with "Access-Control-Allow" options and compact JSON:
package main

import (
	"github.com/takama/router"
)

// Data is helper to construct JSON
type Data map[string]interface{}

func baseHandler(handle router.Handle) router.Handle {
	return func(c *router.Control) {
		c.CompactJSON(true)
		if origin := c.Request.Header.Get("Origin"); origin != "" {
			c.Writer.Header().Set("Access-Control-Allow-Origin", origin)
			c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
		}
		handle(c)
	}
}

func Info(c *router.Control) {
	data := Data{
		"debug": true,
		"error": false,
	}
	c.Body(data)
}

func main() {
	r := router.New()
	r.CustomHandler = baseHandler
	r.GET("/info", Info)

	// Listen and serve on 0.0.0.0:8888
	r.Listen(":8888")
}
  • Check it:
curl -i -H 'Origin: http://foo.com' http://localhost:8888/info/

HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://foo.com
Content-Type: text/plain
Date: Sun, 17 Aug 2014 13:27:10 GMT
Content-Length: 28

{"debug":true,"error":false}
  • Use google json style https://google.github.io/styleguide/jsoncstyleguide.xml:
package main

import (
	"net/http"

	"github.com/takama/router"
)

func main() {
	r := router.New()
	r.GET("/api/v1/people/:action/:id", func(c *router.Control) {

		// Do something

		c.Method("people." + c.Get(":action"))
		c.SetParams(map[string]string{"userId": c.Get(":id")})
		c.SetError(http.StatusNotFound, "UserId not found")
		c.AddError(router.Error{Message: "Group or User not found"})
		c.Code(http.StatusNotFound).Body(nil)
	})
	// Listen and serve on 0.0.0.0:8888
	r.Listen(":8888")
}
  • Check it:
curl -i http://localhost:8888/api/v1/people/get/@me

HTTP/1.1 404 Not Found
Content-Type: application/json
Date: Sat, 22 Oct 2016 14:50:00 GMT
Content-Length: 220

{
  "method": "people.get",
  "params": {
    "userId": "@me"
  },
  "error": {
    "code": 404,
    "message": "UserId not found",
    "errors": [
      {
        "message": "Group or User not found"
      }
    ]
  }
}

Contributors (unsorted)

All the contributors are welcome. If you would like to be the contributor please accept some rules.

  • The pull requests will be accepted only in "develop" branch
  • All modifications or additions should be tested
  • Sorry, I'll not accept code with any dependency, only standard library

Thank you for your understanding!

License

BSD License

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].