-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
60 lines (49 loc) · 1.4 KB
/
main.go
File metadata and controls
60 lines (49 loc) · 1.4 KB
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
package main
import (
"log"
"net/http"
"os"
"github.com/husio/plopper/lith"
"github.com/husio/plopper/plopper"
)
func main() {
conf := struct {
Port string
AuthAPI string
AuthUI string
Database string
}{
Port: env("PORT", "8000"),
AuthAPI: env("LITH_API", "https://lith-demo.herokuapp.com/api"),
AuthUI: env("LOGIN_URL", "https://lith-demo.herokuapp.com/pub/"),
Database: env("DATABASE", "/tmp/plopper.sqlite3"),
}
log.SetOutput(os.Stderr)
log.Printf("Running HTTP server on port %s", conf.Port)
auth := lith.NewClient(conf.AuthAPI, &http.Client{Transport: requestLogger{}})
plopStore, err := plopper.OpenSQLitePlopStore(conf.Database)
if err != nil {
log.Fatalf("cannot open plops store: %s", err)
}
defer plopStore.Close()
http.Handle("/", plopper.NewHTTPApplication(plopStore, auth, conf.AuthUI))
if err := http.ListenAndServe(":"+conf.Port, nil); err != nil {
log.Fatalf("http server: %s", err)
}
}
func env(name, fallback string) string {
if v, ok := os.LookupEnv(name); ok {
return v
}
return fallback
}
type requestLogger struct{}
func (requestLogger) RoundTrip(req *http.Request) (*http.Response, error) {
resp, err := http.DefaultTransport.RoundTrip(req)
if err != nil {
log.Printf("request to %q results in error %s", req.URL, err)
} else {
log.Printf("request to %q results in status code %d", req.URL, resp.StatusCode)
}
return resp, err
}