This is a creation in Article, where the information may have evolved or changed.
Article Source: http://gf.johng.cn/494366
GF Framework provides a very powerful Web server module, supported by Ghttp package, API document address: Godoc.org/github.com/johng-cn/gf/g/net/ghttp.
Hello World!
The usual, let's start with a Hello world:
package mainimport "gitee.com/johng/gf/g/net/ghttp"func main() { s := ghttp.GetServer() s.BindHandler("/", func(r *ghttp.Request){ r.Response.Write("哈喽世界!") }) s.Run()}
This is one of the simplest web Server, it does not support static file processing, only a function, http://127.0.0.1/
when accessed, it will return to "Hello World!" ”。
At any time, you can ghttp.GetServer()
obtain a default Web server object by means of a 单例模式
design, which means that the method is called multiple times and returns the same Web server object.
The run () method performs a listening run of the Web server and, without any additional settings, listens on port 80 by default.
As for the service registration, we'll cover it in a later section, and we'll continue to look at how to create a Web Server that supports static files.
Web Server
Create and run a Web Server that supports static files:
package mainimport "gitee.com/johng/gf/g/net/ghttp"func main() { s := ghttp.GetServer() s.SetIndexFolder(true) s.SetServerRoot("/home/www/") s.Run()}
After the Web server object has been created, we can use Set*
methods to set the properties of the Web server, which we have in the example that involves two property-setting methods:
SetIndexFolder
Used to set whether to allow List of files for the Web Server home directory (default to False);
SetServerRoot
Used to set the home directory of the Web server (the default is null, and at some point the Web server only provides interface services, so the Web server's home directory is not a required parameter);
Web server, by default, does not have any home directory settings, and only the home directory is set up to support access to static files in the home directory. For more property settings, refer to the Ghttp API documentation.
Multi-server support
Ghttp supports multiple Web server runs, let's look at an example:
package mainimport ( "gitee.com/johng/gf/g/net/ghttp")func main() { s1 := ghttp.GetServer("s1") s1.SetPort(8080) s1.SetIndexFolder(true) s1.SetServerRoot("/home/www/static1") go s1.Run() s2 := ghttp.GetServer("s2") s2.SetPort(8081) s2.SetIndexFolder(true) s2.SetServerRoot("/home/www/static2") go s2.Run() select{}}
If you need to support more than one Web server in the same process, you will need to use Goroutine for each Web server to listen asynchronously and keep the main process alive through the select{} statement (and, of course, you can do otherwise).
In addition, you can see that we give ghttp in statements that support multiple Web servers. Getserver passed a different parameter, which is the name of the Web server, before we mentioned that Ghttp's Getserver method takes a singleton design pattern, which is used to identify different Web servers and therefore needs to be unique.
If you need to get the same web Server, pass in the same name. For example, in multiple goroutine, or in a different module, you can pass ghttp. Getserver gets to the same Web server object.
Domain & Multi-domain support
with one Web Server supports multi-domain bindings, and different domains can bind different services.
Let's look at a simple example:
package mainimport "gitee.com/johng/gf/g/net/ghttp"func Hello1(r *ghttp.Request) { r.Response.Write("127.0.0.1: Hello1!")}func Hello2(r *ghttp.Request) { r.Response.Write("localhost: Hello2!")}func main() { s := ghttp.GetServer() s.Domain("127.0.0.1").BindHandler("/", Hello1) s.Domain("localhost").BindHandler("/", Hello2) s.Run()}
We visit http://127.0.0.1/
and http://localhost/
can look at the output of different content.
In addition, the Domain
method supports multiple domain name parameters, separated by the English "," number, for example:
s.Domain("localhost1,localhost2,localhost3").BindHandler("/", Hello2)
The representation of this statement registers the Hello2 method in the specified 3 domain names (localhost1~3) and is not visible to other domain names.
Note that the parameters of the domain method must be the exact domain name and do not support the generic domain name Form , for example: *.johng.cn or. johng.cn is not supported, api.johng.cn or johng.cn is considered the correct domain name parameter.