Skip to content

Commit 03b1616

Browse files
committed
Some changes to fix recovery
1 parent 4c2a1c0 commit 03b1616

File tree

20 files changed

+1618
-1
lines changed

20 files changed

+1618
-1
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ require (
9898
github.com/unknwon/com v1.0.1
9999
github.com/unknwon/i18n v0.0.0-20200823051745-09abd91c7f2c
100100
github.com/unknwon/paginater v0.0.0-20200328080006-042474bd0eae
101+
github.com/unrolled/render v1.0.3
101102
github.com/urfave/cli v1.22.5
102103
github.com/willf/bitset v1.1.11 // indirect
103104
github.com/xanzy/go-gitlab v0.39.0

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFP
283283
github.com/editorconfig/editorconfig-core-go/v2 v2.3.9 h1:4vZN3UCLAUbT408wDutTKGZwOlgGMpV3vhahYufNbV8=
284284
github.com/editorconfig/editorconfig-core-go/v2 v2.3.9/go.mod h1:yoHDFR3nO8O5ssvhITSRsf0owQqIs0c9+nBTtarunPo=
285285
github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M=
286+
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385 h1:clC1lXBpe2kTj2VHdaIu9ajZQe4kcEY9j0NsnDDBZ3o=
287+
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385/go.mod h1:0vRUJqYpeSZifjYj7uP3BG/gKcuzL9xWVV/Y+cK33KM=
286288
github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=
287289
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
288290
github.com/envoyproxy/go-control-plane v0.6.9/go.mod h1:SBwIajubJHhxtWwsL9s8ss4safvEdbitLhGGK48rN6g=
@@ -1086,6 +1088,8 @@ github.com/unknwon/i18n v0.0.0-20200823051745-09abd91c7f2c h1:679/gJXwrsHC3RATr0
10861088
github.com/unknwon/i18n v0.0.0-20200823051745-09abd91c7f2c/go.mod h1:+5rDk6sDGpl3azws3O+f+GpFSyN9GVr0K8cvQLQM2ZQ=
10871089
github.com/unknwon/paginater v0.0.0-20200328080006-042474bd0eae h1:ihaXiJkaca54IaCSnEXtE/uSZOmPxKZhDfVLrzZLFDs=
10881090
github.com/unknwon/paginater v0.0.0-20200328080006-042474bd0eae/go.mod h1:1fdkY6xxl6ExVs2QFv7R0F5IRZHKA8RahhB9fMC9RvM=
1091+
github.com/unrolled/render v1.0.3 h1:baO+NG1bZSF2WR4zwh+0bMWauWky7DVrTOfvE2w+aFo=
1092+
github.com/unrolled/render v1.0.3/go.mod h1:gN9T0NhL4Bfbwu8ann7Ry/TGHYfosul+J0obPf6NBdM=
10891093
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
10901094
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
10911095
github.com/urfave/cli v1.22.5 h1:lNq9sAHXK2qfdI8W+GRItjCEkI+2oR4d+MEHy1CKXoU=

modules/templates/base.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package templates
6+
7+
import (
8+
"time"
9+
10+
"code.gitea.io/gitea/modules/setting"
11+
)
12+
13+
// Vars represents variables to be render in golang templates
14+
type Vars map[string]interface{}
15+
16+
// Merge merges another vars to the current, another Vars will override the current
17+
func (vars Vars) Merge(another map[string]interface{}) Vars {
18+
for k, v := range another {
19+
vars[k] = v
20+
}
21+
return vars
22+
}
23+
24+
// BaseVars returns all basic vars
25+
func BaseVars() Vars {
26+
var startTime = time.Now()
27+
return map[string]interface{}{
28+
"IsLandingPageHome": setting.LandingPageURL == setting.LandingPageHome,
29+
"IsLandingPageExplore": setting.LandingPageURL == setting.LandingPageExplore,
30+
"IsLandingPageOrganizations": setting.LandingPageURL == setting.LandingPageOrganizations,
31+
32+
"ShowRegistrationButton": setting.Service.ShowRegistrationButton,
33+
"ShowMilestonesDashboardPage": setting.Service.ShowMilestonesDashboardPage,
34+
"ShowFooterBranding": setting.ShowFooterBranding,
35+
"ShowFooterVersion": setting.ShowFooterVersion,
36+
37+
"EnableSwagger": setting.API.EnableSwagger,
38+
"EnableOpenIDSignIn": setting.Service.EnableOpenIDSignIn,
39+
"PageStartTime": startTime,
40+
"TmplLoadTimes": func() string {
41+
return time.Since(startTime).String()
42+
},
43+
}
44+
}

modules/templates/dynamic.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
package templates
88

99
import (
10+
"fmt"
1011
"html/template"
1112
"io/ioutil"
13+
"os"
1214
"path"
15+
"path/filepath"
1316
"strings"
1417
texttmpl "text/template"
1518

@@ -25,6 +28,58 @@ var (
2528
bodyTemplates = template.New("")
2629
)
2730

31+
func GetAsset(name string) ([]byte, error) {
32+
fmt.Println("=====2", name)
33+
34+
bs, err := ioutil.ReadFile(filepath.Join(setting.CustomPath, name))
35+
if err != nil && !os.IsNotExist(err) {
36+
return nil, err
37+
} else if err == nil {
38+
return bs, nil
39+
}
40+
41+
return ioutil.ReadFile(filepath.Join(setting.StaticRootPath, name))
42+
}
43+
44+
func GetAssetNames() []string {
45+
tmpls := getDirAssetNames(filepath.Join(setting.CustomPath, "templates"))
46+
tmpls2 := getDirAssetNames(filepath.Join(setting.StaticRootPath, "templates"))
47+
return append(tmpls, tmpls2...)
48+
}
49+
50+
func getDirAssetNames(dir string) []string {
51+
var tmpls []string
52+
isDir, err := util.IsDir(dir)
53+
if err != nil {
54+
log.Warn("Unable to check if templates dir %s is a directory. Error: %v", dir, err)
55+
return tmpls
56+
}
57+
if !isDir {
58+
log.Warn("Templates dir %s is a not directory.", dir)
59+
return tmpls
60+
}
61+
62+
files, err := com.StatDir(dir)
63+
if err != nil {
64+
log.Warn("Failed to read %s templates dir. %v", dir, err)
65+
return tmpls
66+
}
67+
for _, filePath := range files {
68+
if strings.HasPrefix(filePath, "mail/") {
69+
continue
70+
}
71+
72+
if !strings.HasSuffix(filePath, ".tmpl") {
73+
continue
74+
}
75+
76+
fmt.Println("=======3333", filePath, filePath)
77+
78+
tmpls = append(tmpls, "templates/"+filePath)
79+
}
80+
return tmpls
81+
}
82+
2883
// HTMLRenderer implements the macaron handler for serving HTML templates.
2984
func HTMLRenderer() macaron.Handler {
3085
return macaron.Renderer(macaron.RenderOptions{

modules/templates/static.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,45 @@ func (templates templateFileSystem) Get(name string) (io.Reader, error) {
4646
return nil, fmt.Errorf("file '%s' not found", name)
4747
}
4848

49+
func GetAsset(name string) ([]byte, error) {
50+
customDir := filepath.Join(setting.CustomPath, "templates")
51+
bs, err := ioutil.ReadFile(filepath.Join(customDir, name))
52+
if err != nil && !os.IsNotExist(err) {
53+
return nil, err
54+
} else if err == nil {
55+
return bs, nil
56+
}
57+
return Asset(name)
58+
}
59+
60+
func GetAssetNames() []string {
61+
var tmpls = AssetNames()
62+
customDir := path.Join(setting.CustomPath, "templates")
63+
isDir, err := util.IsDir(customDir)
64+
if err != nil {
65+
log.Warn("Unable to check if templates dir %s is a directory. Error: %v", customDir, err)
66+
return tmpls
67+
}
68+
69+
files, err := com.StatDir(customDir)
70+
if err != nil {
71+
log.Warn("Failed to read %s templates dir. %v", customDir, err)
72+
return tmpls
73+
}
74+
for _, filePath := range files {
75+
if strings.HasPrefix(filePath, "mail/") {
76+
continue
77+
}
78+
79+
if !strings.HasSuffix(filePath, ".tmpl") {
80+
continue
81+
}
82+
83+
tmpls = append(tmpls, filePath)
84+
}
85+
return tmpls
86+
}
87+
4988
func NewTemplateFileSystem() templateFileSystem {
5089
fs := templateFileSystem{}
5190
fs.files = make([]macaron.TemplateFile, 0, 10)

routers/routes/chi.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ import (
2222
"code.gitea.io/gitea/modules/public"
2323
"code.gitea.io/gitea/modules/setting"
2424
"code.gitea.io/gitea/modules/storage"
25+
"code.gitea.io/gitea/modules/templates"
2526
"code.gitea.io/gitea/routers"
2627

2728
"github.com/go-chi/chi"
2829
"github.com/go-chi/chi/middleware"
2930
"github.com/prometheus/client_golang/prometheus"
31+
"github.com/unrolled/render"
3032
)
3133

3234
type routerLoggerOptions struct {
@@ -102,11 +104,34 @@ func LoggerHandler(level log.Level) func(next http.Handler) http.Handler {
102104
// with the gitea 500 page.
103105
func Recovery() func(next http.Handler) http.Handler {
104106
return func(next http.Handler) http.Handler {
107+
rnd := render.New(render.Options{
108+
Extensions: []string{".tmpl"},
109+
Directory: "templates",
110+
Funcs: templates.NewFuncMap(),
111+
Asset: templates.GetAsset,
112+
AssetNames: templates.GetAssetNames,
113+
IsDevelopment: setting.RunMode != "prod",
114+
})
115+
105116
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
106117
defer func() {
107118
if err := recover(); err != nil {
108119
combinedErr := fmt.Sprintf("PANIC: %v\n%s", err, string(log.Stack(2)))
109-
http.Error(w, combinedErr, 500)
120+
log.Error("%v", combinedErr)
121+
122+
var (
123+
data = templates.Vars{
124+
"Language": ctx.Locale.Language(),
125+
"CurrentURL": setting.AppSubURL + req.URL.RequestURI(),
126+
}
127+
)
128+
if setting.RunMode != "prod" {
129+
data["ErrMsg"] = combinedErr
130+
}
131+
err := rnd.HTML(w, 500, "status/500", templates.BaseVars().Merge(data))
132+
if err != nil {
133+
log.Error("%v", err)
134+
}
110135
}
111136
}()
112137

@@ -202,6 +227,7 @@ func NewChi() chi.Router {
202227
c.Use(LoggerHandler(setting.RouterLogLevel))
203228
}
204229
}
230+
205231
c.Use(Recovery())
206232
if setting.EnableAccessLog {
207233
setupAccessLogger(c)
@@ -233,6 +259,7 @@ func RegisterInstallRoute(c chi.Router) {
233259
// We need at least one handler in chi so that it does not drop
234260
// our middleware: https://github.com/go-gitea/gitea/issues/13725#issuecomment-735244395
235261
c.Get("/", func(w http.ResponseWriter, req *http.Request) {
262+
panic("lll")
236263
m.ServeHTTP(w, req)
237264
})
238265

vendor/github.com/unrolled/render/.gitignore

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/unrolled/render/.travis.yml

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/unrolled/render/LICENSE

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)