-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest-main.go
54 lines (48 loc) · 1.15 KB
/
rest-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
// Package provides ...
// 182.92.5.210:4567
package main
import (
"github.com/ant0ine/go-json-rest/rest"
"log"
"net/http"
)
type User struct {
Id string
Name string
}
func AuthLogin(w rest.ResponseWriter, req *rest.Request) {
user := User{
Id: req.PathParam("id"),
Name: "ryn",
}
w.WriteJson(&user)
}
func RawWrite(w rest.ResponseWriter, req *rest.Request) {
w.WriteJson(req.PathParam("raw.json"))
}
func main() {
api := rest.NewApi()
api.Use(rest.DefaultDevStack...)
//配置API跨域CORS
api.Use(&rest.CorsMiddleware{
RejectNonCorsRequests: false,
OriginValidator: func(origin string, req *rest.Request) bool {
return origin == req.Header.Get("Origin")
},
AllowedMethods: []string{"GET", "POST", "PUT"},
AllowedHeaders: []string{
"Accept", "Content-Type", "X-Custom-Header", "Origin"},
AccessControlAllowCredentials: true,
AccessControlMaxAge: 3600,
})
//设置API基本Router
router, err := rest.MakeRouter(
rest.Post("/:raw.json", RawWrite),
rest.Post("/auth/login/:id", AuthLogin),
)
if err != nil {
log.Fatal(err)
}
api.SetApp(router)
log.Fatal(http.ListenAndServe(":8080", api.MakeHandler()))
}