-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
135 lines (108 loc) · 2.35 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
package main
import (
"errors"
"log"
"net/http"
"os"
"strings"
"github.com/PonyChat/shoutpage/atheme"
"github.com/codegangsta/negroni"
"github.com/yosssi/ace"
)
func init() {
if os.Getenv("ATHEME_URL") == "" {
panic("Need ATHEME_URL")
}
if os.Getenv("BNC_URL") == "" {
panic("Need BNC_URL")
}
if os.Getenv("OUTPUT_PATH") == "" {
panic("Need OUTPUT_PATH")
}
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
doTemplate("index", rw, r, nil)
})
mux.HandleFunc("/signup", func(rw http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
handleError(rw, r, errors.New("wrong method"))
return
}
err := r.ParseForm()
if err != nil {
handleError(rw, r, err)
return
}
username := r.Form.Get("username")
password := r.Form.Get("password")
a, _ := atheme.NewAtheme(os.Getenv("ATHEME_URL"))
err = a.Login(username, password)
if err != nil {
handleError(rw, r, err)
return
}
info, err := a.NickServ.OwnInfo()
if err != nil {
handleError(rw, r, err)
return
}
groupsRaw, ok := info["groups"]
if !ok {
doTemplate("notinprogram", rw, r, username)
return
}
groups := strings.Split(groupsRaw, ", ")
for _, group := range groups {
if group == "!bncusers" {
goto ok
}
}
doTemplate("notinprogram", rw, r, username)
return
ok:
MakeUser(username, password)
doTemplate("success", rw, r, struct {
Username string
URL string
}{
Username: username,
URL: os.Getenv("BNC_URL"),
})
})
n := negroni.Classic()
n.UseHandler(mux)
n.Run(":3000")
}
func handleError(rw http.ResponseWriter, r *http.Request, err error) {
rw.WriteHeader(500)
data := struct {
Path string
Reason string
}{
Path: r.URL.String(),
Reason: err.Error(),
}
log.Printf("%#v", err)
tpl, err := ace.Load("layout", "error", nil)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
if err := tpl.Execute(rw, data); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
}
func doTemplate(name string, rw http.ResponseWriter, r *http.Request, data interface{}) {
tpl, err := ace.Load("layout", name, nil)
if err != nil {
handleError(rw, r, err)
return
}
if err := tpl.Execute(rw, data); err != nil {
handleError(rw, r, err)
return
}
}