Skip to content

Commit 07bc616

Browse files
committed
Use options
1 parent b70c346 commit 07bc616

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

foundation/console/down_command.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type DownCommand struct {
1616
app foundation.Application
1717
}
1818

19-
type DownOptions struct {
19+
type MaintenanceOptions struct {
2020
Reason string `json:"reason,omitempty"`
2121
Redirect string `json:"redirect,omitempty"`
2222
Render string `json:"render,omitempty"`
@@ -82,7 +82,7 @@ func (r *DownCommand) Handle(ctx console.Context) error {
8282
return nil
8383
}
8484

85-
options := DownOptions{}
85+
options := MaintenanceOptions{}
8686

8787
options.Status = ctx.OptionInt("status")
8888

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,51 @@
11
package middleware
22

33
import (
4+
"encoding/json"
5+
"os"
6+
47
"github.com/goravel/framework/contracts/http"
8+
"github.com/goravel/framework/foundation/console"
59
"github.com/goravel/framework/support/file"
610
"github.com/goravel/framework/support/path"
711
)
812

913
func CheckForMaintenance() http.Middleware {
1014
return func(ctx http.Context) {
1115
filepath := path.Storage("framework/maintenance")
12-
if file.Exists(filepath) {
13-
content, err := file.GetContent(filepath)
14-
15-
if err != nil {
16-
ctx.Request().Abort(http.StatusServiceUnavailable)
17-
return
18-
}
19-
20-
// Checking err to suppress the linter
21-
if err = ctx.Response().String(http.StatusServiceUnavailable, content).Abort(); err != nil {
22-
return
23-
}
16+
if !file.Exists(filepath) {
17+
ctx.Request().Next()
18+
}
19+
20+
content, err := os.ReadFile(filepath)
21+
22+
var maintenanceOptions *console.MaintenanceOptions
23+
err = json.Unmarshal(content, &maintenanceOptions)
24+
25+
if err != nil {
26+
ctx.Request().Abort(http.StatusServiceUnavailable)
27+
return
28+
}
29+
30+
secret := ctx.Request().Query("secret", "")
31+
if secret != "" && maintenanceOptions.Secret != "" && secret == maintenanceOptions.Secret {
32+
ctx.Request().Next()
2433
return
2534
}
2635

27-
ctx.Request().Next()
36+
if maintenanceOptions.Redirect != "" {
37+
ctx.Response().Redirect(http.StatusTemporaryRedirect, maintenanceOptions.Redirect)
38+
return
39+
}
40+
41+
if maintenanceOptions.Render != "" {
42+
ctx.Response().View().Make(maintenanceOptions.Render, nil).Render()
43+
return
44+
}
45+
46+
// Checking err to suppress the linter
47+
if err = ctx.Response().String(maintenanceOptions.Status, maintenanceOptions.Reason).Abort(); err != nil {
48+
return
49+
}
2850
}
2951
}

0 commit comments

Comments
 (0)