-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
83 lines (68 loc) · 1.62 KB
/
app.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
package main
import (
"encoding/json"
"github.com/gorilla/sessions"
"html/template"
"io/ioutil"
"log"
"net/http"
"strconv"
)
var config Config
var store *sessions.CookieStore
const kSessionName = "sess"
func loadConfiguration() error {
file, err := ioutil.ReadFile("config.json")
if err != nil {
return err
}
err = json.Unmarshal(file, &config)
if err != nil {
return err
}
return nil
}
func main() {
// Load configuration
err := loadConfiguration()
if err != nil {
log.Println("Error while loading configuration")
log.Fatal(err)
}
initLogin()
// Load session store
store = sessions.NewCookieStore([]byte(config.SessionSecret))
// Load server
static := http.FileServer(http.Dir("static"))
http.Handle("/static/", http.StripPrefix("/static/", static))
http.HandleFunc("/", serveMain)
http.HandleFunc("/login", serveLogin)
http.HandleFunc("/login/github", serveLoginGithub)
http.HandleFunc("/logout", serveLogout)
http.HandleFunc("/callback", serveLoginCallback)
port := config.Port
if port == 0 {
port = 8080
}
http.ListenAndServe(":"+strconv.Itoa(port), nil)
}
func serveMain(w http.ResponseWriter, r *http.Request) {
// Check authentication
session, _ := store.Get(r, kSessionName)
_, ok := session.Values["id"]
if !ok {
http.Redirect(w, r, "/login", 302)
return
}
template, err := template.ParseFiles("templates/main.html", "templates/dashboard.html")
if err != nil {
log.Println(err.Error())
http.Error(w, http.StatusText(500), 500)
return
}
err = template.Execute(w, session.Values)
if err != nil {
log.Println(err.Error())
http.Error(w, http.StatusText(500), 500)
}
}