GithubHelp home page GithubHelp logo

szxp / mux Goto Github PK

View Code? Open in Web Editor NEW
9.0 1.0 1.0 35 KB

Lightweight HTTP request multiplexer (router) for Golang.

License: BSD 3-Clause "New" or "Revised" License

Go 98.82% Shell 1.18%
router mux golang golang-router golang-mux go

mux's Introduction

Build Status Build Status GoDoc Go Report Card

mux

A lightweight HTTP request router (multiplexer). Documentation is available at GoDoc.

Releases

Master branch is the stable production-ready branch.

Features

  • Static and dynamic patterns supported. Dynamic parameter values are available in the request's context.
  • Compatible with the built-in http.Handler
  • Only standard library dependencies.
  • Go 1.7+ supported.

Benchmarks

Testing the examples in the benchmark directory running the bench.sh script at least three times for each muxer. The result is:

              dynamic route (/some/page/:id)

httprouter    27229 Requests/sec  (+13.40 %)
bone          25679 Requests/sec  ( +6.95 %)
mux           25439 Requests/sec  ( +5.95 %)
gorrila/mux   24010 Requests/sec  (     0 %)
              static route (/other/page/path)

httprouter    27780 Requests/sec  (+10.71 %)
bone          27357 Requests/sec  ( +9,03 %)
mux           26792 Requests/sec  ( +6.77 %)
gorrila/mux   25091 Requests/sec  (     0 %)

The test machine was a Dell Latitude D630 laptop with Intel(R) Core2 Duo T7250 2.00 GHz processor.

Example

package main

import (
	"fmt"
	"github.com/szxp/mux"
	"net/http"
)

func main() {
	muxer := mux.NewMuxer()
	muxer.HandleFunc("/", indexHandler, "GET")
	muxer.HandleFunc("/login", loginHandler, "POST")
	muxer.HandleFunc("/users/:username", userHandler)
	muxer.NotFound(notFoundHandler)
	http.ListenAndServe(":8080", muxer)
}

func indexHandler(w http.ResponseWriter, r *http.Request) {
	body := []byte(`
		<h1>Home</h1>
		<p>
			<a href="/users/admin">Admin profile page</a> <br/>
			<a href="/login">Login page</a>
		</p>
		<form action="/" method="POST">
			<button type="submit">Post to Home URL</button>
		</form>
		<form action="/nonexisting" method="POST">
			<button type="submit">Post to non existing URL</button>
		</form>
	`)
	w.Header().Add("Content-Type", "text/html; charset=utf-8")
	w.Header().Add("Content-Length", fmt.Sprintf("%d", len(body)))
	w.Write(body)
}

func loginHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "Login")
}

func userHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, r.Context().Value(mux.CtxKey("username")))
}

func notFoundHandler(w http.ResponseWriter, r *http.Request, methodMismatch bool) {
	if methodMismatch {
		http.Error(w, r.Method+" not allowed", http.StatusMethodNotAllowed)
		return
	}
	http.Error(w, "not found", http.StatusNotFound)
}

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.