-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
138 lines (113 loc) · 2.69 KB
/
main.go
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
package main
import (
"archiiv/fs"
"archiiv/id"
"flag"
"fmt"
"log/slog"
"net"
"net/http"
"os"
"path/filepath"
"time"
)
func main() {
log := slog.New(slog.NewJSONHandler(os.Stdout, nil))
srv, conf, err := createServer(log, os.Args[1:], os.Getenv)
if err != nil {
fmt.Printf("error from create: %s\n", err)
os.Exit(1)
}
err = run(log, srv, conf)
if err != nil {
fmt.Printf("error from run: %s\n", err)
os.Exit(1)
}
}
func createServer(log *slog.Logger, args []string, env func(string) string) (http.Handler, config, error) {
conf, err := getConfig(args, env)
if err != nil {
return nil, config{}, fmt.Errorf("get config: %w", err)
}
usersDir := filepath.Join(conf.dataDir, "users")
filesDir := filepath.Join(conf.dataDir, "files")
users, err := newUserStore(usersDir)
if err != nil {
return nil, config{}, fmt.Errorf("new user store: %w", err)
}
files, err := fs.NewFs(conf.rootID, filesDir)
if err != nil {
return nil, config{}, fmt.Errorf("new fs: %w", err)
}
mux := http.NewServeMux()
addRoutes(
mux,
log,
conf.secret,
users,
files,
)
var srv http.Handler = mux
srv = logAccesses(log, srv)
return srv, conf, nil
}
type config struct {
host string
port string
secret string
dataDir string
rootID id.ID
}
func getConfig(args []string, env func(string) string) (conf config, err error) {
flags := flag.NewFlagSet("archiiv", flag.ContinueOnError)
flags.StringVar(&conf.host, "host", "localhost", "")
flags.StringVar(&conf.port, "port", "8275", "")
flags.StringVar(&conf.dataDir, "data_dir", "", "")
var rootIDString string
flags.StringVar(&rootIDString, "root_id", "", "")
err = flags.Parse(args)
if err != nil {
err = fmt.Errorf("flags parse: %w", err)
return
}
if !filepath.IsAbs(conf.dataDir) {
err = fmt.Errorf("data dir must be absolute path (is %#v)", conf.dataDir)
return
}
conf.secret = env("ARCHIIV_SECRET")
conf.rootID, err = id.Parse(rootIDString)
if err != nil {
err = fmt.Errorf("id parse: %w", err)
return
}
return
}
func greet(log *slog.Logger) {
hour := time.Now().Hour()
switch {
case hour < 12:
log.Info("Good morning")
case hour < 17:
log.Info("Good afternoon")
default:
log.Info("Good evening")
}
}
func goodbye(log *slog.Logger) {
log.Info("Goodbye")
}
func run(log *slog.Logger, srv http.Handler, conf config) error {
greet(log)
defer goodbye(log)
httpServer := &http.Server{
Addr: net.JoinHostPort(conf.host, conf.port),
Handler: srv,
ReadHeaderTimeout: 1 * time.Second,
}
log.Info("listening", "address", httpServer.Addr)
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Error("listening and serving", "error", err)
return err
}
return nil
}