all repos — homestead @ main

Code for my website

domain/content/publisher/barkeep/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
package main

import (
	"context"
	"errors"
	"fmt"
	"os"
	"path/filepath"

	"github.com/ardanlabs/conf/v3"
	"tailscale.com/tsnet"

	"alin.ovh/homestead/domain/content/publisher"
	"alin.ovh/homestead/domain/web/server"
	"alin.ovh/homestead/shared/env"
	"alin.ovh/x/log"
)

type Options struct {
	Development bool `conf:"flag:dev"`
	Publisher   publisher.Options
	Server      server.Options
}

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)

	if options.Development {
		options.Publisher.Development = true
	}

	hostname := env.GetEnvFallback("TAILSCALE_HOSTNAME", "barkeep")
	stateDir := env.GetEnvFallback("XDG_STATE_HOME", "~/.local/state")

	ts := &tsnet.Server{
		Dir:       filepath.Join(stateDir, "barkeep"),
		Hostname:  hostname,
		Ephemeral: options.Development,
	}

	lc, err := ts.LocalClient()
	if err != nil {
		log.Fatal("error creating tailscale local client", "error", err)
	}

	options.Publisher.LocalClient = lc

	pub, err := publisher.New(&options.Publisher, log.Named("publisher"))
	if err != nil {
		log.Fatal("error running publisher", "error", err)
	}

	listener, err := ts.ListenTLS("tcp", ":443")
	if err != nil {
		log.Fatal("error creating tailscale listener", "error", err)
	}

	options.Server.Listener = listener

	srv, err := server.New(&options.Server, log.Named("server"))
	if err != nil {
		log.Fatal("error creating server", "error", err)
	}

	if err := srv.HostApp(pub.App); err != nil {
		log.Fatal("error hosting app", "error", err)
	}

	go func() {
		_, err := ts.Up(context.Background())
		if err != nil {
			log.Error("error starting tailscale", "error", err)
		}

		log.Info("starting server", "base_url", options.Publisher.BaseURL)
	}()

	if err := srv.Start(); err != nil {
		log.Fatal("error starting server", "error", err)
	}

	defer ts.Close()
}