-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
69 lines (61 loc) · 1.38 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
package main
import (
"encoding/json"
. "github.com/everfore/oauth/oauth2"
"html/template"
"log"
"net/http"
"time"
)
var (
usage = []byte(`<a href="https://github.com/everfore/oauth" ><h1>@oauth2</h1></a>` + "\n" + `
<a href="/signin" ><h1>@sign in#github#</h1></a>`)
OA *OAGithub
)
func init() {
OA = NewOAGithub("8ba2991113e68b4805c1", "b551e8a640d53904d82f95ae0d84915ba4dc0571", "user", "http://bookmark.daoapp.io/callback")
}
func main() {
log.Println("ready...")
http.HandleFunc("/", root)
http.HandleFunc("/signin", signin)
http.HandleFunc("/callback", callback)
err := http.ListenAndServe(":80", nil)
if check_err(err) {
return
}
}
func root(rw http.ResponseWriter, req *http.Request) {
rw.Write(usage)
}
func signin(rw http.ResponseWriter, req *http.Request) {
http.Redirect(rw, req, OA.AuthURL(), 302)
}
func callback(rw http.ResponseWriter, req *http.Request) {
log.Printf("%s\n", req.RemoteAddr)
b, err := OA.NextStep(req)
if nil != err {
rw.Write([]byte(err.Error()))
return
}
var ret map[string]interface{}
err = json.Unmarshal(b, &ret)
if nil == err {
t := template.New("index.html")
t, err := t.ParseFiles("index.html")
if nil != err {
return
}
now := time.Now().String()
ret["now"] = now
t.Execute(rw, ret)
} else {
rw.Write([]byte(err.Error()))
}
}
func check_err(err error) bool {
if nil != err {
return true
}
return false
}