forked from favclip/ucon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
65 lines (53 loc) · 1.49 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
//go:generate jwg -output model_json.go -transcripttag swagger .
//go:generate qbg -output model_query.go .
package example_appengine
import (
"net/http"
"reflect"
"strings"
"github.com/favclip/ucon"
"github.com/favclip/ucon/swagger"
"google.golang.org/appengine"
)
func init() {
var _ ucon.HTTPErrorResponse = &HttpError{}
ucon.Middleware(UseAppengineContext)
ucon.Orthodox()
ucon.Middleware(swagger.RequestValidator())
swPlugin := swagger.NewPlugin(&swagger.Options{
Object: &swagger.Object{
Info: &swagger.Info{
Title: "Todo list",
Version: "v1",
},
Schemes: []string{"http"},
},
DefinitionNameModifier: func(refT reflect.Type, defName string) string {
if strings.HasSuffix(defName, "JSON") {
return defName[:len(defName)-4]
}
return defName
},
})
ucon.Plugin(swPlugin)
ucon.HandleFunc("GET", "/swagger-ui/", func(w http.ResponseWriter, r *http.Request) {
localPath := "./node_modules/swagger-ui/dist/" + r.URL.Path[len("/swagger-ui/"):]
http.ServeFile(w, r, localPath)
})
setupTodo(swPlugin)
ucon.HandleFunc("GET", "/", func(w http.ResponseWriter, r *http.Request) {
localPath := "./public/" + r.URL.Path[len("/"):]
http.ServeFile(w, r, localPath)
})
ucon.DefaultMux.Prepare()
http.Handle("/", ucon.DefaultMux)
}
func UseAppengineContext(b *ucon.Bubble) error {
if b.Context == nil {
b.Context = appengine.NewContext(b.R)
} else {
b.Context = appengine.WithContext(b.Context, b.R)
}
b.R = b.R.WithContext(b.Context)
return b.Next()
}