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

Share HTML template renderers and create a watcher framework #20218

Merged
merged 28 commits into from
Aug 28, 2022
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
1409281
Share HTML template renderers and create a watcher framework
zeripath Jun 28, 2022
07dc4f2
placate lint
zeripath Jul 3, 2022
f265ce5
fix windows
zeripath Jul 3, 2022
726bf7c
Merge branch 'main' into load-templates-once-and-better-reload
zeripath Jul 4, 2022
aa90128
Init needs a ctx
zeripath Jul 4, 2022
0209fa4
make SSPI also share the templates too
zeripath Jul 4, 2022
a04e94a
Merge branch 'main' into load-templates-once-and-better-reload
zeripath Jul 5, 2022
7dd067b
Merge branch 'main' into load-templates-once-and-better-reload
zeripath Jul 6, 2022
f61f79c
Merge branch 'main' into load-templates-once-and-better-reload
zeripath Jul 9, 2022
cbdc8bc
use todo only
zeripath Jul 10, 2022
9e63150
Merge branch 'main' into load-templates-once-and-better-reload
zeripath Jul 12, 2022
1159e0f
Merge branch 'main' into load-templates-once-and-better-reload
zeripath Jul 12, 2022
959595b
Switch to use syncthing/notify instead of fsnotify/fsnotify
zeripath Jul 16, 2022
aca2416
Merge remote-tracking branch 'origin/main' into load-templates-once-a…
zeripath Jul 16, 2022
426eb8f
Revert "Switch to use syncthing/notify instead of fsnotify/fsnotify"
zeripath Jul 17, 2022
f3b8468
Merge branch 'main' into load-templates-once-and-better-reload
zeripath Jul 31, 2022
eb8cf0f
Merge remote-tracking branch 'origin/main' into load-templates-once-a…
zeripath Aug 12, 2022
408a726
Merge branch 'main' into load-templates-once-and-better-reload
zeripath Aug 22, 2022
ab27d13
Merge branch 'main' into load-templates-once-and-better-reload
zeripath Aug 23, 2022
bfab646
Merge remote-tracking branch 'origin/main' into load-templates-once-a…
zeripath Aug 24, 2022
3841573
as per review
zeripath Aug 24, 2022
2dbfc50
Fix tests
zeripath Aug 24, 2022
0ff8921
placate lint
zeripath Aug 24, 2022
a442905
Merge branch 'main' into load-templates-once-and-better-reload
zeripath Aug 27, 2022
c5bdfef
Update modules/templates/htmlrenderer.go
zeripath Aug 27, 2022
55c4ec6
placate lint
zeripath Aug 27, 2022
f7a5cd7
as per wxiaoguang
zeripath Aug 28, 2022
aec5c2f
Merge remote-tracking branch 'origin/main' into load-templates-once-a…
zeripath Aug 28, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/embedded.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func initEmbeddedExtractor(c *cli.Context) error {

sections["public"] = &section{Path: "public", Names: public.AssetNames, IsDir: public.AssetIsDir, Asset: public.Asset}
sections["options"] = &section{Path: "options", Names: options.AssetNames, IsDir: options.AssetIsDir, Asset: options.Asset}
sections["templates"] = &section{Path: "templates", Names: templates.AssetNames, IsDir: templates.AssetIsDir, Asset: templates.Asset}
sections["templates"] = &section{Path: "templates", Names: templates.BuiltinAssetNames, IsDir: templates.BuiltinAssetIsDir, Asset: templates.BuiltinAsset}

for _, sec := range sections {
assets = append(assets, buildAssetList(sec, pats, c)...)
Expand Down
6 changes: 4 additions & 2 deletions cmd/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,10 @@ func runWeb(ctx *cli.Context) error {
return err
}
}
c := install.Routes()
installCtx, cancel := context.WithCancel(graceful.GetManager().HammerContext())
c := install.Routes(installCtx)
err := listen(c, false)
cancel()
if err != nil {
log.Critical("Unable to open listener for installer. Is Gitea already running?")
graceful.GetManager().DoGracefulShutdown()
Expand Down Expand Up @@ -175,7 +177,7 @@ func runWeb(ctx *cli.Context) error {
}

// Set up Chi routes
c := routers.NormalRoutes()
c := routers.NormalRoutes(graceful.GetManager().HammerContext())
err := listen(c, true)
<-graceful.GetManager().Done()
log.Info("PID: %d Gitea Web Finished", os.Getpid())
Expand Down
3 changes: 2 additions & 1 deletion contrib/pr/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
gitea_git "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/markup/external"
repo_module "code.gitea.io/gitea/modules/repository"
Expand Down Expand Up @@ -117,7 +118,7 @@ func runPR() {
// routers.GlobalInit()
external.RegisterRenderers()
markup.Init()
c := routers.NormalRoutes()
c := routers.NormalRoutes(graceful.GetManager().HammerContext())

log.Printf("[PR] Ready for testing !\n")
log.Printf("[PR] Login with user1, user2, user3, ... with pass: password\n")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
github.com/emirpasic/gods v1.18.1
github.com/ethantkoenig/rupture v1.0.1
github.com/felixge/fgprof v0.9.2
github.com/fsnotify/fsnotify v1.5.4
zeripath marked this conversation as resolved.
Show resolved Hide resolved
github.com/gliderlabs/ssh v0.3.4
github.com/go-ap/activitypub v0.0.0-20220615144428-48208c70483b
github.com/go-ap/jsonld v0.0.0-20220615144122-1d862b15410d
Expand Down Expand Up @@ -161,7 +162,6 @@ require (
github.com/envoyproxy/protoc-gen-validate v0.6.2 // indirect
github.com/felixge/httpsnoop v1.0.2 // indirect
github.com/form3tech-oss/jwt-go v3.2.3+incompatible // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/fullstorydev/grpcurl v1.8.1 // indirect
github.com/fxamacker/cbor/v2 v2.4.0 // indirect
github.com/go-ap/errors v0.0.0-20220615144307-e8bc4a40ae9f // indirect
Expand Down
12 changes: 6 additions & 6 deletions integrations/api_activitypub_person_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import (

func TestActivityPubPerson(t *testing.T) {
setting.Federation.Enabled = true
c = routers.NormalRoutes()
c = routers.NormalRoutes(context.TODO())
zeripath marked this conversation as resolved.
Show resolved Hide resolved
defer func() {
setting.Federation.Enabled = false
c = routers.NormalRoutes()
c = routers.NormalRoutes(context.TODO())
}()

onGiteaRun(t, func(*testing.T, *url.URL) {
Expand Down Expand Up @@ -60,10 +60,10 @@ func TestActivityPubPerson(t *testing.T) {

func TestActivityPubMissingPerson(t *testing.T) {
setting.Federation.Enabled = true
c = routers.NormalRoutes()
c = routers.NormalRoutes(context.TODO())
defer func() {
setting.Federation.Enabled = false
c = routers.NormalRoutes()
c = routers.NormalRoutes(context.TODO())
}()

onGiteaRun(t, func(*testing.T, *url.URL) {
Expand All @@ -75,10 +75,10 @@ func TestActivityPubMissingPerson(t *testing.T) {

func TestActivityPubPersonInbox(t *testing.T) {
setting.Federation.Enabled = true
c = routers.NormalRoutes()
c = routers.NormalRoutes(context.TODO())
defer func() {
setting.Federation.Enabled = false
c = routers.NormalRoutes()
c = routers.NormalRoutes(context.TODO())
}()

srv := httptest.NewServer(c)
Expand Down
5 changes: 3 additions & 2 deletions integrations/api_nodeinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package integrations

import (
"context"
"net/http"
"net/url"
"testing"
Expand All @@ -18,10 +19,10 @@ import (

func TestNodeinfo(t *testing.T) {
setting.Federation.Enabled = true
c = routers.NormalRoutes()
c = routers.NormalRoutes(context.TODO())
defer func() {
setting.Federation.Enabled = false
c = routers.NormalRoutes()
c = routers.NormalRoutes(context.TODO())
}()

onGiteaRun(t, func(*testing.T, *url.URL) {
Expand Down
5 changes: 3 additions & 2 deletions integrations/create_no_session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package integrations

import (
"context"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -57,7 +58,7 @@ func TestSessionFileCreation(t *testing.T) {
oldSessionConfig := setting.SessionConfig.ProviderConfig
defer func() {
setting.SessionConfig.ProviderConfig = oldSessionConfig
c = routers.NormalRoutes()
c = routers.NormalRoutes(context.TODO())
}()

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

setting.SessionConfig.ProviderConfig = string(newConfigBytes)

c = routers.NormalRoutes()
c = routers.NormalRoutes(context.TODO())

t.Run("NoSessionOnViewIssue", func(t *testing.T) {
defer PrintCurrentTest(t)()
Expand Down
2 changes: 1 addition & 1 deletion integrations/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestMain(m *testing.M) {
defer cancel()

initIntegrationTest()
c = routers.NormalRoutes()
c = routers.NormalRoutes(context.TODO())

// integration test settings...
if setting.Cfg != nil {
Expand Down
15 changes: 11 additions & 4 deletions modules/charset/escape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,18 @@ then resh (ר), and finally heh (ה) (which should appear leftmost).`,
},
}

type nullLocale struct{}

func (nullLocale) Language() string { return "" }
func (nullLocale) Tr(key string, _ ...interface{}) string { return key }
func (nullLocale) TrN(cnt interface{}, key1, keyN string, args ...interface{}) string { return "" }

var _ (translation.Locale) = nullLocale{}

func TestEscapeControlString(t *testing.T) {
for _, tt := range escapeControlTests {
t.Run(tt.name, func(t *testing.T) {
locale := translation.NewLocale("en_US")
status, result := EscapeControlString(tt.text, locale)
status, result := EscapeControlString(tt.text, nullLocale{})
if !reflect.DeepEqual(*status, tt.status) {
t.Errorf("EscapeControlString() status = %v, wanted= %v", status, tt.status)
}
Expand Down Expand Up @@ -173,7 +180,7 @@ func TestEscapeControlReader(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
input := strings.NewReader(tt.text)
output := &strings.Builder{}
status, err := EscapeControlReader(input, output, translation.NewLocale("en_US"))
status, err := EscapeControlReader(input, output, nullLocale{})
result := output.String()
if err != nil {
t.Errorf("EscapeControlReader(): err = %v", err)
Expand All @@ -195,5 +202,5 @@ func TestEscapeControlReader_panic(t *testing.T) {
for i := 0; i < 6826; i++ {
bs = append(bs, []byte("—")...)
}
_, _ = EscapeControlString(string(bs), translation.NewLocale("en_US"))
_, _ = EscapeControlString(string(bs), nullLocale{})
}
4 changes: 2 additions & 2 deletions modules/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,8 +658,8 @@ func Auth(authMethod auth.Method) func(*Context) {
}

// Contexter initializes a classic context for a request.
func Contexter() func(next http.Handler) http.Handler {
rnd := templates.HTMLRenderer()
func Contexter(ctx context.Context) func(next http.Handler) http.Handler {
_, rnd := templates.HTMLRenderer(ctx)
csrfOpts := getCsrfOpts()
if !setting.IsProd {
CsrfTokenRegenerationInterval = 5 * time.Second // in dev, re-generate the tokens more aggressively for debug purpose
Expand Down
10 changes: 7 additions & 3 deletions modules/context/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package context

import (
gocontext "context"
"fmt"
"net/http"

Expand All @@ -14,6 +15,7 @@ import (
"code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/templates"
)

// Package contains owner, access mode and optional the package descriptor
Expand Down Expand Up @@ -118,12 +120,14 @@ func packageAssignment(ctx *Context, errCb func(int, string, interface{})) {
}

// PackageContexter initializes a package context for a request.
func PackageContexter() func(next http.Handler) http.Handler {
func PackageContexter(ctx gocontext.Context) func(next http.Handler) http.Handler {
_, rnd := templates.HTMLRenderer(ctx)
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
ctx := Context{
Resp: NewResponse(resp),
Data: map[string]interface{}{},
Resp: NewResponse(resp),
Data: map[string]interface{}{},
Render: rnd,
}
defer ctx.Close()

Expand Down
37 changes: 37 additions & 0 deletions modules/options/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2022 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 options

import (
"fmt"
"io/fs"
"os"
"path/filepath"

"code.gitea.io/gitea/modules/util"
)

func walkAssetDir(root string, callback func(path, name string, d fs.DirEntry, err error) error) error {
if err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
// name is the path relative to the root
name := path[len(root):]
zeripath marked this conversation as resolved.
Show resolved Hide resolved
if len(name) > 0 && name[0] == '/' {
name = name[1:]
}
if err != nil {
zeripath marked this conversation as resolved.
Show resolved Hide resolved
if os.IsNotExist(err) {
return callback(path, name, d, err)
}
return err
}
if d.IsDir() && util.CommonSkipDir(d.Name()) {
return fs.SkipDir
}
return callback(path, name, d, err)
}); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("unable to get files for assets in %s: %w", root, err)
}
return nil
}
16 changes: 15 additions & 1 deletion modules/options/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ package options

import (
"fmt"
"io/fs"
"os"
"path"
"path/filepath"

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
Expand Down Expand Up @@ -45,7 +47,7 @@ func Dir(name string) ([]string, error) {

isDir, err = util.IsDir(staticDir)
if err != nil {
return []string{}, fmt.Errorf("Unabe to check if static directory %s is a directory. %v", staticDir, err)
return []string{}, fmt.Errorf("unable to check if static directory %s is a directory. %v", staticDir, err)
}
if isDir {
files, err := util.StatDir(staticDir, true)
Expand All @@ -64,6 +66,18 @@ func Locale(name string) ([]byte, error) {
return fileFromDir(path.Join("locale", name))
}

// WalkLocales reads the content of a specific locale from static or custom path.
func WalkLocales(callback func(path, name string, d fs.DirEntry, err error) error) error {
if err := walkAssetDir(filepath.Join(setting.StaticRootPath, "options", "locale"), callback); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("failed to walk locales. Error: %w", err)
}

if err := walkAssetDir(filepath.Join(setting.CustomPath, "options", "locale"), callback); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("failed to walk locales. Error: %w", err)
}
return nil
zeripath marked this conversation as resolved.
Show resolved Hide resolved
}

// Readme reads the content of a specific readme from static or custom path.
func Readme(name string) ([]byte, error) {
return fileFromDir(path.Join("readme", name))
Expand Down
10 changes: 10 additions & 0 deletions modules/options/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ package options
import (
"fmt"
"io"
"io/fs"
"os"
"path"
"path/filepath"

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
Expand Down Expand Up @@ -74,6 +76,14 @@ func Locale(name string) ([]byte, error) {
return fileFromDir(path.Join("locale", name))
}

// WalkLocales reads the content of a specific locale from static or custom path.
func WalkLocales(callback func(path, name string, d fs.DirEntry, err error) error) error {
if err := walkAssetDir(filepath.Join(setting.CustomPath, "options", "locale"), callback); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("failed to walk locales. Error: %w", err)
}
return nil
}

// Readme reads the content of a specific readme from bindata or custom path.
func Readme(name string) ([]byte, error) {
return fileFromDir(path.Join("readme", name))
Expand Down
Loading