-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
70 lines (60 loc) · 1.34 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
package main
import (
"context"
"database/sql"
"log"
"os"
"os/signal"
"time"
"github.com/t-900-a/gemmit/feeds"
"git.sr.ht/~adnano/go-gemini"
"git.sr.ht/~adnano/go-gemini/certificate"
_ "github.com/jackc/pgx/v4/stdlib"
)
func main() {
hostname := os.Args[1]
certpath := "/var/lib/gemini/certs"
cs := os.Args[2]
if len(os.Args) > 3 {
certpath = os.Args[3]
}
db, err := sql.Open("pgx", cs)
if err != nil {
log.Fatalf("Failed to open a database connection: %v", err)
}
certificates := &certificate.Store{}
if err := certificates.Load(certpath); err != nil {
log.Fatal(err)
}
certificates.Register(hostname)
mux := configureRoutes()
server := &gemini.Server{
Handler: feeds.DatabaseMiddleware(
LoggingMiddleware(
CertificateMiddleware(mux)),
db),
ReadTimeout: 30 * time.Second,
WriteTimeout: 1 * time.Minute,
GetCertificate: certificates.Get,
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
errch := make(chan error)
go func() {
ctx := context.Background()
log.Println("Starting server")
errch <- server.ListenAndServe(ctx)
}()
select {
case err := <-errch:
log.Fatal(err)
case <-c:
log.Println("Shutting down...")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
err := server.Shutdown(ctx)
if err != nil {
log.Fatal(err)
}
}
}