我正在尝试根据路线提供不同的 HTML 文件。路由器对于“/”工作正常,并且它服务于index.html。然而,当转到“/download”等任何其他路径时,它也会呈现index.html,即使要提供的文件名为share.html。
我在这里做错了什么?
    package main
import (
    "net/http"
    "github.com/gorilla/mux"
    "log"
    "path"
    "fmt"
)
// main func
func main() {
    routes()
}
// routes
func routes() {
    // init router
    r := mux.NewRouter()
    // index route
    r.HandleFunc("/", home)
    r.HandleFunc("/share", share)
    r.HandleFunc("/download", download)
    // start server on port 1337
    log.Fatal(http.ListenAndServe(":1337", r))
}
// serves index file
func home(w http.ResponseWriter, r*http.Request) {
    p := path.Dir("./public/views/index.html")
    // set header
    w.Header().Set("Content-type", "text/html")
    http.ServeFile(w, r, p)
}
// get shared files
func share(w http.ResponseWriter, r …我想使用此处指定的处理程序来记录所有内容。
这就是我所拥有的:
r := mux.NewRouter()
s := r.PathPrefix("/api/v1").Subrouter()
s.HandleFunc("/abc", handler.GetAbc).Methods("GET")
s.HandleFunc("/xyz", handler.GetXyz).Methods("GET")
我想使用日志中间件,但我不想在每一行中重复它,正如它们在 github 中显示的那样:
r.Handle("/admin", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(ShowAdminDashboard)))
r.HandleFunc("/", ShowIndex)
有没有办法只将通用日志中间件传递给r,所有通过r路由器的东西都会先通过中间件?
在go程序中,我想同时运行两个Web服务器,
显然它们将在两个不同的端口上服务(如果需要,还有ip地址),
问题是调用时http.handle,当我尝试注册处理程序时' /'对于第二台服务器,它恐慌并说已经有一个与'/'关联的处理程序,
我想我需要创建一个多路复用器DefaultServeMux以及我试图用它来做gorillaMux但无法搞清楚,  
在同一个程序/进程中运行两个Web服务器是否存在根本性的问题.
为了更清楚,两个Web服务器中的一个是用作常规Web服务器,我需要第二个作为RPC服务器来在集群的不同节点上运行的程序的实例之间进行通信,
编辑:为了使它更清楚,这不是实际的代码,但它是要点
myMux := http.NewServeMux()
myMux.HandleFunc("/heartbeat", heartBeatHandler)
http.Handle("/", myMux)
server := &http.Server{
    Addr:    ":3400",
    Handler: myMux,
}
go server.ListenAndServe()
gorillaMux := mux.NewRouter()
gorillaMux.HandleFunc("/", indexHandler)
gorillaMux.HandleFunc("/book", bookHandler)
http.Handle("/", gorillaMux)
server := &http.Server{
    Addr:    ":1234",
    Handler: gorillaMux,
}
log.Fatal(server.ListenAndServe())
我想用(go get github.com/gorilla/mux)安装Mux包但我总是得到错误消息
# github.com/gorilla/context
open go/src/github.com/gorilla/context/context.go: No such file or directory
我自己创建了目录github.com,gorilla和context.但我没有context.go文件....我该如何解决?
我已经使用gorilla/muxand设置了我的 Go 后端rs/cors。当我尝试发送包含自定义标头 ( Bearer)的请求时,它失败了。
我的服务器设置如下所示:
     router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/users", GetUsers).Methods("GET")
router.HandleFunc("/", GetUsers).Methods("GET")
router.HandleFunc("/tweets", GetTweets).Methods("GET")
router.HandleFunc("/login", Login).Methods("POST")
router.HandleFunc("/profile/tweets", ProfileTweets).Methods("GET")
c := cors.New(cors.Options{
    AllowedOrigins: []string{"*"},
    AllowedMethods: []string{"GET", "POST", "PATCH"},
    AllowedHeaders: []string{"Bearer", "Content_Type"},})
handler := c.Handler(router)
log.Fatal(http.ListenAndServe(":8080", handler))
我尝试了各种其他解决方案(例如OPTIONS在Methods调用中添加。我尝试为其传递Bearer令牌的/profile/tweets端点是端点。
我不确定如何继续gorilla/mux以及如何rs/cors添加预检请求。
我得到的实际错误:
Fetch API 无法加载http://localhost:8080/profile/tweets。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问Origin ' http://localhost:4200 '。如果不透明响应满足您的需求,请将请求的模式设置为“no-cors”以在禁用 CORS 的情况下获取资源。
谢谢!
我正在休假,以便在Go上焕然一新。不幸的是,我下面的代码在两条路径上都抛出404。这是最新的迭代。我最初将路由器包含在handleRouter函数中,并认为将其取出可以解决404ing问题。剧透警报:没有。我怎样才能解决这个问题?谢谢!
package main
import (
    "encoding/json"
    "fmt"
    "log"
    "net/http"
    "github.com/gorilla/mux"
)
type Article struct {
    Title   string `json:"Title"`
    Desc    string `json:"desc"`
    Content string `json:"content"`
}
type Articles []Article
func main() {
    fmt.Println("Router v2 - Muxx")
    myRouter := mux.NewRouter()
    myRouter.HandleFunc("/all", returnAllArticles).Methods("GET")
    myRouter.HandleFunc("/", homePage).Methods("GET")
    log.Fatal(http.ListenAndServe(":8000", nil))
}
func homePage(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "Hello:")
    fmt.Println("Endpoint Hit: homepage")
}
func returnAllArticles(w http.ResponseWriter, r *http.Request) {
    articles := Articles{
        Article{Title: "Hello", Desc: "Article Description", Content: "Article Content"},
        Article{Title: "Hello 2", Desc: "Article Description", …假设我的网站是http://soccer.com并且我想支持无限数量的子域,例如:
http://cronaldo.soccer.comhttp://messi.soccer.comhttp://neymar.soccer.comhttp://muller.soccer.com我还想要一些保留的子域,例如:
http://admin.soccer.comhttp://help.soccer.com虽然玩家的子域将由相同的逻辑处理,但保留的子域不会。那么我需要 2 个路由或 2 个路由器?
这是我所拥有的:
package main
import (
    "fmt"
    "net/http"
    "log"
    "html/template"
    "strings"
)
type Mux struct {
    http.Handler
}
func (mux Mux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    domainParts := strings.Split(r.Host, ".")
    fmt.Println("Here: " + domainParts[0])
    if domainParts[0] == "admin" {
        // assign route?
        mux.ServeHTTP(w, r)
    } else if domainParts[0] == "help" {
        // assign route?
        mux.ServeHTTP(w, r)
    } else if isSubDomainValid(domainParts[0]) {
        // assign route …我正在尝试使用 mux 并设置一些处理程序。我有以下处理程序
func homePage(w http.ResponseWriter, r *http.Request) {
    // Some code
}
func main() {
    router := mux.NewRouter().StrictSlash(true)
    router.HandleFunc("/", homePage)
    log.Fatal(http.ListenAndServe(":8090", router))
}
有什么方法可以将更多参数传递给处理程序函数,以便我可以执行更多逻辑?我的意思是向homePage名为message. 像这样的东西...
func homePage(w http.ResponseWriter, r *http.Request, message string) {
    // Do some logic with message
    // Rest of code
}
func main() {
    router := mux.NewRouter().StrictSlash(true)
    router.HandleFunc("/", homePage("hello"))
    log.Fatal(http.ListenAndServe(":8090", router))
}
目前的流程似乎可以解决:
process(sel, X)
begin
    -- set all to 0
    mux_out <= (others => zero);
    -- Set input in correct line
    mux_out(to_integer(unsigned(sel))) <= X;
end process;
我将使用 TestBench 测试更多案例并将结果写在这里,再次感谢大家的帮助:)
==== 上一篇文章 ======= 我已经通过以下 Paebbles 示例实现了 DEMUX:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.MATH_REAL.ALL;
use IEEE.NUMERIC_STD.ALL;
entity DeMUX_1toX_N_bits is
  generic (
    PORTS  : POSITIVE  := 4;
    BITS   : POSITIVE  := 8 );
  port (
    sel  : in  STD_LOGIC_VECTOR(integer(ceil(log2(real(PORTS)))) - 1 downto 0);
     X    : in  STD_LOGIC_VECTOR(BITS - 1 downto 0);
    Y    : out …我正在尝试安装 2 个 http 路由器,例如:
    http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
    })
    r := mux.NewRouter()
    r.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "hi, %q", html.EscapeString(r.URL.Path))
    })
    http.Handle("/subpath", r)
    log.Fatal(http.ListenAndServe(":8080", nil))
然而/subpath/foo归来404 page not found。我正在尝试为特定子路径获取特定路由器,但这似乎不起作用。仅当我将其安装在根目录上/并让多路复用器路由器解析整个路径时,它才有效。理想情况下,我希望多路复用器路由器仅处理相对于其自身根的路径。这是可行的吗?