webserv

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 12, 2024 License: MIT Imports: 10 Imported by: 4

README

build coverage goreport Docs

webserv

Thin web server stub.

Given a listen address, certificate directory, user name and data directory:

  • If certificate directory is not blank, reads fullchain.pem and privkey.pem from it.
  • If the listen address does not specify a port, default port depends on initial user privileges and if we have a certificate.
  • Starts listening on the address and port.
  • If user name is given, switch to that user.
  • If data directory is given, create it if needed and then switch current directory to it.

Documentation

Index

Examples

Constants

View Source
const (
	FullchainPem = "fullchain.pem"
	PrivkeyPem   = "privkey.pem"
)

Variables

This section is empty.

Functions

func BecomeUser

func BecomeUser(userName string) (err error)

func DefaultDataDir

func DefaultDataDir(dataDir, defaultsuffix string) (string, error)

DefaultDataDir returns dataDir if not empty, otherwise the joined path of the default user configuration and defaultsuffix.

func Listener

func Listener(listenAddr, certDir string) (l net.Listener, absCertDir, listenUrl string, err error)

func LoadCert

func LoadCert(certDir string) (cert *tls.Certificate, absCertDir string, err error)

func LogInfo

func LogInfo(logger any, msg string, args ...any)

func UseDataDir

func UseDataDir(dataDir string) (string, error)

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

func (cfg *Config) Apply(logger any) (l net.Listener, err error)

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL