英文:
How to Create http NewServeMux and run with go routine?
问题
我有一个关于http.NewServeMux的问题,我尝试使用go routine运行,但它不起作用。
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
func run() (s *http.Server) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(http.StatusOK)
fmt.Fprint(rw, "under construction")
})
s = &http.Server{
Addr: ":8080",
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
go func() {
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
return
}
func main() {
s := run()
quit := make(chan os.Signal)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("Shutting down server...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer func() {
cancel()
}()
if err := s.Shutdown(ctx); err != nil {
log.Fatal("Server forced to shutdown")
}
log.Println("Server exiting")
}
然后使用以下命令运行:
go run main.go
并访问http://localhost:8080/,总是得到404页面未找到。
你可以如何解决这个问题?
英文:
I have an issue about http.NewServeMux, I've tried to run with go routine but it's doesn't work.
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
func run() (s *http.Server) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(http.StatusOK)
fmt.Fprint(rw, "under construction")
})
s = &http.Server{
Addr: ":8080",
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
go func() {
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
return
}
func main() {
s := run()
quit := make(chan os.Signal)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("Shutting down server...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer func() {
cancel()
}()
if err := s.Shutdown(ctx); err != nil {
log.Fatal("Server forced to shutdown")
}
log.Println("Server exiting")
}
then run with
> go run main.go
And access to http://localhost:8080/ that's always to get.
>404 page not found
How I can solve this issue?
答案1
得分: 0
这是因为你设置了 mux
,但从未对其进行任何操作:
// 在添加默认处理程序后,mux 没有被引用
mux := http.NewServeMux()
mux.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(http.StatusOK)
fmt.Fprint(rw, "正在建设中")
})
将处理程序更改为使用 mux
变量而不是 http.DefaultServeMux
,一切都会按预期工作:
s = &http.Server{
Handler: mux, // 这是重要的一行
Addr: ":8080",
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
英文:
This is because you're setting up mux
but never do anything with it:
// mux is never referenced after you add the default handler
mux := http.NewServeMux()
mux.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(http.StatusOK)
fmt.Fprint(rw, "under construction")
})
Change your handler to use your mux
var instead of http.DefaultServeMux
and everything works as expected:
s = &http.Server{
Handler: mux, // This is the important line
Addr: ":8080",
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论