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

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

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

问题

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

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

r := mux.NewRouter()

http.HandleFunc("/dependencies/", DependencyHandler) //文件服务

http.HandleFunc("/portals/", PortalsHandler) //文件服务

r.HandleFunc("/registeruser", UserRegistrationHandler)

r.HandleFunc("/deleteuser/{username}", DeleteUserHandler)

http.Handle("/",r)

s := &http.Server{
	Addr:           ":" + strconv.Itoa(serverConfigs.HttpPort),
	Handler:        nil,
	ReadTimeout:    time.Duration(serverConfigs.ReadTimeOut) * time.Second,
	WriteTimeout:   time.Duration(serverConfigs.WriteTimeOut) * time.Second,
	MaxHeaderBytes: 1 << 20,
}
英文:

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?.

r := mux.NewRouter()

http.HandleFunc(&quot;/dependencies/&quot;, DependencyHandler) //file serving

http.HandleFunc(&quot;/portals/&quot;, PortalsHandler) //file serving

r.HandleFunc(&quot;/registeruser&quot;, UserRegistrationHandler)

r.HandleFunc(&quot;/deleteuser/{username}&quot;, DeleteUserHandler)

http.Handle(&quot;/&quot;,r)

s := &amp;http.Server{
	Addr:           &quot;:&quot; + strconv.Itoa(serverConfigs.HttpPort),
	Handler:        nil,
	ReadTimeout:    time.Duration(serverConfigs.ReadTimeOut) * time.Second,
	WriteTimeout:   time.Duration(serverConfigs.WriteTimeOut) * time.Second,
	MaxHeaderBytes: 1 &lt;&lt; 20,
}

答案1

得分: 2

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

package main

import (
	"github.com/gorilla/mux"
	"log"
	"net/http"
)

func main() {
	r := mux.NewRouter()
	r.PathPrefix("/portals/").Handler(http.StripPrefix("/portals/", http.FileServer(http.Dir("./portals/"))))
	r.PathPrefix("/dependencies/").Handler(http.StripPrefix("/dependencies/", http.FileServer(http.Dir("./dependencies/"))))
	r.HandleFunc("/registeruser", UserRegistrationHandler)
	r.HandleFunc("/deleteuser/{username}", DeleteUserHandler)
	http.Handle("/", r)

	log.Println("Listening...")
	http.ListenAndServe(":8000", r)
}
英文:

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

package main

import (
	&quot;github.com/gorilla/mux&quot;
	&quot;log&quot;
	&quot;net/http&quot;
)

func main() {
	r := mux.NewRouter()
	r.PathPrefix(&quot;/portals/&quot;).Handler(http.StripPrefix(&quot;/portals/&quot;, http.FileServer(http.Dir(&quot;./portals/&quot;))))
	r.PathPrefix(&quot;/dependencies/&quot;).Handler(http.StripPrefix(&quot;/dependencies/&quot;, http.FileServer(http.Dir(&quot;./dependencies/&quot;))))
	r.HandleFunc(&quot;/registeruser&quot;, UserRegistrationHandler)
	r.HandleFunc(&quot;/deleteuser/{username}&quot;, DeleteUserHandler)
	http.Handle(&quot;/&quot;, r)

	log.Println(&quot;Listening...&quot;)
	http.ListenAndServe(&quot;:8000&quot;, r)
}

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:

确定