zoukankan      html  css  js  c++  java
  • Go Pentester

    Routing with the gorilla/mux Package

    A powerful HTTP router and URL matcher for building Go web servers

    https://github.com/gorilla/mux

    Install package

    go get -u github.com/gorilla/mux

    Build sample 1:

    package main
    
    import (
    	"fmt"
    	"github.com/gorilla/mux"
    	"net/http"
    )
    
    func main() {
    	r := mux.NewRouter()
    	r.HandleFunc("/foo", func(w http.ResponseWriter, req *http.Request) {
    		fmt.Fprintln(w, "hi foo")
    	}).Methods("GET")
    	http.ListenAndServe(":8000", r)
    }
    

    Run the test sample 1.

     Build sample 2: It's helpful to match and pass in parameters within the request patch (for example, when implementing a RESTful API)

    package main
    
    import (
    	"fmt"
    	"github.com/gorilla/mux"
    	"net/http"
    )
    
    func main() {
    	r := mux.NewRouter()
    	r.HandleFunc("/users/{user}", func(w http.ResponseWriter, req *http.Request) {
    		user := mux.Vars(req)["user"]
    		fmt.Fprintf(w, "hi %s
    ", user)
    	}).Methods("GET")
    	http.ListenAndServe(":8000", r)
    }
    

    Run and test sample 2.

     Build sample 3: Use regular expression to qualify the patterns passed.

    package main
    
    import (
    	"fmt"
    	"github.com/gorilla/mux"
    	"net/http"
    )
    
    func main() {
    	r := mux.NewRouter()
    	r.HandleFunc("/users/{user:[a-z]+}", func(w http.ResponseWriter, req *http.Request) {
    		user := mux.Vars(req)["user"]
    		fmt.Fprintf(w, "hi %s
    ", user)
    	}).Methods("GET")
    	http.ListenAndServe(":8000", r)
    }
    

    Run and test sample 3.

    相信未来 - 该面对的绝不逃避,该执著的永不怨悔,该舍弃的不再留念,该珍惜的好好把握。
  • 相关阅读:
    objdump man
    python c cpp extention
    http,get,head,post
    三种客户端访问wcf服务端的方法 C#
    使用HttpWebRequest POST 文件,带参数
    ASP.NET MVC3 HtmlHelper用法大全
    Windows安装memcached图文教程(转)
    sort排序应用
    WPA密码攻击宝典
    Bind和Eval的不同用法 (转)
  • 原文地址:https://www.cnblogs.com/keepmoving1113/p/12436114.html
Copyright © 2011-2022 走看看