简体   繁体   English

在Go中使用URL路由将URL路径与页面名称匹配的正确方法?

[英]Correct way to match URL path to page name with URL routing in Go?

I'm making a Go website (small service), and didn't know how a page URL was verified as correct or not found 404. Eventually I learned that http request routers / multiplexers exist. 我正在制作一个Go网站(小型服务),并且不知道页面URL如何被验证为正确或找不到404。最终,我了解到存在HTTP请求路由器/多路复用器。

Example: 例:

eg.com/articles/animals/Hippos-are-aquatic-and-land-dwelling    = go to page
eg.com/articles/animals/Hippos-are-typofrifjirj     = 404 not found page

Right now I only see one way to do this, you somehow have a list of articles the website has, and you pass it into the router somehow. 现在,我只看到一种方法,您以某种方式获得了网站上的文章列表,然后以某种方式将其传递到路由器中。 How should you get that list of articles? 您应该如何获取该文章列表?

For a dynamic relational database site: Do you query your database for article titles, and make that a map string? 对于动态关系数据库站点:是否在数据库中查询文章标题,并使其成为地图字符串?

For static files on a static website: you use some http fileserver dir function in the router or net/http? 对于静态网站上的静态文件:您是否在路由器或net / http中使用了一些http fileserver dir函数?

If so, for the database, does that mean you have to query your database every single time a page is visited? 如果是这样,对于数据库来说,这是否意味着您必须在每次访问页面时都要查询数据库? Or do you store the list of articles in a file or something and update it each time a new article is made? 还是将文章列表存储在文件或其他内容中,并在每次制作新文章时对其进行更新?

Also, I would plan to use https://github.com/julienschmidt/httprouter or similar. 另外,我打算使用https://github.com/julienschmidt/httprouter或类似的名称。

Here's how to do it with the net/http router assuming that everything after /articles/animals/ in the path is the id of an article: 假设路径中/articles/animals/之后的所有内容都是/articles/animals/的ID,以下是使用net / http路由器进行操作的方法:

Register the handler using a trailing slash for match on all paths with the prefix `/articles/animals/': 使用尾部斜杠注册处理程序,以在所有带有前缀`/ articles / animals /'的路径上进行匹配:

mux.HandleFunc("/articles/animals/", animalHandler)

In the handler, strip the /articles/animals/ to get the id of the article. 在处理程序中,剥离/articles/animals/以获取文章的ID。 Look up the article in the database. 在数据库中查找文章。 Respond with 404 if not there. 如果不存在,请回复404。

func animalHandler(w http.ResponseWriter, r *http.Request) {
  id := strings.TrimPrefix(r.URL.Path, "/articles/animals/"))
  article, err := queryArticleByID(id) 
  if err == errNotFound {
     http.Error(w, "internal error", http.StatusNotFound)
     return
  } else if err != nil {
     log.Println(err)
     http.Error(w, "internal error", http.StatusInternalError)
  }
  ... render the article
}

This example assumes that the queryArticleByID() function queries the database and returns errNotFound if no article is present for the give id. 此示例假定queryArticleByID()函数查询数据库,并且如果没有有关给定ID的文章,则返回errNotFound

Regarding caching: The queryArticleByID() can check a cache before querying the database. 关于缓存: queryArticleByID()可以在查询数据库之前检查缓存。 Any caching is independent of how routing is handled. 任何缓存都与路由的处理方式无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM