码迷,mamicode.com
首页 > Web开发 > 详细

Go Pentester - HTTP Servers(2)

时间:2020-03-07 18:54:27      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:url   %s   pat   png   test   com   exp   int   parameter   

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\n", 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\n", user)
	}).Methods("GET")
	http.ListenAndServe(":8000", r)
}

Run and test sample 3.

技术图片

 

Go Pentester - HTTP Servers(2)

标签:url   %s   pat   png   test   com   exp   int   parameter   

原文地址:https://www.cnblogs.com/keepmoving1113/p/12436114.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!