-
Notifications
You must be signed in to change notification settings - Fork 1
/
github.go
67 lines (55 loc) · 1.51 KB
/
github.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
package oauth
import (
"net/http"
"github.com/labstack/echo/v4"
"github.com/lucky-byte/reactgo/serve/ctx"
"github.com/lucky-byte/reactgo/serve/db"
)
func githubGet(c echo.Context) error {
cc := c.(*ctx.Context)
ql := `select * from oauth where provider = 'github'`
var result []db.OAuth
err := db.Select(ql, &result)
if err != nil {
cc.ErrLog(err).Error("查询 GitHub 授权配置错")
return c.NoContent(http.StatusInternalServerError)
}
var oauth db.OAuth
if len(result) == 0 {
ql = `insert into oauth (provider, sortno) values ('github', 1)`
err = db.ExecOne(ql)
if err != nil {
cc.ErrLog(err).Error("新增 GitHub 授权配置错")
return c.NoContent(http.StatusInternalServerError)
}
} else {
oauth = result[0]
}
return c.JSON(http.StatusOK, echo.Map{
"clientid": oauth.ClientId,
"secret": oauth.Secret,
"enabled": oauth.Enabled,
})
}
func githubSet(c echo.Context) error {
cc := c.(*ctx.Context)
var clientid, secret string
var enabled bool
err := echo.FormFieldBinder(c).
MustString("clientid", &clientid).
MustString("secret", &secret).
MustBool("enabled", &enabled).BindError()
if err != nil {
return cc.BadRequest(err)
}
cc.Trim(&clientid, &secret)
ql := `
update oauth set clientid = ?, secret = ?, enabled = ?
where provider = 'github'
`
if err = db.ExecOne(ql, clientid, secret, enabled); err != nil {
cc.ErrLog(err).Error("更新 GitHub 授权配置错")
return c.NoContent(http.StatusInternalServerError)
}
return c.NoContent(http.StatusOK)
}