forked from go-gitea/gitea
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor routers directory (go-gitea#15800)
* refactor routers directory * move func used for web and api to common * make corsHandler a function to prohibit side efects * rm unused func Co-authored-by: 6543 <6543@obermui.de>
- Loading branch information
Showing
107 changed files
with
940 additions
and
800 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright 2021 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package common | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"code.gitea.io/gitea/models" | ||
"code.gitea.io/gitea/models/migrations" | ||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/setting" | ||
) | ||
|
||
// InitDBEngine In case of problems connecting to DB, retry connection. Eg, PGSQL in Docker Container on Synology | ||
func InitDBEngine(ctx context.Context) (err error) { | ||
log.Info("Beginning ORM engine initialization.") | ||
for i := 0; i < setting.Database.DBConnectRetries; i++ { | ||
select { | ||
case <-ctx.Done(): | ||
return fmt.Errorf("Aborted due to shutdown:\nin retry ORM engine initialization") | ||
default: | ||
} | ||
log.Info("ORM engine initialization attempt #%d/%d...", i+1, setting.Database.DBConnectRetries) | ||
if err = models.NewEngine(ctx, migrations.Migrate); err == nil { | ||
break | ||
} else if i == setting.Database.DBConnectRetries-1 { | ||
return err | ||
} | ||
log.Error("ORM engine initialization attempt #%d/%d failed. Error: %v", i+1, setting.Database.DBConnectRetries, err) | ||
log.Info("Backing off for %d seconds", int64(setting.Database.DBConnectBackoff/time.Second)) | ||
time.Sleep(setting.Database.DBConnectBackoff) | ||
} | ||
models.HasEngine = true | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2021 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package common | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
|
||
"code.gitea.io/gitea/modules/context" | ||
"code.gitea.io/gitea/modules/log" | ||
) | ||
|
||
// LoggerHandler is a handler that will log the routing to the default gitea log | ||
func LoggerHandler(level log.Level) func(next http.Handler) http.Handler { | ||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | ||
start := time.Now() | ||
|
||
_ = log.GetLogger("router").Log(0, level, "Started %s %s for %s", log.ColoredMethod(req.Method), req.URL.RequestURI(), req.RemoteAddr) | ||
|
||
next.ServeHTTP(w, req) | ||
|
||
var status int | ||
if v, ok := w.(context.ResponseWriter); ok { | ||
status = v.Status() | ||
} | ||
|
||
_ = log.GetLogger("router").Log(0, level, "Completed %s %s %v %s in %v", log.ColoredMethod(req.Method), req.URL.RequestURI(), log.ColoredStatus(status), log.ColoredStatus(status, http.StatusText(status)), log.ColoredTime(time.Since(start))) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2021 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package common | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/modules/context" | ||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/setting" | ||
|
||
"github.com/chi-middleware/proxy" | ||
"github.com/go-chi/chi/middleware" | ||
) | ||
|
||
// Middlewares returns common middlewares | ||
func Middlewares() []func(http.Handler) http.Handler { | ||
var handlers = []func(http.Handler) http.Handler{ | ||
func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) { | ||
next.ServeHTTP(context.NewResponse(resp), req) | ||
}) | ||
}, | ||
} | ||
|
||
if setting.ReverseProxyLimit > 0 { | ||
opt := proxy.NewForwardedHeadersOptions(). | ||
WithForwardLimit(setting.ReverseProxyLimit). | ||
ClearTrustedProxies() | ||
for _, n := range setting.ReverseProxyTrustedProxies { | ||
if !strings.Contains(n, "/") { | ||
opt.AddTrustedProxy(n) | ||
} else { | ||
opt.AddTrustedNetwork(n) | ||
} | ||
} | ||
handlers = append(handlers, proxy.ForwardedHeaders(opt)) | ||
} | ||
|
||
handlers = append(handlers, middleware.StripSlashes) | ||
|
||
if !setting.DisableRouterLog && setting.RouterLogLevel != log.NONE { | ||
if log.GetLogger("router").GetLevel() <= setting.RouterLogLevel { | ||
handlers = append(handlers, LoggerHandler(setting.RouterLogLevel)) | ||
} | ||
} | ||
if setting.EnableAccessLog { | ||
handlers = append(handlers, context.AccessLogger()) | ||
} | ||
|
||
handlers = append(handlers, func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) { | ||
// Why we need this? The Recovery() will try to render a beautiful | ||
// error page for user, but the process can still panic again, and other | ||
// middleware like session also may panic then we have to recover twice | ||
// and send a simple error page that should not panic any more. | ||
defer func() { | ||
if err := recover(); err != nil { | ||
combinedErr := fmt.Sprintf("PANIC: %v\n%s", err, string(log.Stack(2))) | ||
log.Error("%v", combinedErr) | ||
if setting.IsProd() { | ||
http.Error(resp, http.StatusText(500), 500) | ||
} else { | ||
http.Error(resp, combinedErr, 500) | ||
} | ||
} | ||
}() | ||
next.ServeHTTP(resp, req) | ||
}) | ||
}) | ||
return handlers | ||
} |
Oops, something went wrong.