# Web Module Configuration
Here we mainly discuss the individual configuration items of the Web module, and how to use the configuration module, you can refer toConfigure Module
# Default Configuration Files
BConfig is the default web configuration instance and Beego will parse the conf/app.conf file by default.
Example:
appname = beepkg
httpaddr = "127.0.0.1"
httpport = 9090
runmode ="dev"
autorender = false
recoverpanic = false
viewspath = "myview"
2
3
4
5
6
7
Note here that the configuration items are not named using camel naming, but are kept in all lowercase. However, different configuration formats can support both camel naming and underscore naming. For example, the JSON configuration format is only related to the value of the json tag inside your structure.
# Specify Configuration Files
const ConfigFile = "./my-custom.conf"
func main() {
err := web.LoadAppConfig("ini", ConfigFile)
if err != nil {
logs.Critical("An error occurred:", err)
panic(err)
}
val, _ := web.AppConfig.String("name")
logs.Info("load config name is", val)
}
2
3
4
5
6
7
8
9
10
11
12
You can call LoadAppConfig multiple times, if the later file conflicts with the previous key, then the latest loaded one will be the latest value.
# Code As Configuration
When the application is simple, or when the configuration items have the same value in different environments, you can consider using code for configuration. It has the advantage of being able to fully enjoy compile-time type checking, but of course the disadvantage is also very obvious, i.e., it is not flexible enough and cannot be dynamically modified at runtime.
The configuration of the Web module in Beego is defined in the structure:
type Config struct {
// ...
}
2
3
Example:
web.BConfig.AppName="My-App"
# Configuration Items
The detailed field meanings can be directly referred to the source code Config definition (opens new window)
# App config
AppName
The application name. By default this is "Beego". If the application is created by
bee new project_nameit will be set to project_name.beego.BConfig.AppName = "beego"RunMode
The application mode. By default this is set to
dev. Other valid modes includeprodandtest. Indevmode user friendly error pages will be shown. Inprodmode user friendly error pages will not be rendered.beego.BConfig.RunMode = "dev"RouterCaseSensitive
Set case sensitivity for the router. By default this value is true.
beego.BConfig.RouterCaseSensitive = trueServerName
The Beego server name. By default this name is
beego.beego.BConfig.ServerName = "beego"RecoverPanic
When active the application will recover from exceptions without exiting the application. By default this is set to true.
beego.BConfig.RecoverPanic = trueCopyRequestBody
Toggle copying of raw request body in context. By default this is false except for GET, HEAD or file uploading.
beego.BConfig.CopyRequestBody = falseEnableGzip
Enable Gzip. By default this is false. If Gzip is enabled the output of templates will be compressed by Gzip or zlib according to the
Accept-Encodingsetting of the browser.beego.BConfig.EnableGzip = falseFurther properties can be configured as below:
gzipCompressLevel = 9Sets the compression level used for deflate compression(0-9). By default is 9 (best speed).gzipMinLength = 256Original content will only be compressed if length is either unknown or greater than gzipMinLength. The default length is 20B.includedMethods = get;postList of HTTP methods to compress. By default only GET requests are compressed.MaxMemory
Sets the memory cache size for file uploading. By default this is
1 << 26(64M).beego.BConfig.MaxMemory = 1 << 26EnableErrorsShow
Toggles the display of error messages. By default this is True.
beego.BConfig.EnableErrorsShow = trueEnableErrorsRender
Toggles rendering error messages. By default this is set to True. User friendly error pages will not be rendered even in dev
RunModeif this value is false.
# Web config
AutoRender
Enable auto render. By default this is True. This value should be set to false for API applications, as there is no need to render templates.
beego.BConfig.WebConfig.AutoRender = trueEnableDocs
Enable Docs. By default this is False.
beego.BConfig.WebConfig.EnableDocs = falseFlashName
Sets the Flash Cookie name. By default this is
BEEGO_FLASH.beego.BConfig.WebConfig.FlashName = "BEEGO_FLASH"FlashSeperator
Set the Flash data separator. By default this is
BEEGOFLASH.beego.BConfig.WebConfig.FlashSeperator = "BEEGOFLASH"DirectoryIndex
Enable listing of the static directory. By default this is False and will return a 403 error.
beego.BConfig.WebConfig.DirectoryIndex = falseStaticDir
Sets the static file dir(s). By default this is
static.Single dir,
StaticDir = download. Same asbeego.SetStaticPath("/download","download")Multiple dirs,
StaticDir = download:down download2:down2. Same asbeego.SetStaticPath("/download","down")andbeego.SetStaticPath("/download2","down2")
beego.BConfig.WebConfig.StaticDir = map[string]string{"download":"download"}StaticExtensionsToGzip
Sets a list of file extensions that will support compression by Gzip. The formats
.cssand.jsare supported by default.beego.BConfig.WebConfig.StaticExtensionsToGzip = []string{".css", ".js"}Same as in config file StaticExtensionsToGzip = .css, .js
TemplateLeft
Left mark of the template,
{{by default.beego.BConfig.WebConfig.TemplateLeft = "{{"TemplateRight
Right mark of the template,
}}by default.beego.BConfig.WebConfig.TemplateRight = "}}"ViewsPath
Set the location of template files. This is set to
viewsby default.beego.BConfig.WebConfig.ViewsPath = "views"EnableXSRF Enable XSRF
beego.BConfig.WebConfig.EnableXSRF = falseXSRFKEY
Set the XSRF key. By default this is
beegoxsrf.beego.BConfig.WebConfig.XSRFKEY = "beegoxsrf"XSRFExpire
Set the XSRF expire time. By default this is set to
0.beego.BConfig.WebConfig.XSRFExpire = 0CommentRouterPath
Beego scan
CommentRouterPathto auto generate router, the default value iscontrollers。beego.BConfig.WebConfig.CommentRouterPath = "controllers"
# HTTP Server config
Graceful
Enable graceful shutdown. By default this is False.
beego.BConfig.Listen.Graceful = falseServerTimeOut
Set the http timeout. By default thi is '0', no timeout.
beego.BConfig.Listen.ServerTimeOut = 0ListenTCP4
Set the address type. default is tcp6 but we can set it to true to force use TCP4.
`beego.BConfig.Listen.ListenTCP4 = true`
EnableHTTP
Enable HTTP listen. By default this is set to True.
beego.BConfig.Listen.EnableHTTP = trueHTTPAddr
Set the address the app listens to. By default this value is empty and the app will listen to all IPs.
beego.BConfig.Listen.HTTPAddr = ""HTTPPort
Set the port the app listens on. By default this is 8080
beego.BConfig.Listen.HTTPPort = 8080EnableHTTPS
Enable HTTPS. By default this is False. When enabled
HTTPSCertFileandHTTPSKeyFilemust also be set.beego.BConfig.Listen.EnableHTTPS = falseHTTPSAddr
Set the address the app listens to. Default is empty and the app will listen to all IPs.
beego.BConfig.Listen.HTTPSAddr = ""HTTPSPort
Set the port the app listens on. By default this is 10443
beego.BConfig.Listen.HTTPSPort = 10443HTTPSCertFile
Set the SSL cert path. By default this value is empty.
beego.BConfig.Listen.HTTPSCertFile = "conf/ssl.crt"HTTPSKeyFile
Set the SSL key path. By default this value is empty.
beego.BConfig.Listen.HTTPSKeyFile = "conf/ssl.key"EnableAdmin
Enable supervisor module. By default this is False.
beego.BConfig.Listen.EnableAdmin = falseAdminAddr
Set the address the admin app listens to. By default this is blank and the app will listen to any IP.
beego.BConfig.Listen.AdminAddr = ""AdminPort
Set the port the admin app listens on. By default this is 8088.
beego.BConfig.Listen.AdminPort = 8088EnableFcgi
Enable fastcgi. By default this is False.
beego.BConfig.Listen.EnableFcgi = falseEnableStdIo
Enable fastcgi standard I/O or not. By default this is False.
beego.BConfig.Listen.EnableStdIo = false
# Session config
SessionOn
Enable session. By default this is False.
beego.BConfig.WebConfig.Session.SessionOn = falseSessionProvider
Set the session provider. By default this is
memory.beego.BConfig.WebConfig.Session.SessionProvider = "memory"SessionName
Set the session cookie name stored in the browser. By default this is
beegosessionID.beego.BConfig.WebConfig.Session.SessionName = "beegosessionID"SessionGCMaxLifetime
Set the session expire time. By default this is 3600s.
beego.BConfig.WebConfig.Session.SessionGCMaxLifetime = 3600SessionProviderConfig
Set the session provider config. Different providers can require different config settings. Please see session for more information.
SessionCookieLifeTime
Set the valid expiry time of the cookie in browser for session. By default this is 3600s.
beego.BConfig.WebConfig.Session.SessionCookieLifeTime = 3600SessionAutoSetCookie
Enable SetCookie. By default this is True.
beego.BConfig.WebConfig.Session.SessionAutoSetCookie = trueSessionDomain
Set the session cookie domain. By default this is empty.
beego.BConfig.WebConfig.Session.SessionDomain = ""
# Log config
See [logs module](en-US/module/logs.md) for more information.
AccessLogs
Enable output access logs. By default these logs will not be output under 'prod' mode.
beego.BConfig.Log.AccessLogs = falseFileLineNum
Toggle printing line numbers. By default this is True. This config is not supported in config file.
beego.BConfig.Log.FileLineNum = trueOutputs
Log outputs config. This config is not supported in config file.
beego.BConfig.Log.Outputs = map[string]string{"console": ""}or
beego.BConfig.Log.Outputs["console"] = ""