-
Notifications
You must be signed in to change notification settings - Fork 1
/
web.go
154 lines (132 loc) · 3.33 KB
/
web.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
package roll
import (
"crypto/tls"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"log"
"net"
"net/http"
"path"
"time"
"github.com/gorilla/mux"
"github.com/gorilla/rpc/v2"
rpcjson "github.com/gorilla/rpc/v2/json"
)
type Duration struct {
time.Duration
}
type Time struct {
time.Time
}
func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(d.String())
}
func (d *Duration) UnmarshalJSON(b []byte) error {
var v string
if err := json.Unmarshal(b, &v); err != nil {
return err
}
var err error
d.Duration, err = time.ParseDuration(v)
if err != nil {
return err
}
return nil
}
func (t Time) MarshalJSON() ([]byte, error) {
return json.Marshal(t.Format(time.RFC1123))
}
func (t *Time) UnmarshalJSON(b []byte) error {
var v string
if err := json.Unmarshal(b, &v); err != nil {
return err
}
var err error
t.Time, err = time.Parse(time.RFC1123, v)
if err != nil {
return err
}
return nil
}
func (b *Bot) getTemplate(filename string) (*template.Template, error) {
file, err := b.openFile(path.Join("templates", filename))
if err != nil {
return nil, fmt.Errorf("Can't find template %s: %v", filename, err)
}
d, err := ioutil.ReadAll(file)
if err != nil {
return nil, fmt.Errorf("Error reading template %s: %v", filename, err)
}
t := template.Must(
template.New(filename).
Funcs(b.funcMap).
Parse(string(d)))
return t, nil
}
func (b *Bot) AddTemplateFunc(name string, f interface{}) error {
if _, ok := b.funcMap[name]; ok {
return fmt.Errorf("%s template func already registered", name)
}
b.funcMap[name] = f
return nil
}
func (b *Bot) indexHandler(w http.ResponseWriter, req *http.Request) {
indexTemplate, err := b.getTemplate("index.html")
if err != nil {
// do 404
return
}
indexTemplate.Execute(w, nil)
}
func (b *Bot) redirectHandler(w http.ResponseWriter, req *http.Request) {
// remove/add not default ports from req.Host
addr := b.Config.HTTPRedirectBase
if addr == "" {
addr = b.Config.HTTPSAddr
}
target := "https://" + addr + req.URL.Path
if len(req.URL.RawQuery) > 0 {
target += "?" + req.URL.RawQuery
}
log.Printf("redirect to: %s", target)
http.Redirect(w, req, target, http.StatusTemporaryRedirect)
}
func (b *Bot) startWebserver() error {
r := mux.NewRouter()
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))
r.HandleFunc("/auth/", b.authHandler)
r.HandleFunc("/wiki/{page}", b.wikiHandler)
r.HandleFunc("/", b.indexHandler)
s := rpc.NewServer()
s.RegisterCodec(rpcjson.NewCodec(), "application/json")
for name, mod := range b.modules {
if provider, ok := mod.(RPCServiceProvider); ok {
s.RegisterService(provider.GetRPCService(), name)
}
}
r.Handle("/rpc", s)
cert, err := tls.LoadX509KeyPair(b.Config.CertFile, b.Config.KeyFile)
if err != nil {
return err
}
config := &tls.Config{
Certificates: []tls.Certificate{cert},
}
log.Printf("About to listen on https://%s/", b.Config.HTTPSAddr)
listener, err := tls.Listen("tcp", b.Config.HTTPSAddr, config)
if err != nil {
return err
}
go http.Serve(listener, r)
if b.Config.HTTPAddr != "" {
log.Printf("Starting HTTP redirector on http://%s/", b.Config.HTTPAddr)
listener, err := net.Listen("tcp", b.Config.HTTPAddr)
if err != nil {
return err
}
go http.Serve(listener, http.HandlerFunc(b.redirectHandler))
}
return nil
}