/
admin.go
92 lines (75 loc) · 2.03 KB
/
admin.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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"github.com/BurntSushi/toml"
"hawx.me/code/indieauth"
"hawx.me/code/indieauth/sessions"
"hawx.me/code/mux"
"hawx.me/code/riviera-admin/handlers"
"hawx.me/code/serve"
)
const help = `Usage: riviera-admin [options] FILE
An admin panel for riviera.
--port <num> # Port to bind to (default: 8081)
--socket <path> # Serve using a unix socket instead
--settings <path> # Path to settings (default: ./settings.toml)
--help # Display help message
`
type Conf struct {
Secret string
URL string
PathPrefix string
Me string
}
func Log(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s", r.Method, r.URL)
handler.ServeHTTP(w, r)
})
}
func main() {
var (
settingsPath = flag.String("settings", "./settings.toml", "")
port = flag.String("port", "8081", "")
socket = flag.String("socket", "", "")
)
flag.Usage = func() { fmt.Println(help) }
flag.Parse()
if flag.NArg() == 0 {
fmt.Println(help)
return
}
opmlPath := flag.Arg(0)
var conf *Conf
if _, err := toml.DecodeFile(*settingsPath, &conf); err != nil {
log.Fatal("toml: ", err)
}
auth, err := indieauth.Authentication(conf.URL, conf.URL+"/callback")
if err != nil {
log.Fatal(err)
}
session, err := sessions.New(conf.Me, conf.Secret, auth)
if err != nil {
log.Fatal(err)
}
session.Root = conf.PathPrefix
http.Handle("/", mux.Method{
"GET": session.Choose(
handlers.List(opmlPath, conf.URL),
handlers.Login(conf.URL),
),
})
http.Handle("/subscribe", session.Shield(mux.Method{
"GET": handlers.Subscribe(opmlPath, conf.PathPrefix),
}))
http.Handle("/unsubscribe", session.Shield(mux.Method{
"GET": handlers.Unsubscribe(opmlPath, conf.PathPrefix),
}))
http.Handle("/sign-in", session.SignIn())
http.Handle("/callback", session.Callback())
http.Handle("/sign-out", session.SignOut())
serve.Serve(*port, *socket, Log(http.DefaultServeMux))
}