Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: merge master into v1.14.x #31

Merged
merged 10 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions .env
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
APP_NAME=Goravel
APP_ENV=local
APP_KEY=h20Gmg7UIuUDD66gfUSUjIkPLU8KLqMK
APP_KEY=ABCDEFGHIJKLMNOPQRSTUVWXYZ123456
APP_DEBUG=true
APP_URL=http://localhost
APP_HOST=127.0.0.1
Expand All @@ -9,17 +9,17 @@ APP_PORT=3000
GRPC_HOST=127.0.0.1
GRPC_PORT=3001

JWT_SECRET=L9qknFZyWHDSVXXe1TjUsR7XQsxkbb8B
JWT_SECRET=ABCDEFGHIJKLMNOPQRSTUVWXYZ123456

LOG_CHANNEL=stack
LOG_LEVEL=debug

DB_CONNECTION=postgres
DB_HOST=127.0.0.1
DB_PORT=5432
DB_CONNECTION=sqlite
DB_HOST=
DB_PORT=
DB_DATABASE=goravel
DB_USERNAME=goravel
DB_PASSWORD=goravel
DB_USERNAME=
DB_PASSWORD=

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=
Expand Down
7 changes: 5 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ APP_URL=http://localhost
APP_HOST=127.0.0.1
APP_PORT=3000

GRPC_HOST=127.0.0.1
GRPC_PORT=3001
GRPC_HOST=
GRPC_PORT=

JWT_SECRET=

Expand All @@ -21,6 +21,9 @@ DB_DATABASE=goravel
DB_USERNAME=root
DB_PASSWORD=

SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=
REDIS_PORT=6379
Expand Down
9 changes: 0 additions & 9 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@ jobs:
go: [ "1.21", "1.22" ]
runs-on: ubuntu-latest
steps:
- name: Set up PostgreSQL
uses: harmon758/postgresql-action@v1
with:
postgresql version: '11'
postgresql db: 'goravel'
postgresql user: 'goravel'
postgresql password: 'goravel'
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
Expand All @@ -33,7 +26,5 @@ jobs:
${{ runner.os }}-go-
- name: Install dependencies
run: go mod tidy
- name: Run migrate
run: go run . artisan migrate
- name: Run tests
run: go test -timeout 1h ./...
7 changes: 4 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.18.3-alpine3.16 AS builder
FROM golang:alpine AS builder

ENV GO111MODULE=on \
CGO_ENABLED=0 \
Expand All @@ -10,14 +10,15 @@ COPY . .
RUN go mod tidy
RUN go build --ldflags "-extldflags -static" -o main .

FROM alpine:3.16
FROM alpine:latest

WORKDIR /www

COPY --from=builder /build/main /www/
COPY --from=builder /build/database/ /www/database/
COPY --from=builder /build/public/ /www/public/
COPY --from=builder /build/storage/ /www/storage/
COPY --from=builder /build/resources/ /www/resources/
COPY --from=builder /build/.env /www/.env

ENTRYPOINT ["/www/main"]
ENTRYPOINT ["/www/main"]
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ 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#L41)
[routes/web.go](https://github.com/goravel/example/blob/master/routes/web.go#L44)

### View nesting

[routes/web.go](https://github.com/goravel/example/blob/master/routes/web.go#L52)
[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#L60)
[routes/web.go](https://github.com/goravel/example/blob/master/routes/web.go#L61)

### Session

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

### ε•ι‘΅ι’ε‰η«―εΊ”η”¨ι›†ζˆεˆ°ζ‘†ζžΆ

[routes/web.go](https://github.com/goravel/example/blob/master/routes/web.go#L43)
[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#L52)
[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#L60)
[routes/web.go](https://github.com/goravel/example/blob/master/routes/web.go#L61)

### Session

Expand Down
70 changes: 70 additions & 0 deletions app/http/controllers/auth_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package controllers

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

"goravel/app/models"
)

type AuthController struct {
// Dependent services
}

func NewAuthController() *AuthController {
return &AuthController{
// Inject services
}
}

func (r *AuthController) Login(ctx http.Context) http.Response {
// Create a user
var user models.User
if err := facades.Orm().Query().FirstOrCreate(&user, models.User{
Name: ctx.Request().Input("name", "Goravel"),
}); err != nil {
return ctx.Response().Json(http.StatusInternalServerError, http.Json{
"error": err.Error(),
})
}

var (
token string
err error
)

// Use different guards to login
if guard := ctx.Request().Header("Guard"); guard == "" {
token, err = facades.Auth(ctx).LoginUsingID(user.ID)
if err != nil {
return ctx.Response().String(http.StatusInternalServerError, err.Error())
}
} else {
token, err = facades.Auth(ctx).Guard(guard).Login(user)
if err != nil {
return ctx.Response().String(http.StatusInternalServerError, err.Error())
}
}

return ctx.Response().Header("Authorization", "Bearer "+token).Success().Json(http.Json{
"user": user,
})
}

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

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

return ctx.Response().Success().Json(http.Json{
"user": user,
})
}
94 changes: 94 additions & 0 deletions app/http/controllers/user_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package controllers

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

"goravel/app/models"
)

type UserController struct {
//Dependent services
}

func NewUserController() *UserController {
return &UserController{
//Inject services
}
}

func (r *UserController) Index(ctx http.Context) http.Response {
var users []models.User
if err := facades.Orm().Query().Get(&users); err != nil {
return ctx.Response().Json(http.StatusBadRequest, http.Json{
"error": err.Error(),
})
}

return ctx.Response().Success().Json(http.Json{
"users": users,
})
}

func (r *UserController) Show(ctx http.Context) http.Response {
var user models.User
if err := facades.Orm().Query().Where("id", ctx.Request().Input("id")).First(&user); err != nil {
return ctx.Response().Json(http.StatusBadRequest, http.Json{
"error": err.Error(),
})
}

return ctx.Response().Success().Json(http.Json{
"user": user,
})
}

func (r *UserController) Store(ctx http.Context) http.Response {
user := models.User{
Name: ctx.Request().Input("name"),
Avatar: ctx.Request().Input("avatar"),
}
if err := facades.Orm().Query().Create(&user); err != nil {
return ctx.Response().Json(http.StatusBadRequest, http.Json{
"error": err.Error(),
})
}

return ctx.Response().Success().Json(http.Json{
"user": user,
})
}

func (r *UserController) Update(ctx http.Context) http.Response {
if _, err := facades.Orm().Query().Where("id", ctx.Request().Input("id")).Update(models.User{
Name: ctx.Request().Input("name"),
}); err != nil {
return ctx.Response().Json(http.StatusBadRequest, http.Json{
"error": err.Error(),
})
}

var user models.User
if err := facades.Orm().Query().Where("id", ctx.Request().Input("id")).First(&user); err != nil {
return ctx.Response().Json(http.StatusBadRequest, http.Json{
"error": err.Error(),
})
}

return ctx.Response().Success().Json(http.Json{
"user": user,
})
}

func (r *UserController) Destroy(ctx http.Context) http.Response {
result, err := facades.Orm().Query().Where("id", ctx.Request().Input("id")).Delete(&models.User{})
if err != nil {
return ctx.Response().Json(http.StatusBadRequest, http.Json{
"error": err.Error(),
})
}

return ctx.Response().Success().Json(http.Json{
"rows_affected": result.RowsAffected,
})
}
11 changes: 10 additions & 1 deletion app/http/controllers/validation_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/goravel/framework/support/carbon"

"goravel/app/http/requests"
"goravel/app/models"
Expand All @@ -29,6 +30,11 @@ air
4.9 curl --location --request POST 'http://127.0.0.1:3000/validation/form' --header 'Content-Type: multipart/form-data' --form 'name="goravel"'
********************************/

type User struct {
Name string `json:"name" form:"name"`
Date carbon.DateTime `json:"date" form:"date"`
}

type ValidationController struct {
// Dependent services
}
Expand All @@ -42,6 +48,7 @@ func NewValidationController() *ValidationController {
func (r *ValidationController) Json(ctx http.Context) http.Response {
validator, err := ctx.Request().Validate(map[string]string{
"name": "required",
"date": "required|date",
})
if err != nil {
return ctx.Response().Json(http.StatusBadRequest, http.Json{
Expand All @@ -54,7 +61,7 @@ func (r *ValidationController) Json(ctx http.Context) http.Response {
})
}

var user models.User
var user User
if err := validator.Bind(&user); err != nil {
return ctx.Response().Json(http.StatusBadRequest, http.Json{
"message": err.Error(),
Expand All @@ -63,6 +70,7 @@ func (r *ValidationController) Json(ctx http.Context) http.Response {

return ctx.Response().Success().Json(http.Json{
"name": user.Name,
"date": user.Date.ToDateTimeString(),
})
}

Expand All @@ -84,6 +92,7 @@ func (r *ValidationController) Request(ctx http.Context) http.Response {
"name": userCreate.Name,
"tags": userCreate.Tags,
"scores": userCreate.Scores,
"date": userCreate.Date.ToDateTimeString(),
})
}

Expand Down
10 changes: 6 additions & 4 deletions app/http/controllers/validation_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"testing"

"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/support/carbon"
testingmock "github.com/goravel/framework/testing/mock"
"github.com/goravel/gin"
"github.com/stretchr/testify/suite"

"goravel/app/models"
)

type ValidationControllerTestSuite struct {
Expand Down Expand Up @@ -36,11 +35,13 @@ func (s *ValidationControllerTestSuite) TestJson() {
mockContext.EXPECT().Request().Return(mockRequest).Once()
mockRequest.EXPECT().Validate(map[string]string{
"name": "required",
"date": "required|date",
}).Return(mockValidator, nil).Once()
mockValidator.EXPECT().Fails().Return(false).Once()
var user models.User
var user User
mockValidator.EXPECT().Bind(&user).Run(func(user any) {
user.(*models.User).Name = "Goravel"
user.(*User).Name = "Goravel"
user.(*User).Date = carbon.NewDateTime(carbon.Parse("2024-07-08 22:34:31"))
}).Return(nil).Once()
mockContext.EXPECT().Response().Return(mockResponse).Once()
mockResponseStatus := mockFactory.ResponseStatus()
Expand All @@ -49,6 +50,7 @@ func (s *ValidationControllerTestSuite) TestJson() {
resp := &gin.JsonResponse{}
mockResponseStatus.EXPECT().Json(http.Json{
"name": "Goravel",
"date": "2024-07-08 22:34:31",
}).Return(resp).Once()

s.Equal(resp, NewValidationController().Json(mockContext))
Expand Down
Loading