all repos — homestead @ 5ce60eee31c403537efd6ddb20308a3652a928b2

Code for my website

cmd/server/main.go (view raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main

import (
	"context"
	"fmt"
	"net"
	"net/url"
	"os"
	"os/signal"
	"slices"
	"strconv"
	"strings"

	"go.alanpearce.eu/website/internal/config"
	"go.alanpearce.eu/website/internal/server"
	"go.alanpearce.eu/website/internal/website"
	"go.alanpearce.eu/x/log"

	"github.com/ardanlabs/conf/v3"
	"gitlab.com/tozd/go/errors"
)

type Options struct {
	Development    bool   `conf:"default:false,flag:dev"`
	Source         string `conf:"default:../website"`
	Destination    string `conf:"default:public"`
	Redirect       bool   `conf:"default:true"`
	Port           int    `conf:"default:8080,short:p"`
	TLS            bool   `conf:"default:false"`
	TLSPort        int    `conf:"default:8443"`
	ListenAddress  string `conf:"default:localhost"`
	Domains        string `conf:"default:localhost"`
	ACMEIssuer     string
	ACMEIssuerCert string
}

func main() {
	options := &Options{}
	help, err := conf.Parse("", options)
	if err != nil {
		if errors.Is(err, conf.ErrHelpWanted) {
			fmt.Println(help)
			os.Exit(1)
		}
		panic("parsing runtime configuration" + err.Error())
	}
	log := log.Configure(!options.Development)

	cfg, err := config.GetConfig(options.Source, log)
	if err != nil {
		log.Fatal("could not read config", "error", err)
	}

	// Domains?
	webOpts := &website.Options{
		Source:      options.Source,
		Destination: options.Destination,
		Redirect:    options.Redirect,
		Development: options.Development,
		Config:      cfg,
	}

	if options.Development {
		tmpdir, err := os.MkdirTemp("", "website")
		if err != nil {
			log.Fatal("could not create temporary directory", "error", err)
		}
		log.Info("using temporary directory", "dir", tmpdir)
		defer os.RemoveAll(tmpdir)
		webOpts.Destination = tmpdir

		cfg.CSP.ScriptSrc = slices.Insert(cfg.CSP.ScriptSrc, 0, "'unsafe-inline'")
		cfg.CSP.ConnectSrc = slices.Insert(cfg.CSP.ConnectSrc, 0, "'self'")
		if options.Domains != "" {
			cfg.Domains = strings.Split(options.Domains, ",")
		} else {
			cfg.Domains = []string{options.ListenAddress}
		}
		cfg.BaseURL = mkBaseURL(options, cfg)
	}

	serverOpts := &server.Options{
		Development:    options.Development,
		ListenAddress:  options.ListenAddress,
		Port:           options.Port,
		TLS:            options.TLS,
		TLSPort:        options.TLSPort,
		ACMEIssuer:     options.ACMEIssuer,
		ACMEIssuerCert: options.ACMEIssuerCert,
		Config:         cfg,
	}

	sv, err := server.New(serverOpts, log.Named("server"))
	if err != nil {
		log.Error("could not create server", "error", err)

		return
	}

	website, err := website.New(webOpts, log.Named("website"))
	if err != nil {
		log.Error("could not initialise website", "error", err)

		return
	}

	sv.HostApp(website.App)
	if options.Redirect {
		sv.HostFallbackApp(website.MakeRedirectorApp())
	}

	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
	defer cancel()

	go func() {
		err = sv.Start()
		if err != nil {
			// Error starting or closing listener:
			log.Fatal("error starting server", err)
		}
	}()

	<-ctx.Done()
	log.Debug("calling stop")
	<-sv.Stop()
	log.Debug("done")
}

func mkBaseURL(options *Options, cfg *config.Config) config.URL {
	scheme := "http"
	port := options.Port
	if options.TLS {
		scheme = "https"
		port = options.TLSPort
	}
	return config.URL{
		URL: &url.URL{
			Scheme: scheme,
			Host:   net.JoinHostPort(cfg.Domains[0], strconv.Itoa(port)),
		},
	}
}