Skip to content
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

Refactor routers directory #15800

Merged
merged 17 commits into from
Jun 8, 2021
4 changes: 1 addition & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ issues:
- path: modules/log/
linters:
- errcheck
- path: routers/routes/web.go
linters:
- dupl
- path: routers/api/v1/repo/issue_subscription.go
linters:
- dupl
Expand Down Expand Up @@ -114,3 +111,4 @@ issues:
linters:
- staticcheck
text: "svc.IsAnInteractiveSession is deprecated: Use IsWindowsService instead."

8 changes: 4 additions & 4 deletions cmd/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/routers"
"code.gitea.io/gitea/routers/routes"
"code.gitea.io/gitea/routers/install"

context2 "github.com/gorilla/context"
"github.com/urfave/cli"
Expand Down Expand Up @@ -88,7 +88,7 @@ func runWeb(ctx *cli.Context) error {
}

// Perform pre-initialization
needsInstall := routers.PreInstallInit(graceful.GetManager().HammerContext())
needsInstall := install.PreloadSettings(graceful.GetManager().HammerContext())
if needsInstall {
// Flag for port number in case first time run conflict
if ctx.IsSet("port") {
Expand All @@ -101,7 +101,7 @@ func runWeb(ctx *cli.Context) error {
return err
}
}
c := routes.InstallRoutes()
c := install.Routes()
err := listen(c, false)
select {
case <-graceful.GetManager().IsShutdown():
Expand Down Expand Up @@ -134,7 +134,7 @@ func runWeb(ctx *cli.Context) error {
}

// Set up Chi routes
c := routes.NormalRoutes()
c := routers.NormalRoutes()
err := listen(c, true)
<-graceful.GetManager().Done()
log.Info("PID: %d Gitea Web Finished", os.Getpid())
Expand Down
3 changes: 1 addition & 2 deletions contrib/pr/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers"
"code.gitea.io/gitea/routers/routes"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
Expand Down Expand Up @@ -116,7 +115,7 @@ func runPR() {
//routers.GlobalInit()
external.RegisterRenderers()
markup.Init()
c := routes.NormalRoutes()
c := routers.NormalRoutes()

log.Printf("[PR] Ready for testing !\n")
log.Printf("[PR] Login with user1, user2, user3, ... with pass: password\n")
Expand Down
6 changes: 3 additions & 3 deletions integrations/create_no_session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers/routes"
"code.gitea.io/gitea/routers"

"gitea.com/go-chi/session"
jsoniter "github.com/json-iterator/go"
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestSessionFileCreation(t *testing.T) {
oldSessionConfig := setting.SessionConfig.ProviderConfig
defer func() {
setting.SessionConfig.ProviderConfig = oldSessionConfig
c = routes.NormalRoutes()
c = routers.NormalRoutes()
}()

var config session.Options
Expand All @@ -84,7 +84,7 @@ func TestSessionFileCreation(t *testing.T) {

setting.SessionConfig.ProviderConfig = string(newConfigBytes)

c = routes.NormalRoutes()
c = routers.NormalRoutes()

t.Run("NoSessionOnViewIssue", func(t *testing.T) {
defer PrintCurrentTest(t)()
Expand Down
3 changes: 1 addition & 2 deletions integrations/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/routers"
"code.gitea.io/gitea/routers/routes"

"github.com/PuerkitoBio/goquery"
jsoniter "github.com/json-iterator/go"
Expand Down Expand Up @@ -88,7 +87,7 @@ func TestMain(m *testing.M) {
defer cancel()

initIntegrationTest()
c = routes.NormalRoutes()
c = routers.NormalRoutes()

// integration test settings...
if setting.Cfg != nil {
Expand Down
8 changes: 4 additions & 4 deletions integrations/lfs_getobject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/lfs"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/routers/routes"
"code.gitea.io/gitea/routers/web"

jsoniter "github.com/json-iterator/go"
gzipp "github.com/klauspost/compress/gzip"
Expand Down Expand Up @@ -99,7 +99,7 @@ func TestGetLFSLarge(t *testing.T) {
t.Skip()
return
}
content := make([]byte, routes.GzipMinSize*10)
content := make([]byte, web.GzipMinSize*10)
for i := range content {
content[i] = byte(i % 256)
}
Expand All @@ -115,7 +115,7 @@ func TestGetLFSGzip(t *testing.T) {
t.Skip()
return
}
b := make([]byte, routes.GzipMinSize*10)
b := make([]byte, web.GzipMinSize*10)
for i := range b {
b[i] = byte(i % 256)
}
Expand All @@ -136,7 +136,7 @@ func TestGetLFSZip(t *testing.T) {
t.Skip()
return
}
b := make([]byte, routes.GzipMinSize*10)
b := make([]byte, web.GzipMinSize*10)
for i := range b {
b[i] = byte(i % 256)
}
Expand Down
6 changes: 3 additions & 3 deletions routers/api/v1/repo/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"code.gitea.io/gitea/modules/repofiles"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/routers/repo"
"code.gitea.io/gitea/routers/common"
)

// GetRawFile get a file by path on a repository
Expand Down Expand Up @@ -83,7 +83,7 @@ func GetRawFile(ctx *context.APIContext) {
}
return
}
if err = repo.ServeBlob(ctx.Context, blob); err != nil {
if err = common.ServeBlob(ctx.Context, blob); err != nil {
ctx.Error(http.StatusInternalServerError, "ServeBlob", err)
}
}
Expand Down Expand Up @@ -126,7 +126,7 @@ func GetArchive(ctx *context.APIContext) {
ctx.Repo.GitRepo = gitRepo
defer gitRepo.Close()

repo.Download(ctx.Context)
common.Download(ctx.Context)
}

// GetEditorconfig get editor config of a repository
Expand Down
39 changes: 39 additions & 0 deletions routers/common/db.go
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) {
6543 marked this conversation as resolved.
Show resolved Hide resolved
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
}
33 changes: 33 additions & 0 deletions routers/common/logger.go
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)))
})
}
}
76 changes: 76 additions & 0 deletions routers/common/middleware.go
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
}
Loading