|
1 | 1 | package middleware |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + |
4 | 7 | "github.com/goravel/framework/contracts/http" |
| 8 | + "github.com/goravel/framework/foundation/console" |
5 | 9 | "github.com/goravel/framework/support/file" |
6 | 10 | "github.com/goravel/framework/support/path" |
7 | 11 | ) |
8 | 12 |
|
9 | 13 | func CheckForMaintenance() http.Middleware { |
10 | 14 | return func(ctx http.Context) { |
11 | 15 | 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() |
24 | 33 | return |
25 | 34 | } |
26 | 35 |
|
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 | + } |
28 | 50 | } |
29 | 51 | } |
0 commit comments