Configuration Object

Configuration object definition: https://pkg.go.dev/github.com/gogf/gf/v2/net/ghttp#ServerConfig

Configuration Methods

Method list: https://pkg.go.dev/github.com/gogf/gf/v2/net/ghttp#Server

Brief description:

  1. You can set it using SetConfig and SetConfigWithMap.
  2. Specific configurations can also be set using the Set*/Enable* methods of the Server object.
  3. The main thing to note is that the configuration items cannot be modified after the Server executes Start to prevent concurrency safety issues.

SetConfigWithMap Method

We can use the SetConfigWithMap method to set/modify specific configurations of the Server through Key-Value pairs, while the remaining configurations can use the default settings. The name of the Key is the attribute name in the ServerConfig struct, and it is case insensitive. Words can also be connected using -/_/space symbols. For details, refer to the Type Conversion - Struct chapter.

Simple example:

  1. s := g.Server()
  2. s.SetConfigWithMap(g.Map{
  3. "Address": ":80",
  4. "ServerRoot": "/var/www/MyServerRoot",
  5. })
  6. s.Run()

The key name ServerRoot can also be used as serverRoot, server-root, server_root, or server root, and other configuration properties follow this rule.

A more complete example:

  1. s := g.Server()
  2. s.SetConfigWithMap(g.Map{
  3. "address": ":80",
  4. "serverRoot": "/var/www/Server",
  5. "indexFiles": g.Slice{"index.html", "main.html"},
  6. "accessLogEnabled": true,
  7. "errorLogEnabled": true,
  8. "pprofEnabled": true,
  9. "logPath": "/var/log/ServerLog",
  10. "sessionIdName": "MySessionId",
  11. "sessionPath": "/tmp/MySessionStoragePath",
  12. "sessionMaxAge": 24 * time.Hour,
  13. "dumpRouterMap": false,
  14. })
  15. s.Run()