Getting Started - 图1tip

GoFrame is a modular framework with well-developed infrastructure, where the WebServer module is one of the core modules. We choose Web service development as the entry point to the framework to make it easier for everyone to learn and understand.

The GoFrame framework provides a very powerful WebServer, implemented by the ghttp module. It includes a rich and comprehensive set of components such as Router, Cookie, Session, route registration, configuration management, template engine, cache control, etc., supporting features like hot restart, hot update, multi-domain, multi-port, multi-instance, HTTPS, Rewrite, PProf, and more.

API Documentation:

https://pkg.go.dev/github.com/gogf/gf/v2/net/ghttp

Hello World

As usual, let’s start with a Hello World:

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/net/ghttp"
  5. )
  6. func main() {
  7. s := g.Server()
  8. s.BindHandler("/", func(r *ghttp.Request) {
  9. r.Response.Write("Hello World!")
  10. })
  11. s.Run()
  12. }

This is the simplest service. By default, it does not support static file processing, and only has one function: accessing http://127.0.0.1/ will return Hello World!.

At any time, you can get a default Server object through the g.Server() method, which is designed using the singleton pattern, meaning that calling this method multiple times returns the same Server object. By executing the Run() method, the Server starts listening. Without any additional settings, it listens on port 80 by default.

The route registration will be introduced in subsequent chapters. Let’s see how to create a Server that supports static files.

Static Service

Create and run a WebServer that supports static files:

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. )
  5. func main() {
  6. s := g.Server()
  7. s.SetIndexFolder(true)
  8. s.SetServerRoot("/home/www/")
  9. s.Run()
  10. }

After creating the Server object, we can use the Set* methods to set the Server‘s properties. In this example, two property setting methods are involved:

  1. SetIndexFolder is used to set whether to allow listing the files in the Server‘s root directory (default is false).
  2. SetServerRoot is used to set the Server‘s root directory (similar to the root configuration in nginx, default is empty).

Server does not have any root directory settings by default. Only after setting the root directory does it support accessing static files under the corresponding root directory.

Multiple Port Listening

Server supports multiple port listening at the same time, you just need to set multiple port numbers in the SetPort parameter (of course, for the HTTPS service, we can also use the SetHTTPSPort to set and support listening on multiple port numbers. The introduction to the HTTPS service can be found in the corresponding subsequent chapters).

Let’s see an example:

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/net/ghttp"
  5. )
  6. func main() {
  7. s := g.Server()
  8. s.BindHandler("/", func(r *ghttp.Request){
  9. r.Response.Writeln("go frame!")
  10. })
  11. s.SetPort(8100, 8200, 8300)
  12. s.Run()
  13. }

After executing the above example, visiting the following URLs will yield the expected same result:

  1. http://127.0.0.1:8100/
  2. http://127.0.0.1:8200/
  3. http://127.0.0.1:8300/

Multi-instance Support

Server supports running multiple instances in the same process. Let’s see an example:

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. )
  5. func main() {
  6. s1 := g.Server("s1")
  7. s1.SetPort(8080)
  8. s1.SetIndexFolder(true)
  9. s1.SetServerRoot("/home/www/static1")
  10. s1.Start()
  11. s2 := g.Server("s2")
  12. s2.SetPort(8088)
  13. s2.SetIndexFolder(true)
  14. s2.SetServerRoot("/home/www/static2")
  15. s2.Start()
  16. g.Wait()
  17. }

It can be seen that in the statements supporting multiple Servers, different singleton name parameters are passed to the g.Server method. This parameter is used to identify different Server instances, so it needs to be unique. If you need to obtain the same Server instance, just pass the same name. For example, in multiple goroutines or different modules, you can get the same Server instance through g.Server.

Domain Binding

Server supports multi-domain binding, and different domains can bind to different services.

Let’s see a simple example:

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/net/ghttp"
  5. )
  6. func Hello1(r *ghttp.Request) {
  7. r.Response.Write("127.0.0.1: Hello1!")
  8. }
  9. func Hello2(r *ghttp.Request) {
  10. r.Response.Write("localhost: Hello2!")
  11. }
  12. func main() {
  13. s := g.Server()
  14. s.Domain("127.0.0.1").BindHandler("/", Hello1)
  15. s.Domain("localhost").BindHandler("/", Hello2)
  16. s.Run()
  17. }

Accessing http://127.0.0.1/ and http://localhost/ will display different outputs.

Additionally, the Domain method supports multiple domain parameters, separated by a comma, such as:

  1. s.Domain("localhost1,localhost2,localhost3").BindHandler("/", Hello2)

This statement registers the Hello2 method to the specified 3 domains (localhost1~3), not visible to other domains.

Note that: the parameters of the Domain method must be precise domain names and do not support wildcard forms, such as *.goframe.org or .goframe.org, which are not supported. api.goframe.org or goframe.org are considered correct domain parameters.

Routing Features

Server provides very excellent routing features. Let’s first see a simple example:

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/net/ghttp"
  4. "github.com/gogf/gf/v2/frame/g"
  5. )
  6. func main() {
  7. s := g.Server()
  8. s.BindHandler("/{class}-{course}/:name/*act", func(r *ghttp.Request) {
  9. r.Response.Writef(
  10. "%v %v %v %v",
  11. r.Get("class"),
  12. r.Get("course"),
  13. r.Get("name"),
  14. r.Get("act"),
  15. )
  16. })
  17. s.SetPort(8199)
  18. s.Run()
  19. }

This is an example of a mixed routing rule, used to display a specific class, subject, student, and corresponding action. After running, we can see the test result by visiting this address: http://127.0.0.1:8199/class3-math/john/score. On the page, you can see that the routing rules are parsed one by one, and the business layer can process the corresponding business logic according to the parsed parameters. For specific route registration management, please see the subsequent Router - Route Patterns chapter.

Configuration Management

The core components of GoFrame implement convenient configuration management features, allowing component functionality configuration through configuration file modifications. In most scenarios, we recommend using configuration files to manage component configurations. For Server configurations, see the Configuration chapter.

Graceful Restart

Server has built-in support for graceful restart features. Detailed introduction can be found in the Graceful Restart chapter.

HTTPS Support

Server supports HTTPS services and also supports providing HTTP&HTTPS services in a single process. Detailed HTTPS introduction can be found in the HTTPS & TLS chapter.

More Features

For more features and capabilities, please continue reading the subsequent chapters.