Documentation
¶
Index ¶
- Constants
- func BecomeUser(userName string) (err error)
- func DefaultDataDir(dataDir, defaultsuffix string) (string, error)
- func Listener(listenAddr, certDir string) (l net.Listener, absCertDir, listenUrl string, err error)
- func LoadCert(certDir string) (cert *tls.Certificate, absCertDir string, err error)
- func LogInfo(logger any, msg string, args ...any)
- func UseDataDir(dataDir string) (string, error)
- type Config
Examples ¶
Constants ¶
const ( FullchainPem = "fullchain.pem" PrivkeyPem = "privkey.pem" )
Variables ¶
This section is empty.
Functions ¶
func BecomeUser ¶
func DefaultDataDir ¶
DefaultDataDir returns dataDir if not empty, otherwise the joined path of the default user configuration and defaultsuffix.
func UseDataDir ¶
UseDataDir expands environment variables in dataDir, transforms it into an absolute path, creates it if it does not exist and finally changes current directory to that path.
Returns the final path.
Types ¶
type Config ¶
type Config struct { Listen string // optional specific address (and/or port) to listen on CertDir string // if set, directory to look for fullchain.pem and privkey.pem User string // if set, user to switch to after opening listening port DataDir string // if set, ensure this directory exists and switch to it ListenURL string // after Apply called, set to an URL we listen on }
func (*Config) Apply ¶
Apply loads certificates if CertDir is set, starts a net.Listener (TLS or normal) on the Listen address and port, if User is set it switches to that user and group, if DataDir is set, creates that directory if needed and switches to that.
On a non-error return, CertDir and DataDir will be absoulte paths or empty, and ListenURL will be a printable and connectable URL like "http://localhost:80".
Example ¶
package main import ( "fmt" "os" "path" "testing" "github.com/linkdata/webserv" ) func withCertFiles(t *testing.T, fn func(destdir string)) { t.Helper() certPem := []byte(`-----BEGIN CERTIFICATE----- MIIBhTCCASugAwIBAgIQIRi6zePL6mKjOipn+dNuaTAKBggqhkjOPQQDAjASMRAw DgYDVQQKEwdBY21lIENvMB4XDTE3MTAyMDE5NDMwNloXDTE4MTAyMDE5NDMwNlow EjEQMA4GA1UEChMHQWNtZSBDbzBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABD0d 7VNhbWvZLWPuj/RtHFjvtJBEwOkhbN/BnnE8rnZR8+sbwnc/KhCk3FhnpHZnQz7B 5aETbbIgmuvewdjvSBSjYzBhMA4GA1UdDwEB/wQEAwICpDATBgNVHSUEDDAKBggr BgEFBQcDATAPBgNVHRMBAf8EBTADAQH/MCkGA1UdEQQiMCCCDmxvY2FsaG9zdDo1 NDUzgg4xMjcuMC4wLjE6NTQ1MzAKBggqhkjOPQQDAgNIADBFAiEA2zpJEPQyz6/l Wf86aX6PepsntZv2GYlA5UpabfT2EZICICpJ5h/iI+i341gBmLiAFQOyTDT+/wQc 6MF9+Yw1Yy0t -----END CERTIFICATE-----`) keyPem := []byte(`-----BEGIN EC PRIVATE KEY----- MHcCAQEEIIrYSSNQFaA2Hwf1duRSxKtLYX5CB04fSeQ6tF1aY/PuoAoGCCqGSM49 AwEHoUQDQgAEPR3tU2Fta9ktY+6P9G0cWO+0kETA6SFs38GecTyudlHz6xvCdz8q EKTcWGekdmdDPsHloRNtsiCa697B2O9IFA== -----END EC PRIVATE KEY-----`) destdir, err := os.MkdirTemp("", "weblistener") if err == nil { defer func() { if err := os.RemoveAll(destdir); err != nil { t.Error(err) } }() if err = os.WriteFile(path.Join(destdir, webserv.FullchainPem), certPem, 0640); err == nil { if err = os.WriteFile(path.Join(destdir, webserv.PrivkeyPem), keyPem, 0640); err == nil { fn(destdir) } } } if err != nil { t.Error(err) } } func main() { var cfg webserv.Config if l, err := cfg.Apply(os.Stdout); err == nil { defer l.Close() fmt.Print("OK") } else { fmt.Print(err) } }
Output: OK