Skip to content

Commit

Permalink
upgrade: v1.15.0 (#40)
Browse files Browse the repository at this point in the history
* upgrade: v1.15.0

* add test cases

* fix typo
  • Loading branch information
hwbrzzl authored Dec 30, 2024
1 parent a2c3bb6 commit 0b2e8f3
Show file tree
Hide file tree
Showing 17 changed files with 296 additions and 89 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,23 @@ Welcome to star, PR and issues!

### Integration of single page application into the framework

[routes/web.go](https://github.com/goravel/example/blob/master/routes/web.go#L44)
[routes/web.go](https://github.com/goravel/example/blob/master/routes/web.go#L26)

### View nesting

[routes/web.go](https://github.com/goravel/example/blob/master/routes/web.go#L53)

### Localization

[routes/web.go](https://github.com/goravel/example/blob/master/routes/web.go#L61)
[routes/web.go](https://github.com/goravel/example/blob/master/routes/web.go#L33)

### Session

[routes/web.go](https://github.com/goravel/example/blob/master/routes/web.go#L65)
[routes/web.go](https://github.com/goravel/example/blob/master/routes/web.go#L42)

### Cookie

[routes/web.go](https://github.com/goravel/example/blob/master/routes/web.go#L81)
[routes/web.go](https://github.com/goravel/example/blob/master/routes/web.go#L58)

### Localization

[routes/api.go](https://github.com/goravel/example/blob/master/routes/api.go#L37)

### GraphQL

Expand Down
16 changes: 8 additions & 8 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,23 @@ Laravel!

### 单页面前端应用集成到框架

[routes/web.go](https://github.com/goravel/example/blob/master/routes/web.go#L44)
[routes/web.go](https://github.com/goravel/example/blob/master/routes/web.go#L26)

### 视图嵌套

[routes/web.go](https://github.com/goravel/example/blob/master/routes/web.go#L53)

### 本地化

[routes/web.go](https://github.com/goravel/example/blob/master/routes/web.go#L61)
[routes/web.go](https://github.com/goravel/example/blob/master/routes/web.go#L33)

### Session

[routes/web.go](https://github.com/goravel/example/blob/master/routes/web.go#L65)
[routes/web.go](https://github.com/goravel/example/blob/master/routes/web.go#L42)

### Cookie

[routes/web.go](https://github.com/goravel/example/blob/master/routes/web.go#L81)
[routes/web.go](https://github.com/goravel/example/blob/master/routes/web.go#L58)

### 本地化

[routes/api.go](https://github.com/goravel/example/blob/master/routes/api.go#L37)

### GraphQL

Expand Down
15 changes: 14 additions & 1 deletion app/http/controllers/auth_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controllers
import (
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/facades"
"github.com/spf13/cast"

"goravel/app/models"
)
Expand Down Expand Up @@ -52,19 +53,31 @@ func (r *AuthController) Login(ctx http.Context) http.Response {
}

func (r *AuthController) Info(ctx http.Context) http.Response {
var user models.User
var (
id string
user models.User
err error
)

if guard := ctx.Request().Header("Guard"); guard == "" {
if err := facades.Auth(ctx).User(&user); err != nil {
return ctx.Response().String(http.StatusInternalServerError, err.Error())
}
id, err = facades.Auth(ctx).ID()

} else {
if err := facades.Auth(ctx).Guard(guard).User(&user); err != nil {
return ctx.Response().String(http.StatusInternalServerError, err.Error())
}
id, err = facades.Auth(ctx).Guard(guard).ID()
}

if err != nil {
return ctx.Response().String(http.StatusInternalServerError, err.Error())
}

return ctx.Response().Success().Json(http.Json{
"id": cast.ToUint(id),
"user": user,
})
}
1 change: 1 addition & 0 deletions app/http/controllers/validation_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func (r *ValidationController) Request(ctx http.Context) http.Response {
"tags": userCreate.Tags,
"scores": userCreate.Scores,
"date": userCreate.Date.ToDateTimeString(),
"code": userCreate.Code,
})
}

Expand Down
6 changes: 3 additions & 3 deletions app/http/middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func Auth() http.Middleware {

token := ctx.Request().Header("Authorization", "")
if token == "" {
ctx.Request().AbortWithStatus(http.StatusUnauthorized)
ctx.Response().String(http.StatusUnauthorized, "Unauthorized").Abort()
return
}

Expand All @@ -27,14 +27,14 @@ func Auth() http.Middleware {
token, err = facades.Auth(ctx).Guard(guard).Refresh()
if err != nil {
// Refresh time exceeded
ctx.Request().AbortWithStatus(http.StatusUnauthorized)
ctx.Request().Abort(http.StatusUnauthorized)
return
}

token = "Bearer " + token
} else {
// Token is invalid
ctx.Request().AbortWithStatus(http.StatusUnauthorized)
ctx.Request().Abort(http.StatusUnauthorized)
return
}
}
Expand Down
6 changes: 3 additions & 3 deletions app/http/middleware/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func Jwt() httpcontract.Middleware {
return func(ctx httpcontract.Context) {
token := ctx.Request().Header("Authorization", "")
if token == "" {
ctx.Request().AbortWithStatus(http.StatusUnauthorized)
ctx.Request().Abort(http.StatusUnauthorized)
return
}

Expand All @@ -22,13 +22,13 @@ func Jwt() httpcontract.Middleware {
token, err = facades.Auth(ctx).Refresh()
if err != nil {
// Refresh time exceeded
ctx.Request().AbortWithStatus(http.StatusUnauthorized)
ctx.Request().Abort(http.StatusUnauthorized)
return
}

token = "Bearer " + token
} else {
ctx.Request().AbortWithStatus(http.StatusUnauthorized)
ctx.Request().Abort(http.StatusUnauthorized)
return
}
}
Expand Down
6 changes: 5 additions & 1 deletion app/http/requests/user_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type UserCreate struct {
Tags []string `form:"tags" json:"tags"`
Scores []int `form:"scores" json:"scores"`
Date carbon.Carbon `form:"date" json:"date"`
Code int `form:"code" json:"code"`
}

func (r *UserCreate) Authorize(ctx http.Context) error {
Expand All @@ -24,6 +25,7 @@ func (r *UserCreate) Rules(ctx http.Context) map[string]string {
"tags.*": "required|string",
"scores.*": "required|int",
"date": "required|date",
"code": `required|regex:^\d{4,6}$`,
}
}

Expand All @@ -44,5 +46,7 @@ func (r *UserCreate) PrepareForValidation(ctx http.Context, data validation.Data
}

func (r *UserCreate) Filters(ctx http.Context) map[string]string {
return map[string]string{}
return map[string]string{
"name": "trim",
}
}
4 changes: 4 additions & 0 deletions app/providers/route_service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ func (receiver *RouteServiceProvider) Register(app foundation.Application) {
func (receiver *RouteServiceProvider) Boot(app foundation.Application) {
// Add HTTP middleware
facades.Route().GlobalMiddleware(http.Kernel{}.Middleware()...)
facades.Route().Recover(func(ctx contractshttp.Context, err any) {
ctx.Request().AbortWithStatus(contractshttp.StatusInternalServerError)
})

receiver.configureRateLimiting()

// Add routes
routes.Web()
routes.Api()
routes.Graphql()
routes.Test()
}

func (receiver *RouteServiceProvider) configureRateLimiting() {
Expand Down
2 changes: 2 additions & 0 deletions config/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func init() {
"host": config.Env("APP_HOST", "127.0.0.1"),
// HTTP Port
"port": config.Env("APP_PORT", "3000"),
// HTTP Timeout, default is 3 seconds
"request_timeout": 3,
// HTTPS Configuration
"tls": map[string]any{
// HTTPS Host
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ require (
github.com/gofiber/fiber/v2 v2.52.5
github.com/gofiber/template/html/v2 v2.1.2
github.com/goravel/example-proto v0.0.1
github.com/goravel/fiber v1.2.3-0.20241223085554-7ce6cfc651a4
github.com/goravel/framework v1.14.1-0.20241225031759-b261a622ba52
github.com/goravel/gin v1.2.3-0.20241223085040-c46775c141d8
github.com/goravel/fiber v1.2.3-0.20241230080004-45db67e182b3
github.com/goravel/framework v1.14.1-0.20241230074406-15bcb208658a
github.com/goravel/gin v1.2.3-0.20241230080008-9d244a75eecb
github.com/gorilla/websocket v1.5.0
github.com/opentracing/opentracing-go v1.2.0
github.com/pkg/errors v0.9.1
Expand Down Expand Up @@ -220,7 +220,7 @@ require (
gorm.io/gorm v1.25.12 // indirect
gorm.io/plugin/dbresolver v1.5.3 // indirect
modernc.org/libc v1.61.5 // indirect
modernc.org/mathutil v1.7.0 // indirect
modernc.org/mathutil v1.7.1 // indirect
modernc.org/memory v1.8.0 // indirect
modernc.org/sqlite v1.34.4 // indirect
)
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -441,14 +441,14 @@ github.com/gookit/validate v1.5.4 h1:nwBo6vULnVUeNFCOde6RKFRbOCKJXVMnWR0ghedacLg
github.com/gookit/validate v1.5.4/go.mod h1:p9sRPfpvYB4vXICBpEPzv8FoAky+XhUOhWQghgmmat4=
github.com/goravel/example-proto v0.0.1 h1:ZxETeKREQWjuJ49bX/Hqj1NLR5Vyj489Ks6dRxYeQsk=
github.com/goravel/example-proto v0.0.1/go.mod h1:I8IPsHr4Ndf7KxmdsRpBR2LQ0Geo48+pjv9IIWf3mZg=
github.com/goravel/fiber v1.2.3-0.20241223085554-7ce6cfc651a4 h1:spc4sjxiQJf20x2uhAgE9n4OZrlHE1dWOfm3NIIhE/E=
github.com/goravel/fiber v1.2.3-0.20241223085554-7ce6cfc651a4/go.mod h1:Bz164ScN/tUtjJR/w5g+eiL+PT9MFVU2JPBTY3DkW+4=
github.com/goravel/fiber v1.2.3-0.20241230080004-45db67e182b3 h1:nGxPSHlQC/uih1w3DiZih2vVNoK52IDLdQrxb/uyBQQ=
github.com/goravel/fiber v1.2.3-0.20241230080004-45db67e182b3/go.mod h1:P9QrJbetj2CE72aD6FLRRrAtzEju7hwdcnsK75d3zlM=
github.com/goravel/file-rotatelogs/v2 v2.4.2 h1:g68AzbePXcm0V2CpUMc9j4qVzcDn7+7aoWSjZ51C0m4=
github.com/goravel/file-rotatelogs/v2 v2.4.2/go.mod h1:23VuSW8cBS4ax5cmbV+5AaiLpq25b8UJ96IhbAkdo8I=
github.com/goravel/framework v1.14.1-0.20241225031759-b261a622ba52 h1:H9qpVdZUl1Jso6arYKfLylMYWR1J3ePOlBAwzIDfNs4=
github.com/goravel/framework v1.14.1-0.20241225031759-b261a622ba52/go.mod h1:Qo5Xlf+slrosyMxBKbNoxpEmzB6y2C5FI4BHNdVOSyI=
github.com/goravel/gin v1.2.3-0.20241223085040-c46775c141d8 h1:J5R2rmeDKacNr4tooDBBogx9QzPgli1A38sbWTB9AYo=
github.com/goravel/gin v1.2.3-0.20241223085040-c46775c141d8/go.mod h1:Vs0vxSTg6C7bhyrXYzGLjow6jAap7lHnSwPc3A9gJXQ=
github.com/goravel/framework v1.14.1-0.20241230074406-15bcb208658a h1:iVjoA10Ej3VE1XEGXdsQR+75waq8BGMr9pwfOvrLGCQ=
github.com/goravel/framework v1.14.1-0.20241230074406-15bcb208658a/go.mod h1:Qo5Xlf+slrosyMxBKbNoxpEmzB6y2C5FI4BHNdVOSyI=
github.com/goravel/gin v1.2.3-0.20241230080008-9d244a75eecb h1:PBLFjM/Xu/TWDmlwpJU2QG6W+E8yfIG3C32MBqax4Fg=
github.com/goravel/gin v1.2.3-0.20241230080008-9d244a75eecb/go.mod h1:VyaHPOq3bAYJE3zE3t2S7YwwjmrRSrGkFJohGdYzZNA=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
Expand Down Expand Up @@ -1251,8 +1251,8 @@ modernc.org/gc/v2 v2.6.0 h1:Tiw3pezQj7PfV8k4Dzyu/vhRHR2e92kOXtTFU8pbCl4=
modernc.org/gc/v2 v2.6.0/go.mod h1:wzN5dK1AzVGoH6XOzc3YZ+ey/jPgYHLuVckd62P0GYU=
modernc.org/libc v1.61.5 h1:WzsPUvWl2CvsRmk2foyWWHUEUmQ2iW4oFyWOVR0O5ho=
modernc.org/libc v1.61.5/go.mod h1:llBdEGIywhnRgAFuTF+CWaKV8/2bFgACcQZTXhkAuAM=
modernc.org/mathutil v1.7.0 h1:KPlMfpLMs4EXAo8T8JJEkmCT9KP/B4vU1+GaBnDhHQY=
modernc.org/mathutil v1.7.0/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
modernc.org/memory v1.8.0 h1:IqGTL6eFMaDZZhEWwcREgeMXYwmW83LYW8cROZYkg+E=
modernc.org/memory v1.8.0/go.mod h1:XPZ936zp5OMKGWPqbD3JShgd/ZoQ7899TUuQqxY+peU=
modernc.org/opt v0.1.3 h1:3XOZf2yznlhC+ibLltsDGzABUGVx8J6pnFMS3E4dcq4=
Expand Down
20 changes: 10 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,6 @@ func main() {
}
}()

// Listen for the OS signal
go func() {
<-quit
if err := facades.Route().Shutdown(); err != nil {
facades.Log().Errorf("Route Shutdown error: %v", err)
}

os.Exit(0)
}()

// Start grpc server by facades.Grpc().
go func() {
if err := facades.Grpc().Run(); err != nil {
Expand All @@ -49,5 +39,15 @@ func main() {
}
}()

// Listen for the OS signal
go func() {
<-quit
if err := facades.Route().Shutdown(); err != nil {
facades.Log().Errorf("Route Shutdown error: %v", err)
}

os.Exit(0)
}()

select {}
}
25 changes: 25 additions & 0 deletions routes/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,38 @@ package routes

import (
"github.com/goravel/framework/facades"
httpmiddleware "github.com/goravel/framework/http/middleware"

"goravel/app/http/controllers"
"goravel/app/http/middleware"
)

func Api() {
// Auth
authController := controllers.NewAuthController()
facades.Route().Post("auth/login", authController.Login)
facades.Route().Middleware(middleware.Auth()).Get("auth/info", authController.Info)

// 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)

// Localization
langController := controllers.NewLangController()
facades.Route().Middleware(middleware.Lang()).Get("lang", langController.Index)
}
55 changes: 55 additions & 0 deletions routes/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package routes

import (
"time"

"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/facades"

"goravel/app/http/controllers"
)

func Test() {
facades.Route().Resource("users", controllers.NewUserController())

facades.Route().Get("stream", func(ctx http.Context) http.Response {
return ctx.Response().Stream(http.StatusCreated, func(w http.StreamWriter) error {
data := []string{"a", "b", "c"}
for _, item := range data {
if _, err := w.Write([]byte(item + "\n")); err != nil {
return err
}

if err := w.Flush(); err != nil {
return err
}

time.Sleep(1 * time.Second)
}

return nil
})
})

facades.Route().Get("timeout", func(ctx http.Context) http.Response {
time.Sleep(10 * time.Second)
return ctx.Response().String(http.StatusOK, "ok")
})

facades.Route().Get("panic", func(ctx http.Context) http.Response {
panic("test panic")
})

facades.Route().Get("bind-query", func(ctx http.Context) http.Response {
type Query struct {
Name string `form:"name"`
}

var query Query
ctx.Request().BindQuery(&query)

return ctx.Response().Json(http.StatusOK, http.Json{
"name": query.Name,
})
})
}
Loading

0 comments on commit 0b2e8f3

Please sign in to comment.