-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
80 lines (71 loc) · 1.82 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
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"encoding/json"
"flag"
"github.com/webx-top/echo"
"github.com/webx-top/echo/engine/fasthttp"
"github.com/webx-top/echo/engine/standard"
"github.com/webx-top/echo/handler/oauth2"
mw "github.com/webx-top/echo/middleware"
"github.com/webx-top/echo/middleware/session"
boltStore "github.com/webx-top/echo/middleware/session/engine/bolt"
cookieStore "github.com/webx-top/echo/middleware/session/engine/cookie"
)
func main() {
port := flag.String(`p`, "8080", "port")
flag.Parse()
e := echo.New()
e.Use(mw.Log())
sessionOptions := &echo.SessionOptions{
Engine: `bolt`,
Name: `SESSIONID`,
CookieOptions: &echo.CookieOptions{
Path: `/`,
Domain: ``,
MaxAge: 0,
Secure: false,
HttpOnly: true,
},
}
cookieStore.RegWithOptions(&cookieStore.CookieOptions{
KeyPairs: [][]byte{
[]byte(`123456789012345678901234567890ab`),
},
})
boltStore.RegWithOptions(&boltStore.BoltOptions{
File: `./session.db`,
KeyPairs: [][]byte{
[]byte(`123456789012345678901234567890ab`),
},
BucketName: `session`,
})
e.Use(session.Middleware(sessionOptions))
e.Get("/", func(c echo.Context) error {
return c.HTML(`Login: <a href="/oauth/login/github" target="_blank">github</a>`)
})
config := oauth2.NewConfig()
config.AddAccount(&oauth2.Account{
On: true,
Name: `github`,
Key: `9b168a10a77fbcafcdcf`,
Secret: `929bbf6136084052faf4f5768c14af805173ac27`,
})
options := oauth2.New(`http://www.coscms.com`, config)
options.SetSuccessHandler(func(ctx echo.Context) error {
user := options.User(ctx)
b, e := json.MarshalIndent(user, "", " ")
if e != nil {
return e
}
return ctx.String(string(b))
})
options.Wrapper(e)
switch `` {
case `fast`:
// FastHTTP
e.Run(fasthttp.New(":" + *port))
default:
// Standard
e.Run(standard.New(":" + *port))
}
}