/
developerController.go
92 lines (83 loc) · 2.18 KB
/
developerController.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
package controllers
import (
"fmt"
"html/template"
"log"
"net/http"
"github.com/UHERO/rest-api/common"
"github.com/markbates/goth/gothic"
)
// IndexHandler produces the landing page
func IndexHandler(w http.ResponseWriter, r *http.Request) {
log.Println("Index Requested")
t, err := template.New("index").Parse(indexTemplate)
if err != nil {
fmt.Fprintln(w, err)
return
}
err = t.Execute(w, nil)
if err != nil {
fmt.Fprintln(w, err)
}
}
// DeveloperHandler produces the authenticated developer landing page
//func DeveloperHandler(applicationRepository data.Repository) func(w http.ResponseWriter, r *http.Request) {
// return func(w http.ResponseWriter, r *http.Request) {
//
// appClaims, ok := common.FromContext(r.Context())
// if ok != true {
// panic(errors.New("cannot get value from context"))
// }
//
// log.Printf("username: %s", appClaims.Username)
// filename := "./static/build/bundled/index.html"
// developerIndex, err := os.Open(filename)
// if err != nil {
// panic(err)
// }
// http.ServeContent(w, r, filename, time.Now(), developerIndex)
//
// //applications, err := applicationRepository.GetAll(appClaims.Username)
// //if err != nil {
// // panic(err)
// //}
// //
// //userResource := UserResource{User: appClaims.Username, Applications: applications}
// //t, _ := template.New("userinfo").Parse(applicationTemplate)
// //t.Execute(w, userResource)
//
// }
//}
// AuthCallback is the callback used by GitHub
func AuthCallback(w http.ResponseWriter, r *http.Request) {
userProfile, err := gothic.CompleteUserAuth(w, r)
if err != nil {
fmt.Fprintln(w, err)
return
}
token, err := common.GenerateJWT(userProfile.NickName, "user")
common.StoreJWT(w, r, token)
if err != nil {
panic(err)
}
http.Redirect(w, r, "/", http.StatusMovedPermanently)
}
// View Templates
var indexTemplate = `
<p><a href="/auth?provider=github">Log in with GitHub</a></p>
`
var applicationTemplate = `
<h1>Applications for {{.User}}</h1>
{{range .Applications}}
<h2>{{.Name}} ({{.Id}})</h2>
<dl class="dl-horizontal">
<dt>Hostname</dt>
<dd>{{.Hostname}}</dd>
<dt>API Key</dt>
<dd>{{.APIKey}}</dd>
</dl>
{{else}}
No Applications
{{end}}
<button>Add Application</button>
`