Add timeouts
Adds read and write timeout configuration settings and allows to configure a maximum acceptable header size.
This commit is contained in:
parent
cec58caffe
commit
03cb62aba7
|
@ -13,13 +13,21 @@ type Config struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConfigSettings struct {
|
type ConfigSettings struct {
|
||||||
BindAddress string `yaml:"bind"` // Bind address for the webserver
|
BindAddress string `yaml:"bind"` // Bind address for the webserver
|
||||||
UID int `yaml:"uid"` // Custom user ID or 0, if not being used
|
UID int `yaml:"uid"` // Custom user ID or 0, if not being used
|
||||||
GID int `yaml:"gid"` // Custom group ID or 0, if not being used
|
GID int `yaml:"gid"` // Custom group ID or 0, if not being used
|
||||||
|
ReadTimeout int `yaml:"readtimeout"` // Timeout for reading the whole request
|
||||||
|
WriteTimeout int `yaml:"writetimeout"` // Timeout for writing the whole response
|
||||||
|
MaxHeaderBytes int `yaml:"maxheadersize"` // Maximum size of the receive body
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cf *Config) SetDefaults() {
|
func (cf *Config) SetDefaults() {
|
||||||
cf.Settings.BindAddress = ":2088"
|
cf.Settings.BindAddress = ":2088"
|
||||||
|
cf.Settings.UID = 0
|
||||||
|
cf.Settings.GID = 0
|
||||||
|
cf.Settings.ReadTimeout = 0
|
||||||
|
cf.Settings.WriteTimeout = 0
|
||||||
|
cf.Settings.MaxHeaderBytes = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check performs sanity checks on the config
|
// Check performs sanity checks on the config
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var cf Config
|
var cf Config
|
||||||
|
@ -122,7 +123,15 @@ func main() {
|
||||||
awaitTerminationSignal()
|
awaitTerminationSignal()
|
||||||
|
|
||||||
log.Printf("Launching webserver on %s", cf.Settings.BindAddress)
|
log.Printf("Launching webserver on %s", cf.Settings.BindAddress)
|
||||||
log.Fatal(http.ListenAndServe(cf.Settings.BindAddress, nil))
|
server := &http.Server{
|
||||||
|
Addr: cf.Settings.BindAddress,
|
||||||
|
ReadTimeout: time.Duration(cf.Settings.ReadTimeout) * time.Second,
|
||||||
|
WriteTimeout: time.Duration(cf.Settings.WriteTimeout) * time.Second,
|
||||||
|
MaxHeaderBytes: cf.Settings.MaxHeaderBytes,
|
||||||
|
}
|
||||||
|
err := server.ListenAndServe()
|
||||||
|
log.Fatal(err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// create a http handler function from the given hook
|
// create a http handler function from the given hook
|
||||||
|
|
|
@ -8,6 +8,9 @@ settings:
|
||||||
# This is a known issue, see https://codeberg.org/grisu48/weblug/issues/9
|
# This is a known issue, see https://codeberg.org/grisu48/weblug/issues/9
|
||||||
uid: 0 # run under specified user id
|
uid: 0 # run under specified user id
|
||||||
gid: 0 # run under specified group id
|
gid: 0 # run under specified group id
|
||||||
|
readtimeout: 10 # if set, maximum number of seconds to receive the full request
|
||||||
|
writetimeout: 10 # if set, maximum number of seconds to send the full response
|
||||||
|
maxheadersize: 4096 # maximum header size
|
||||||
|
|
||||||
# hook definitions. A hook needs to define the HTTP endpoint ("route") and the command
|
# hook definitions. A hook needs to define the HTTP endpoint ("route") and the command
|
||||||
# See the following examples for more possible options.
|
# See the following examples for more possible options.
|
||||||
|
|
Loading…
Reference in a new issue