/
server.go
99 lines (85 loc) · 1.96 KB
/
server.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
package main
import (
"encoding/json"
"flag"
"log"
"net/http"
"github.com/guregu/bbs"
"github.com/guregu/relay/eti"
"github.com/guregu/relay/fourchan"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
)
var cfgFile = flag.String("config", "config.toml", "config file path")
var cfg config
var servers []relay
type relay struct {
server *bbs.Server
Path string `json:"path"`
}
func main() {
flag.Parse()
var err error
cfg, err = parseConfig(*cfgFile)
if err != nil {
log.Fatal(err)
}
if cfg.Server.Host == "" {
log.Fatalf("No host set in %s", *cfgFile)
}
if cfg.ETI.Enabled {
path := maybe(cfg.ETI.Path, "/bbs")
wsPath := ws(path)
eti.Setup(cfg.ETI.Name, cfg.ETI.Description, wsPath)
if cfg.ETI.Cache && cfg.Cache.Addr != "" {
eti.DBConnect(cfg.Cache.Addr, "eti")
}
srv := bbs.NewServer(eti.New)
goji.Handle(path, srv)
goji.Handle(path+"/ws", srv.WS)
servers = append(servers, relay{
server: srv,
Path: path,
})
}
if cfg.FourChan.Enabled {
path := maybe(cfg.FourChan.Path, "/bbs")
wsPath := ws(path)
fourchan.Setup(cfg.FourChan.Name, cfg.FourChan.Description, wsPath)
// no cache yet. sorry moot
srv := bbs.NewServer(fourchan.New)
goji.Handle(path, srv)
goji.Handle(path+"/ws", srv.WS)
servers = append(servers, relay{
server: srv,
Path: path,
})
}
if cfg.Web.Index != "" {
log.Println(cfg.Web.Index)
goji.Get(cfg.Web.Index, indexHandler)
}
if cfg.Web.Root != "" {
goji.Get("/*", http.FileServer(http.Dir(cfg.Web.Root)))
}
goji.Serve()
}
// for index.json, which lists all our servers
func indexHandler(c web.C, w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
data, err := json.Marshal(servers)
log.Printf("%s", string(data))
if err != nil {
log.Println(err)
}
w.Write(data)
}
func maybe(test, def string) string {
if test == "" {
return def
}
return test
}
func ws(path string) string {
return "ws://" + cfg.Server.Host + path + "/ws"
}