如何将Go语言服务器同时用作文件服务器和后端逻辑服务器?

huangapple go评论106阅读模式
英文:

How to use go-lang server as both file server and back-end logic server

问题

在PHP中,我们可以托管应用程序并使用相同的服务器和端口来处理后端逻辑调用。

我在Go语言中使用了以下方法来实现这一点。有没有更好的方法来实现这个?

  1. r := mux.NewRouter()
  2. http.HandleFunc("/dependencies/", DependencyHandler) //文件服务
  3. http.HandleFunc("/portals/", PortalsHandler) //文件服务
  4. r.HandleFunc("/registeruser", UserRegistrationHandler)
  5. r.HandleFunc("/deleteuser/{username}", DeleteUserHandler)
  6. http.Handle("/",r)
  7. s := &http.Server{
  8. Addr: ":" + strconv.Itoa(serverConfigs.HttpPort),
  9. Handler: nil,
  10. ReadTimeout: time.Duration(serverConfigs.ReadTimeOut) * time.Second,
  11. WriteTimeout: time.Duration(serverConfigs.WriteTimeOut) * time.Second,
  12. MaxHeaderBytes: 1 << 20,
  13. }
英文:

In php we can host application and use the same server,port to handle the back-end logic calls.

I've used the following way to achieve this in go-lang. Is there a better way to achieve this?.

  1. r := mux.NewRouter()
  2. http.HandleFunc(&quot;/dependencies/&quot;, DependencyHandler) //file serving
  3. http.HandleFunc(&quot;/portals/&quot;, PortalsHandler) //file serving
  4. r.HandleFunc(&quot;/registeruser&quot;, UserRegistrationHandler)
  5. r.HandleFunc(&quot;/deleteuser/{username}&quot;, DeleteUserHandler)
  6. http.Handle(&quot;/&quot;,r)
  7. s := &amp;http.Server{
  8. Addr: &quot;:&quot; + strconv.Itoa(serverConfigs.HttpPort),
  9. Handler: nil,
  10. ReadTimeout: time.Duration(serverConfigs.ReadTimeOut) * time.Second,
  11. WriteTimeout: time.Duration(serverConfigs.WriteTimeOut) * time.Second,
  12. MaxHeaderBytes: 1 &lt;&lt; 20,
  13. }

答案1

得分: 2

你可以使用mux路由器来提供静态文件和实现后端逻辑处理程序。可以使用PathPrefix()和StripPrefix()来实现:

  1. package main
  2. import (
  3. "github.com/gorilla/mux"
  4. "log"
  5. "net/http"
  6. )
  7. func main() {
  8. r := mux.NewRouter()
  9. r.PathPrefix("/portals/").Handler(http.StripPrefix("/portals/", http.FileServer(http.Dir("./portals/"))))
  10. r.PathPrefix("/dependencies/").Handler(http.StripPrefix("/dependencies/", http.FileServer(http.Dir("./dependencies/"))))
  11. r.HandleFunc("/registeruser", UserRegistrationHandler)
  12. r.HandleFunc("/deleteuser/{username}", DeleteUserHandler)
  13. http.Handle("/", r)
  14. log.Println("Listening...")
  15. http.ListenAndServe(":8000", r)
  16. }
英文:

You can serve static files and implement your backend logic handlers with mux router. Use PathPrefix() and StripPrefix() for that:

  1. package main
  2. import (
  3. &quot;github.com/gorilla/mux&quot;
  4. &quot;log&quot;
  5. &quot;net/http&quot;
  6. )
  7. func main() {
  8. r := mux.NewRouter()
  9. r.PathPrefix(&quot;/portals/&quot;).Handler(http.StripPrefix(&quot;/portals/&quot;, http.FileServer(http.Dir(&quot;./portals/&quot;))))
  10. r.PathPrefix(&quot;/dependencies/&quot;).Handler(http.StripPrefix(&quot;/dependencies/&quot;, http.FileServer(http.Dir(&quot;./dependencies/&quot;))))
  11. r.HandleFunc(&quot;/registeruser&quot;, UserRegistrationHandler)
  12. r.HandleFunc(&quot;/deleteuser/{username}&quot;, DeleteUserHandler)
  13. http.Handle(&quot;/&quot;, r)
  14. log.Println(&quot;Listening...&quot;)
  15. http.ListenAndServe(&quot;:8000&quot;, r)
  16. }

huangapple
  • 本文由 发表于 2015年8月30日 00:09:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/32288048.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定