Regex Go HTTP服务器:使用映射处理路由

Regex Go HTTP服务器:使用映射处理路由,regex,http,go,Regex,Http,Go,我有一个映射,其中包含路由(例如/static/stylesheets/main.css)作为键,相应的代码作为值(实际上是一个巨大的字符串)。我只是想知道,有没有一种简单的方法可以创建一个HTTP服务器,它总是根据map检查传入的请求,并呈现与匹配的键相关联的值(如果该键存在) 到目前为止,我已经 func main(){ var m=generateMap() http.handleFunc(“/”,renderContent); } func renderContent(w http.Re

我有一个
映射
,其中包含路由(例如
/static/stylesheets/main.css
)作为键,相应的代码作为值(实际上是一个巨大的字符串)。我只是想知道,有没有一种简单的方法可以创建一个HTTP服务器,它总是根据
map
检查传入的请求,并呈现与匹配的键相关联的值(如果该键存在)

到目前为止,我已经

func main(){
var m=generateMap()
http.handleFunc(“/”,renderContent);
}
func renderContent(w http.ResponseWriter,r*http.Request){
io.WriteString(w,m[path]);
}
我知道这段代码远未完成,但希望它能澄清我的目标。如何将
路径
m
传递到
renderContent
中,以及如何让
handleFunc
实际处理正则表达式(基本上是任何路径)?

使您的地图成为:


如果你不想自己写,那就用一些小的、快速的、开箱即用的东西——看看gorilla mux。他们的工具包可以让你选择你想要的组件,所以只要添加他们的mux,如果你只想用正则表达式进行路由

我发现它特别有助于从路由中获取变量

r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler)
...

vars := mux.Vars(request)
category := vars["category"]
我建议包是最快的路由器。Go的内置mux不完整且速度缓慢,无法轻松捕获URL参数

此外,您可以考虑将每个路由构造为一个结构,这样当您有多条路由时更容易处理。

此代码捕获URL参数,将其与映射的关键字进行比较,并将代码块字符串打印到控制台

package main

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

type Route struct {
    Name        string
    Method      string
    Pattern     string
    Handle      mux.Handle

}

var codeChunk = map[string]string{ "someUrlPath" : "func printHello(){\n\tfmt.Println(\"Hello\")\n}" }

var route = Route{
    "MyHandler",
    "GET",
    "/:keywordUrl",
    MyHandler,
}

func MyHandler(w http.ResponseWriter, r *http.Request, ps mux.Params) {

    // Handle route "/:keywordUrl"
    w.WriteHeader(http.StatusOK)

    // get the parameter from the URL
    path := ps.ByName("keywordUrl")

    for key, value := range codeChunk {
        // Compare the parameter to the key of the map
        if key != "" && path == key {
            // do something
            fmt.Println(value)
        }
    }
}

func main() {
    router := mux.New()
    router.Handle(route.Method, route.Pattern, route.Handle)

    log.Fatal(http.ListenAndServe(":8080", router))

    // browse to http://localhost:8080/someUrlPath to see 
    // the map's string value being printed.
}

你说的“处理正则表达式”是什么意思?而且,看起来你想要的基本上是一个定制。听起来你在描述一个
muxer
router
,其中有很多已经编写好了。
package main

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

type Route struct {
    Name        string
    Method      string
    Pattern     string
    Handle      mux.Handle

}

var codeChunk = map[string]string{ "someUrlPath" : "func printHello(){\n\tfmt.Println(\"Hello\")\n}" }

var route = Route{
    "MyHandler",
    "GET",
    "/:keywordUrl",
    MyHandler,
}

func MyHandler(w http.ResponseWriter, r *http.Request, ps mux.Params) {

    // Handle route "/:keywordUrl"
    w.WriteHeader(http.StatusOK)

    // get the parameter from the URL
    path := ps.ByName("keywordUrl")

    for key, value := range codeChunk {
        // Compare the parameter to the key of the map
        if key != "" && path == key {
            // do something
            fmt.Println(value)
        }
    }
}

func main() {
    router := mux.New()
    router.Handle(route.Method, route.Pattern, route.Handle)

    log.Fatal(http.ListenAndServe(":8080", router))

    // browse to http://localhost:8080/someUrlPath to see 
    // the map's string value being printed.
}