Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
/muxPublic

Package mux implements an HTTP request multiplexer.

License

NotificationsYou must be signed in to change notification settings

hslam/mux

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

74 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PkgGoDevBuild StatuscodecovGo Report CardLICENSE

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 mainimport ("github.com/hslam/mux""log""net/http")funcmain() {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 mainimport ("fmt""github.com/hslam/mux""log""net/http")funcmain() {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 -XGEThttp://localhost:8080/favicon.ico

Not Found : /favicon.ico

curl -XGEThttp://localhost:8080/hello

hello world Method:GET

curl -XPOSThttp://localhost:8080/hello

hello world Method:POST

curl -Ihttp://localhost:8080/hello

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

curl -XPATCHhttp://localhost:8080/hello

hello world Method:PATCH

curl -XOPTIONShttp://localhost:8080/hello

hello world Method:OPTIONS

curlhttp://localhost:8080/hello/123/world/456

hello key:123 value:456

curlhttp://localhost:8080/group/foo/1

group/foo id:1

curlhttp://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.

About

Package mux implements an HTTP request multiplexer.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp