Server - lucasmenendez/shgf GitHub Wiki
Initializing server
shgf.Server
instance allows the developer to configure the behavior of the server and register new routes to handle it. For more information about shgf.Server
check out the documentation here.
server, err := shgf.New(shgf.LocalConf())
if err != nil {
fmt.Println(err)
}
// new routes go here
s.Listen()
Server Configuration
Configuration instance allows setting some parameters to define server behavior and properties. For an advance configuration, read full docs about shgf.Config
into the GoDoc article.
Local Host configuration
shgf.LocalConf()
method returns the default configuration to deploy a localhost server. It is configured with debug enabled, and serve into 127.0.0.1:8080
.
/*
var conf = &shgf.Config{
Hostname: "127.0.0.1",
Port: 8080,
PortTLS: 8081,
Debug: true,
}
*/
var conf = shgf.LocalConf()
Basic HTTP configuration
shgf.BasicConf
method receives server hostname and port number and returns the configuration to deploy a simple HTTP server with that parameters.
/*
var conf = &shgf.Config{
Hostname: "0.0.0.0",
Port: 8080,
}
*/
var conf = shgf.BasicConf("0.0.0.0", 8080)
Enable TLS
To enable TLS, append a TLS port number and a cert
and key
files path to the any shgf.Config
:
var conf = shgf.BasicConf("0.0.0.0", 8080)
conf.TLSPort = 8081
conf.TLSCert = "/path/to/cert.pem"
conf.TLSKey = "/path/to/key.pem"
Enable HTTP/2
To enable HTTP2, TLS must be configured.
var conf = &shgf.Config{
Hostname: "127.0.0.1",
Port: 8080,
PortTLS: 8081,
TLSCert: "/path/to/cert.pem",
TLSKey: "/path/to/key.pem",
HTTP2: true,
}