-
Notifications
You must be signed in to change notification settings - Fork 12
/
web.go
99 lines (83 loc) · 3.12 KB
/
web.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package routes
import (
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/contracts/route"
"github.com/goravel/framework/facades"
httpmiddleware "github.com/goravel/framework/http/middleware"
"github.com/spf13/cast"
"goravel/app/http/controllers"
"goravel/app/http/middleware"
)
func Web() {
facades.Route().Get("/", func(ctx http.Context) http.Response {
return ctx.Response().Json(200, http.Json{
"Hello": "Goravel",
})
})
// DB
dbController := controllers.NewDBController()
facades.Route().Get("/db", dbController.Index)
// Websocket
websocketController := controllers.NewWebsocketController()
facades.Route().Get("/ws", websocketController.Server)
// Validation
validationController := controllers.NewValidationController()
facades.Route().Post("/validation/json", validationController.Json)
facades.Route().Post("/validation/request", validationController.Request)
facades.Route().Post("/validation/form", validationController.Form)
// JWT
jwtController := controllers.NewJwtController()
facades.Route().Middleware(httpmiddleware.Throttle("login")).Get("/jwt/login", jwtController.Login)
facades.Route().Middleware(middleware.Jwt()).Get("/jwt", jwtController.Index)
// Swagger
swaggerController := controllers.NewSwaggerController()
facades.Route().Get("/swagger/*any", swaggerController.Index)
// Single Page Application
// 1. Add your single page application to `resources/views/*`
// 2. Add route to `/route/web.go`, needs to contain your home page and static routes
// 3. Configure nginx based on the /nginx.conf file
facades.Route().Get("web", func(ctx http.Context) http.Response {
return ctx.Response().View().Make("index.html")
})
facades.Route().Static("css", "./resources/views/css")
// View Nesting
// Check the views in `resources/views/admin/*`
facades.Route().Get("view", func(ctx http.Context) http.Response {
return ctx.Response().View().Make("admin/index.tmpl", map[string]any{
"name": "Goravel",
})
})
// Localization
langController := controllers.NewLangController()
facades.Route().Middleware(middleware.Lang()).Get("lang", langController.Index)
// Session
facades.Route().Prefix("session").Group(func(router route.Router) {
router.Get("put", func(ctx http.Context) http.Response {
ctx.Request().Session().Put("name", "Goravel")
return ctx.Response().Success().Json(http.Json{
"name": cast.ToString(ctx.Request().Session().Get("name")),
})
})
router.Get("get", func(ctx http.Context) http.Response {
return ctx.Response().Success().Json(http.Json{
"name": ctx.Request().Session().Get("name"),
})
})
})
// Cookie
facades.Route().Prefix("cookie").Group(func(router route.Router) {
router.Get("put", func(ctx http.Context) http.Response {
ctx.Response().Cookie(http.Cookie{
Name: "name",
Value: "Goravel",
})
return ctx.Response().Success().String("Set cookie: name=Goravel")
})
router.Get("get", func(ctx http.Context) http.Response {
return ctx.Response().Success().Json(http.Json{
"name": ctx.Request().Cookie("name"),
})
})
})
facades.Route().Resource("users", controllers.NewUserController())
}