gunk-dev/the-valley

main.go

source80bcab9b10admain's published tip history
3.6 KiB
1// Command valley-web serves a receiver's store to anonymous readers, as
2// the store's authorization allows: the active export's plan, its knots,
3// and a source browser over the main of each project anyone may read.
4//
5// valley-web --state FILE --listen ADDR
6//
7// It opens the store with export.OpenReadOnly, which writes nothing to
8// it, so it may run as a user that can only read the receiver's store. It
9// serves it with package server until it is sent SIGINT or SIGTERM. It
10// logs to standard error, and prints the address it listens on once it
11// does.
12package main
13
14import (
15 "context"
16 "errors"
17 "flag"
18 "fmt"
19 "io"
20 "log"
21 "net"
22 "net/http"
23 "os"
24 "os/signal"
25 "syscall"
26 "time"
27
28 "the-valley/web/export"
29 "the-valley/web/server"
30)
31
32const usage = `usage: valley-web --state FILE --listen ADDR
33
34Serve the active export of a web export receiver's store to anonymous
35readers, as the store's authorization allows: the plan page, each knot,
36and a source browser over main for each project anyone may read.
37
38 --state FILE the receiver's existing store descriptor, such as
39 /var/lib/valley-web/store; valley-web opens the store
40 read-only and writes nothing there
41 --listen ADDR the TCP address to listen on, such as 127.0.0.1:8080;
42 port 0 picks a free port
43
44valley-web prints "valley-web: listening on ADDR" once it listens, and
45stops on SIGINT or SIGTERM.
46`
47
48func main() {
49 ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
50 defer stop()
51 err := run(ctx, os.Args[1:], os.Stderr)
52 var bad usageError
53 switch {
54 case errors.As(err, &bad):
55 fmt.Fprintf(os.Stderr, "valley-web: %v\n\n%s", err, usage)
56 os.Exit(2)
57 case err != nil:
58 fmt.Fprintf(os.Stderr, "valley-web: %v\n", err)
59 os.Exit(1)
60 }
61}
62
63// usageError is a command line valley-web cannot run with.
64type usageError struct{ error }
65
66// run serves until ctx is done, then shuts the server down, letting
67// requests in flight finish for a few seconds.
68func run(ctx context.Context, args []string, stderr io.Writer) error {
69 flags := flag.NewFlagSet("valley-web", flag.ContinueOnError)
70 flags.SetOutput(io.Discard)
71 state := flags.String("state", "", "")
72 listen := flags.String("listen", "", "")
73 if err := flags.Parse(args); errors.Is(err, flag.ErrHelp) {
74 fmt.Fprint(stderr, usage)
75 return nil
76 } else if err != nil {
77 return usageError{err}
78 }
79 switch {
80 case flags.NArg() > 0:
81 return usageError{fmt.Errorf("unexpected argument %q", flags.Arg(0))}
82 case *state == "":
83 return usageError{errors.New("--state is required")}
84 case *listen == "":
85 return usageError{errors.New("--listen is required")}
86 }
87
88 logger := log.New(stderr, "valley-web: ", log.LstdFlags)
89 store, err := export.OpenReadOnly(*state)
90 if err != nil {
91 return err
92 }
93 ln, err := net.Listen("tcp", *listen)
94 if err != nil {
95 return err
96 }
97 srv := &http.Server{
98 Handler: server.New(server.Config{Store: store, Log: func(err error) { logger.Print(err) }}),
99 ReadHeaderTimeout: 10 * time.Second,
100 ReadTimeout: 30 * time.Second,
101 WriteTimeout: 60 * time.Second,
102 IdleTimeout: 120 * time.Second,
103 MaxHeaderBytes: 64 << 10,
104 ErrorLog: logger,
105 }
106 fmt.Fprintf(stderr, "valley-web: listening on %s\n", ln.Addr())
107 served := make(chan error, 1)
108 go func() { served <- srv.Serve(ln) }()
109 select {
110 case err := <-served:
111 return err
112 case <-ctx.Done():
113 }
114 shutdown, cancel := context.WithTimeout(context.Background(), 5*time.Second)
115 defer cancel()
116 if err := srv.Shutdown(shutdown); err != nil {
117 return err
118 }
119 if err := <-served; !errors.Is(err, http.ErrServerClosed) {
120 return err
121 }
122 return nil
123}