Git Product home page Git Product logo

mux's Introduction

mux

PkgGoDev Build Status codecov Go Report Card LICENSE

Package mux implements an HTTP request multiplexer.

Features

  • Middleware
  • Group
  • Path matching and routing
  • Fully compatible with the http.HandlerFunc
  • Not found
  • Recovery
  • HTTP Handler

Get started

Install

go get github.com/hslam/mux

Import

import "github.com/hslam/mux"

Usage

Simple Example

package main

import (
	"github.com/hslam/mux"
	"log"
	"net/http"
)

func main() {
	m := mux.New()
	m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hello World"))
	})
	log.Fatal(http.ListenAndServe(":8080", m))
}

Example

package main

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

func main() {
	m := mux.New()
	m.Recovery(mux.Recovery)
	m.NotFound(func(w http.ResponseWriter, r *http.Request) {
		http.Error(w, "Not Found : "+r.URL.String(), http.StatusNotFound)
	})
	m.Use(func(w http.ResponseWriter, r *http.Request) {
		fmt.Printf("Host:%s Path:%s Method:%s\n", r.Host, r.URL.Path, r.Method)
	})
	m.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(fmt.Sprintf("hello world Method:%s\n", r.Method)))
	}).All()
	m.HandleFunc("/hello/:key/world/:value", func(w http.ResponseWriter, r *http.Request) {
		params := m.Params(r)
		w.Write([]byte(fmt.Sprintf("hello key:%s value:%s\n", params["key"], params["value"])))
	}).GET().POST().PUT().DELETE()
	m.Group("/group", func(m *mux.Mux) {
		m.HandleFunc("/foo/:id", func(w http.ResponseWriter, r *http.Request) {
			params := m.Params(r)
			w.Write([]byte(fmt.Sprintf("group/foo id:%s\n", params["id"])))
		}).GET()
		m.HandleFunc("/bar/:id", func(w http.ResponseWriter, r *http.Request) {
			params := m.Params(r)
			w.Write([]byte(fmt.Sprintf("group/bar id:%s\n", params["id"])))
		}).GET()
	})
	log.Fatal(http.ListenAndServe(":8080", m))
}

curl -XGET http://localhost:8080/favicon.ico

Not Found : /favicon.ico

curl -XGET http://localhost:8080/hello

hello world Method:GET

curl -XPOST http://localhost:8080/hello

hello world Method:POST

curl -I http://localhost:8080/hello

HTTP/1.1 200 OK
Date: Tue, 01 Oct 2019 20:28:42 GMT
Content-Length: 24
Content-Type: text/plain; charset=utf-8

curl -XPATCH http://localhost:8080/hello

hello world Method:PATCH

curl -XOPTIONS http://localhost:8080/hello

hello world Method:OPTIONS

curl http://localhost:8080/hello/123/world/456

hello key:123 value:456

curl http://localhost:8080/group/foo/1

group/foo id:1

curl http://localhost:8080/group/bar/2

group/bar id:2

License

This package is licensed under a MIT license (Copyright (c) 2019 Meng Huang)

Author

mux was written by Meng Huang.

mux's People

Contributors

hslam avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.