-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
203 lines (176 loc) · 4.66 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"database/sql"
"flag"
"fmt"
"log"
"net/http"
"net/url"
"path/filepath"
// register sqlite3 for database/sql
_ "github.com/mattn/go-sqlite3"
"github.com/BurntSushi/toml"
"hawx.me/code/serve"
"hawx.me/code/tally-ho/auth"
"hawx.me/code/tally-ho/blog"
"hawx.me/code/tally-ho/media"
"hawx.me/code/tally-ho/micropub"
"hawx.me/code/tally-ho/silos"
"hawx.me/code/tally-ho/webmention"
"hawx.me/code/tally-ho/websub"
)
func usage() {
fmt.Println(`Usage: tally-ho [options]
--config PATH=./config.toml
--web DIR=web
--db PATH=file::memory
--media-dir DIR
--port PORT=8080
--socket PATH`)
}
type config struct {
Me string
Name string
Title string
Description string
BaseURL string
MediaURL string
Flickr, Twitter struct {
ConsumerKey string
ConsumerSecret string
AccessToken string
AccessTokenSecret string
}
Github struct {
AccessToken string
}
}
func main() {
var (
configPath = flag.String("config", "./config.toml", "")
webPath = flag.String("web", "web", "")
dbPath = flag.String("db", "file::memory:", "")
mediaDir = flag.String("media-dir", "", "")
port = flag.String("port", "8080", "")
socket = flag.String("socket", "", "")
)
flag.Usage = usage
flag.Parse()
var conf config
if _, err := toml.DecodeFile(*configPath, &conf); err != nil {
log.Println("ERR decode-config;", err)
return
}
baseURL, err := url.Parse(conf.BaseURL)
if err != nil {
log.Println("ERR base-url-invalid;", err)
return
}
mediaURL, err := url.Parse(conf.MediaURL)
if err != nil {
log.Println("ERR media-url-invalid;", err)
return
}
templates, err := blog.ParseTemplates(*webPath)
if err != nil {
log.Println("ERR parse-templates;", err)
return
}
db, err := sql.Open("sqlite3", *dbPath)
if err != nil {
log.Printf("ERR sql-open path=%s; %v\n", *dbPath, err)
return
}
fw := &blog.FileWriter{
MediaDir: *mediaDir,
MediaURL: mediaURL,
}
var blogSilos []interface{}
var micropubSyndicateTo []micropub.SyndicateTo
if conf.Twitter.ConsumerKey != "" {
twitter, err := silos.Twitter(silos.TwitterOptions{
ConsumerKey: conf.Twitter.ConsumerKey,
ConsumerSecret: conf.Twitter.ConsumerSecret,
AccessToken: conf.Twitter.AccessToken,
AccessTokenSecret: conf.Twitter.AccessTokenSecret,
}, fw)
if err != nil {
log.Println("WARN twitter;", err)
} else {
blogSilos = append(blogSilos, twitter)
micropubSyndicateTo = append(micropubSyndicateTo, micropub.SyndicateTo{
UID: twitter.UID(),
Name: twitter.Name(),
})
}
}
if conf.Flickr.ConsumerKey != "" {
flickr, err := silos.Flickr(silos.FlickrOptions{
ConsumerKey: conf.Flickr.ConsumerKey,
ConsumerSecret: conf.Flickr.ConsumerSecret,
AccessToken: conf.Flickr.AccessToken,
AccessTokenSecret: conf.Flickr.AccessTokenSecret,
})
if err != nil {
log.Println("WARN flickr;", err)
} else {
blogSilos = append(blogSilos, flickr)
micropubSyndicateTo = append(micropubSyndicateTo, micropub.SyndicateTo{
UID: flickr.UID(),
Name: flickr.Name(),
})
}
}
if conf.Github.AccessToken != "" {
github, err := silos.Github(silos.GithubOptions{
AccessToken: conf.Github.AccessToken,
})
if err != nil {
log.Println("WARN github;", err)
} else {
blogSilos = append(blogSilos, github)
micropubSyndicateTo = append(micropubSyndicateTo, micropub.SyndicateTo{
UID: github.UID(),
Name: github.Name(),
})
}
}
hubStore, err := blog.NewHubStore(db)
if err != nil {
log.Println("ERR blog-hub-store;", err)
return
}
mediaEndpointURL, _ := url.Parse("/-/media")
hubEndpointURL, _ := url.Parse("/-/hub")
websubhub := websub.New(baseURL.ResolveReference(hubEndpointURL).String(), hubStore)
b, err := blog.New(blog.Config{
Me: conf.Me,
Name: conf.Name,
Title: conf.Title,
Description: conf.Description,
BaseURL: baseURL,
MediaURL: mediaURL,
MediaDir: *mediaDir,
HubURL: baseURL.ResolveReference(hubEndpointURL).String(),
}, db, templates, websubhub, blogSilos)
if err != nil {
log.Println("ERR new-blog;", err)
return
}
defer b.Close()
http.Handle("/", b.Handler())
http.Handle("/public/",
http.StripPrefix("/public/",
http.FileServer(
http.Dir(filepath.Join(*webPath, "static")))))
http.Handle("/-/micropub", micropub.Endpoint(
b,
conf.Me,
baseURL.ResolveReference(mediaEndpointURL).String(),
micropubSyndicateTo,
fw))
http.Handle("/-/webmention", webmention.Endpoint(b))
http.Handle("/-/media", auth.Only(conf.Me, media.Endpoint(fw, auth.HasScope)))
http.Handle("/-/hub", websubhub)
serve.Serve(*port, *socket, http.DefaultServeMux)
}