-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Refactor routers directory #15800
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
Merged
Merged
Refactor routers directory #15800
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a9c6c24
Refactor routers directory
lunny 37071d6
Fix lint
lunny d1b4859
Fix lint
lunny b3e627b
Fix lint
lunny b88b2f7
merge
lunny 62a91aa
Fix lint
lunny 5f507c7
Fix test
lunny 3d60f16
routers: move func used for web and api to common
6543 2da6968
make corsHandler a function to prohibit side efects
6543 ab3e40b
move CorsHandler to common
6543 57f2698
rm unused func
6543 71a255e
make linter happy
6543 f4c98a4
rm
6543 140337a
Revert "move CorsHandler to common"
6543 069fa9d
Follow @zeripath's review
lunny 37b84b8
please linter
6543 b75a005
Merge branch 'main' into lunny/refactor_routers_directory
zeripath File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.