Skip to main content
Version: 2.8.x(Latest)
tip

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:

package main

import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
)

func main() {
s := g.Server()
s.BindHandler("/", func(r *ghttp.Request) {
r.Response.Write("Hello World!")
})
s.Run()
}

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:

package main

import (
"github.com/gogf/gf/v2/frame/g"
)

func main() {
s := g.Server()
s.SetIndexFolder(true)
s.SetServerRoot("/home/www/")
s.Run()
}

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:

package main

import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
)

func main() {
s := g.Server()
s.BindHandler("/", func(r *ghttp.Request){
r.Response.Writeln("go frame!")
})
s.SetPort(8100, 8200, 8300)
s.Run()
}

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

http://127.0.0.1:8100/
http://127.0.0.1:8200/
http://127.0.0.1:8300/

Multi-instance Support

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

package main

import (
"github.com/gogf/gf/v2/frame/g"
)

func main() {
s1 := g.Server("s1")
s1.SetPort(8080)
s1.SetIndexFolder(true)
s1.SetServerRoot("/home/www/static1")
s1.Start()

s2 := g.Server("s2")
s2.SetPort(8088)
s2.SetIndexFolder(true)
s2.SetServerRoot("/home/www/static2")
s2.Start()

g.Wait()
}

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:

package main

import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/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 := g.Server()
s.Domain("127.0.0.1").BindHandler("/", Hello1)
s.Domain("localhost").BindHandler("/", Hello2)
s.Run()
}

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:

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:

package main

import (
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/frame/g"
)

func main() {
s := g.Server()
s.BindHandler("/{class}-{course}/:name/*act", func(r *ghttp.Request) {
r.Response.Writef(
"%v %v %v %v",
r.Get("class"),
r.Get("course"),
r.Get("name"),
r.Get("act"),
)
})
s.SetPort(8199)
s.Run()
}

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.