-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
63 lines (58 loc) · 1.71 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
package main
import (
"flag"
"html/template"
"log"
"net/http"
"os"
"path/filepath"
"sync"
"github.com/snukone/trace"
"github.com/stretchr/gomniauth"
"github.com/stretchr/gomniauth/providers/github"
"github.com/stretchr/objx"
)
// templ represents a single template
type templateHandler struct {
once sync.Once
filename string
templ *template.Template
}
// ServeHTTP handles the HTTP request.
func (t *templateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
t.once.Do(func() {
t.templ =
template.Must(template.ParseFiles(filepath.Join("templates",
t.filename)))
})
data := map[string]interface{}{
"Host": r.Host,
}
if authCookie, err := r.Cookie("auth"); err == nil {
data["UserData"] = objx.MustFromBase64(authCookie.Value)
}
t.templ.Execute(w, data)
}
func main() {
var addr = flag.String("addr", ":8080", "The addr of the application.")
flag.Parse() // parse the flags
// set up gomniauth
gomniauth.SetSecurityKey("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
gomniauth.WithProviders(
github.New("60a4c05cdc1199396d32", "3dfc10b50c17fed83bc354b268e10366a9ce46d9", "http://localhost:8080/auth/callback/github"),
)
r := newRoom()
r.tracer = trace.New(os.Stdout)
http.Handle("/chat", MustAuth(&templateHandler{filename: "chat.html"}))
http.Handle("/login", &templateHandler{filename: "login.html"})
http.HandleFunc("/auth/", loginHandler)
http.Handle("/room", r)
http.Handle("/images/", http.StripPrefix("/images", http.FileServer(http.Dir("images/"))))
// get the room going
go r.run()
// start the web server
log.Println("Starting web server on", *addr)
if err := http.ListenAndServe(*addr, nil); err != nil {
log.Fatal("ListenAndServe:", err)
}
}