Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit46fa9dd

Browse files
committed
CORS
1 parent7d3b5f2 commit46fa9dd

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

‎api/api.go‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package api
22

33
import (
44
"log"
5+
"net/http"
56
"path"
67
"strconv"
78

@@ -71,11 +72,13 @@ func openStore(p string) {
7172
funccreateRouter()*echo.Echo {
7273
r:=echo.New()
7374

75+
r.SetHTTPErrorHandler(errorHandler)
76+
7477
r.Use(mw.Logger())
7578
r.Use(mw.Recover())
79+
r.Use(cors())
7680
r.Use(mw.StripTrailingSlash())
7781
r.Use(auth())
78-
r.Use(mw.Gzip())
7982

8083
account:=r.Group("/account")
8184
account.Post("/login",login)
@@ -116,3 +119,15 @@ func createRouter() *echo.Echo {
116119

117120
returnr
118121
}
122+
123+
funcerrorHandler(errerror,c*echo.Context) {
124+
code:=http.StatusInternalServerError
125+
msg:=http.StatusText(code)
126+
ifhe,ok:=err.(*echo.HTTPError);ok {
127+
code=he.Code()
128+
msg=he.Error()
129+
}
130+
c.Response().Header().Set("Content-Type","text/plain; charset=utf-8")
131+
c.Response().WriteHeader(code)
132+
c.Response().Write([]byte(msg))
133+
}

‎api/auth.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func auth() echo.HandlerFunc {
1313
ifc.Request().URL.Path!="/account/login" {
1414
token:=c.Request().Header.Get("Authorization")
1515
iftoken=="" {
16-
returnecho.NewHTTPError(401)
16+
returnecho.NewHTTPError(401,"Bad token")
1717
}
1818

1919
user:=authCache.get(token)
@@ -25,7 +25,7 @@ func auth() echo.HandlerFunc {
2525

2626
user=store.GetUserByToken(token)
2727
ifuser==nil {
28-
returnecho.NewHTTPError(401)
28+
returnecho.NewHTTPError(401,"Bad token")
2929
}
3030

3131
authCache.set(token,user)

‎api/cors.go‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package api
2+
3+
import (
4+
"github.com/Castcloud/castcloud-go-server/Godeps/_workspace/src/github.com/labstack/echo"
5+
)
6+
7+
funccors() echo.MiddlewareFunc {
8+
returnfunc(h echo.HandlerFunc) echo.HandlerFunc {
9+
returnfunc(c*echo.Context)error {
10+
ifc.Request().Header.Get("Origin")=="" {
11+
returnh(c)
12+
}
13+
14+
c.Response().Header().Set("Access-Control-Allow-Origin","*")
15+
ifc.Request().Method=="OPTIONS" {
16+
c.Response().Header().Set("Access-Control-Allow-Methods","GET, POST, PUT, DELETE")
17+
c.Response().Header().Set("Access-Control-Allow-Headers","Authorization")
18+
returnc.NoContent(200)
19+
}
20+
21+
returnh(c)
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp