-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
57 lines (44 loc) · 1.19 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/joho/godotenv"
)
func main() {
err := godotenv.Load(".env")
if err != nil {
log.Fatalf("Error occurred loading env variables: %s", err)
}
mux := http.NewServeMux()
mux.HandleFunc("GET /oauthdemo", func(w http.ResponseWriter, r *http.Request) {
oauthDemo(w, r)
})
mux.HandleFunc("GET /google-oauth-callback", func(w http.ResponseWriter, r *http.Request) {
data, _ := getUserDataFromGoogle(r.FormValue("code"))
if err != nil {
log.Println(err.Error())
return
}
html, err := getAsHtml(data)
if err != nil {
log.Println(err.Error())
return
}
w.Header().Set("Content-Type", "text/html")
_, _ = w.Write([]byte(html))
})
mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
metrics := getStatus(r)
html, err := generateServerStatusHTML(metrics)
if err != nil {
log.Println(err.Error())
}
w.Header().Set("Content-Type", "text/html")
_, _ = w.Write([]byte(html))
})
log.Printf("Running on %s\n", os.Getenv("BASE_URL"))
log.Printf("Try oauth demo on %s/oauthdemo\n", os.Getenv("BASE_URL"))
log.Fatal(http.ListenAndServe(fmt.Sprintf("0.0.0.0:%s", os.Getenv("PORT")), mux))
}