-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
main.go
102 lines (87 loc) · 2.73 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
package main
import (
"context"
"embed"
"errors"
"flag"
"io"
"log"
"net/http"
"github.com/kelseyhightower/envconfig"
"github.com/owulveryck/goMarkableStream/internal/pubsub"
"github.com/owulveryck/goMarkableStream/internal/remarkable"
)
type configuration struct {
BindAddr string `envconfig:"SERVER_BIND_ADDR" default:":2001" required:"true" description:"The server bind address"`
Username string `envconfig:"SERVER_USERNAME" default:"admin"`
Password string `envconfig:"SERVER_PASSWORD" default:"password"`
TLS bool `envconfig:"HTTPS" default:"true"`
Compression bool `envconfig:"COMPRESSION" default:"false"`
RLECompression bool `envconfig:"RLE_COMPRESSION" default:"true"`
DevMode bool `envconfig:"DEV_MODE" default:"false"`
ZSTDCompression bool `envconfig:"ZSTD_COMPRESSION" default:"false" description:"Enable zstd compression"`
ZSTDCompressionLevel int `envconfig:"ZSTD_COMPRESSION_LEVEL" default:"3" description:"Zstd compression level (1-22, where 1 is fastest and 22 is maximum compression)"`
}
const (
// ConfigPrefix for environment variable based configuration
ConfigPrefix = "RK"
)
var (
pointerAddr int64
file io.ReaderAt
// Define the username and password for authentication
c configuration
//go:embed client/*
assetsFS embed.FS
//go:embed assets/cert.pem assets/key.pem
tlsAssets embed.FS
)
func validateConfiguration(c *configuration) error {
if remarkable.Model == remarkable.RemarkablePaperPro {
if c.RLECompression {
return errors.New("RLE compression is not supported on the Remarkable Paper Pro. Disable it by setting RLE_COMPRESSION=false")
}
}
return nil
}
func main() {
var err error
ifaces()
help := flag.Bool("h", false, "print usage")
unsafe := flag.Bool("unsafe", false, "disable authentication")
flag.Parse()
if *help {
envconfig.Usage(ConfigPrefix, &c)
return
}
if err := envconfig.Process(ConfigPrefix, &c); err != nil {
envconfig.Usage(ConfigPrefix, &c)
log.Fatal(err)
}
if err := validateConfiguration(&c); err != nil {
panic(err)
}
file, pointerAddr, err = remarkable.GetFileAndPointer()
if err != nil {
log.Fatal(err)
}
eventPublisher := pubsub.NewPubSub()
eventScanner := remarkable.NewEventScanner()
eventScanner.StartAndPublish(context.Background(), eventPublisher)
mux := setMuxer(eventPublisher)
// handler := BasicAuthMiddleware(gzMiddleware(mux))
var handler http.Handler
handler = BasicAuthMiddleware(mux)
if *unsafe {
handler = mux
}
l, err := setupListener(context.Background(), &c)
if err != nil {
log.Fatal(err)
}
log.Printf("listening on %v", l.Addr())
if c.TLS {
log.Fatal(runTLS(l, handler))
}
log.Fatal(http.Serve(l, handler))
}