Skip to content

Commit 83ed234

Browse files
authored
Integrate templates into bindata optionally (go-gitea#314)
Integrated optional bindata for the templates
1 parent 1b5b297 commit 83ed234

File tree

15 files changed

+274
-107
lines changed

15 files changed

+274
-107
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ _testmain.go
2929
coverage.out
3030

3131
/modules/public/bindata.go
32+
/modules/templates/bindata.go
3233

3334
*.db
3435
*.log

cmd/web.go

+3-55
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,21 @@ package cmd
77
import (
88
"crypto/tls"
99
"fmt"
10-
"io/ioutil"
1110
"net"
1211
"net/http"
1312
"net/http/fcgi"
1413
"os"
1514
"path"
1615
"strings"
1716

18-
"code.gitea.io/git"
1917
"code.gitea.io/gitea/models"
2018
"code.gitea.io/gitea/modules/auth"
2119
"code.gitea.io/gitea/modules/bindata"
2220
"code.gitea.io/gitea/modules/context"
2321
"code.gitea.io/gitea/modules/log"
2422
"code.gitea.io/gitea/modules/public"
2523
"code.gitea.io/gitea/modules/setting"
26-
"code.gitea.io/gitea/modules/template"
24+
"code.gitea.io/gitea/modules/templates"
2725
"code.gitea.io/gitea/routers"
2826
"code.gitea.io/gitea/routers/admin"
2927
apiv1 "code.gitea.io/gitea/routers/api/v1"
@@ -39,10 +37,7 @@ import (
3937
"github.com/go-macaron/i18n"
4038
"github.com/go-macaron/session"
4139
"github.com/go-macaron/toolbox"
42-
"github.com/go-xorm/xorm"
43-
version "github.com/mcuadros/go-version"
4440
"github.com/urfave/cli"
45-
ini "gopkg.in/ini.v1"
4641
macaron "gopkg.in/macaron.v1"
4742
)
4843

@@ -74,45 +69,6 @@ type VerChecker struct {
7469
Expected string
7570
}
7671

77-
// checkVersion checks if binary matches the version of templates files.
78-
func checkVersion() {
79-
// Templates.
80-
data, err := ioutil.ReadFile(setting.StaticRootPath + "/templates/.VERSION")
81-
if err != nil {
82-
log.Fatal(4, "Fail to read 'templates/.VERSION': %v", err)
83-
}
84-
tplVer := string(data)
85-
if tplVer != setting.AppVer {
86-
if version.Compare(tplVer, setting.AppVer, ">") {
87-
log.Fatal(4, "Binary version is lower than template file version, did you forget to recompile Gogs?")
88-
} else {
89-
log.Fatal(4, "Binary version is higher than template file version, did you forget to update template files?")
90-
}
91-
}
92-
93-
// Check dependency version.
94-
checkers := []VerChecker{
95-
{"github.com/go-xorm/xorm", func() string { return xorm.Version }, "0.5.5"},
96-
{"github.com/go-macaron/binding", binding.Version, "0.3.2"},
97-
{"github.com/go-macaron/cache", cache.Version, "0.1.2"},
98-
{"github.com/go-macaron/csrf", csrf.Version, "0.1.0"},
99-
{"github.com/go-macaron/i18n", i18n.Version, "0.3.0"},
100-
{"github.com/go-macaron/session", session.Version, "0.1.6"},
101-
{"github.com/go-macaron/toolbox", toolbox.Version, "0.1.0"},
102-
{"gopkg.in/ini.v1", ini.Version, "1.8.4"},
103-
{"gopkg.in/macaron.v1", macaron.Version, "1.1.7"},
104-
{"code.gitea.io/git", git.Version, "0.4.1"},
105-
}
106-
for _, c := range checkers {
107-
if !version.Compare(c.Version(), c.Expected, ">=") {
108-
log.Fatal(4, `Dependency outdated!
109-
Package '%s' current version (%s) is below requirement (%s),
110-
please use following command to update this package and recompile Gogs:
111-
go get -u %[1]s`, c.ImportPath, c.Version(), c.Expected)
112-
}
113-
}
114-
}
115-
11672
// newMacaron initializes Macaron instance.
11773
func newMacaron() *macaron.Macaron {
11874
m := macaron.New()
@@ -140,15 +96,8 @@ func newMacaron() *macaron.Macaron {
14096
},
14197
))
14298

143-
funcMap := template.NewFuncMap()
144-
m.Use(macaron.Renderer(macaron.RenderOptions{
145-
Directory: path.Join(setting.StaticRootPath, "templates"),
146-
AppendDirectories: []string{path.Join(setting.CustomPath, "templates")},
147-
Funcs: funcMap,
148-
IndentJSON: macaron.Env != macaron.PROD,
149-
}))
150-
models.InitMailRender(path.Join(setting.StaticRootPath, "templates/mail"),
151-
path.Join(setting.CustomPath, "templates/mail"), funcMap)
99+
m.Use(templates.Renderer())
100+
models.InitMailRender(templates.Mailer())
152101

153102
localeNames, err := bindata.AssetDir("conf/locale")
154103
if err != nil {
@@ -200,7 +149,6 @@ func runWeb(ctx *cli.Context) error {
200149
setting.CustomConf = ctx.String("config")
201150
}
202151
routers.GlobalInit()
203-
checkVersion()
204152

205153
m := newMacaron()
206154

models/git_diff.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ import (
1818

1919
"code.gitea.io/git"
2020
"code.gitea.io/gitea/modules/base"
21+
"code.gitea.io/gitea/modules/highlight"
2122
"code.gitea.io/gitea/modules/log"
2223
"code.gitea.io/gitea/modules/process"
2324
"code.gitea.io/gitea/modules/setting"
24-
"code.gitea.io/gitea/modules/template/highlight"
2525
"github.com/Unknwon/com"
2626
"github.com/sergi/go-diff/diffmatchpatch"
2727
"golang.org/x/net/html/charset"

models/mail.go

+37-42
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
package models
66

77
import (
8+
"bytes"
89
"fmt"
910
"html/template"
1011
"path"
1112

12-
"gopkg.in/gomail.v2"
13-
"gopkg.in/macaron.v1"
14-
1513
"code.gitea.io/gitea/modules/base"
1614
"code.gitea.io/gitea/modules/log"
1715
"code.gitea.io/gitea/modules/mailer"
1816
"code.gitea.io/gitea/modules/markdown"
1917
"code.gitea.io/gitea/modules/setting"
18+
"gopkg.in/gomail.v2"
19+
"gopkg.in/macaron.v1"
2020
)
2121

2222
const (
@@ -31,27 +31,11 @@ const (
3131
mailNotifyCollaborator base.TplName = "notify/collaborator"
3232
)
3333

34-
type mailRenderInterface interface {
35-
HTMLString(string, interface{}, ...macaron.HTMLOptions) (string, error)
36-
}
37-
38-
var mailRender mailRenderInterface
34+
var templates *template.Template
3935

4036
// InitMailRender initializes the macaron mail renderer
41-
func InitMailRender(dir, appendDir string, funcMap []template.FuncMap) {
42-
opt := &macaron.RenderOptions{
43-
Directory: dir,
44-
AppendDirectories: []string{appendDir},
45-
Funcs: funcMap,
46-
Extensions: []string{".tmpl", ".html"},
47-
}
48-
ts := macaron.NewTemplateSet()
49-
ts.Set(macaron.DEFAULT_TPL_SET_NAME, opt)
50-
51-
mailRender = &macaron.TplRender{
52-
TemplateSet: ts,
53-
Opt: opt,
54-
}
37+
func InitMailRender(tmpls *template.Template) {
38+
templates = tmpls
5539
}
5640

5741
// SendTestMail sends a test mail
@@ -67,13 +51,15 @@ func SendUserMail(c *macaron.Context, u *User, tpl base.TplName, code, subject,
6751
"ResetPwdCodeLives": setting.Service.ResetPwdCodeLives / 60,
6852
"Code": code,
6953
}
70-
body, err := mailRender.HTMLString(string(tpl), data)
71-
if err != nil {
72-
log.Error(3, "HTMLString: %v", err)
54+
55+
var content bytes.Buffer
56+
57+
if err := templates.ExecuteTemplate(&content, string(tpl), data); err != nil {
58+
log.Error(3, "Template: %v", err)
7359
return
7460
}
7561

76-
msg := mailer.NewMessage([]string{u.Email}, subject, body)
62+
msg := mailer.NewMessage([]string{u.Email}, subject, content.String())
7763
msg.Info = fmt.Sprintf("UID: %d, %s", u.ID, info)
7864

7965
mailer.SendAsync(msg)
@@ -97,13 +83,15 @@ func SendActivateEmailMail(c *macaron.Context, u *User, email *EmailAddress) {
9783
"Code": u.GenerateEmailActivateCode(email.Email),
9884
"Email": email.Email,
9985
}
100-
body, err := mailRender.HTMLString(string(mailAuthActivateEmail), data)
101-
if err != nil {
102-
log.Error(3, "HTMLString: %v", err)
86+
87+
var content bytes.Buffer
88+
89+
if err := templates.ExecuteTemplate(&content, string(mailAuthActivateEmail), data); err != nil {
90+
log.Error(3, "Template: %v", err)
10391
return
10492
}
10593

106-
msg := mailer.NewMessage([]string{email.Email}, c.Tr("mail.activate_email"), body)
94+
msg := mailer.NewMessage([]string{email.Email}, c.Tr("mail.activate_email"), content.String())
10795
msg.Info = fmt.Sprintf("UID: %d, activate email", u.ID)
10896

10997
mailer.SendAsync(msg)
@@ -114,13 +102,15 @@ func SendRegisterNotifyMail(c *macaron.Context, u *User) {
114102
data := map[string]interface{}{
115103
"Username": u.DisplayName(),
116104
}
117-
body, err := mailRender.HTMLString(string(mailAuthRegisterNotify), data)
118-
if err != nil {
119-
log.Error(3, "HTMLString: %v", err)
105+
106+
var content bytes.Buffer
107+
108+
if err := templates.ExecuteTemplate(&content, string(mailAuthRegisterNotify), data); err != nil {
109+
log.Error(3, "Template: %v", err)
120110
return
121111
}
122112

123-
msg := mailer.NewMessage([]string{u.Email}, c.Tr("mail.register_notify"), body)
113+
msg := mailer.NewMessage([]string{u.Email}, c.Tr("mail.register_notify"), content.String())
124114
msg.Info = fmt.Sprintf("UID: %d, registration notify", u.ID)
125115

126116
mailer.SendAsync(msg)
@@ -136,13 +126,15 @@ func SendCollaboratorMail(u, doer *User, repo *Repository) {
136126
"RepoName": repoName,
137127
"Link": repo.HTMLURL(),
138128
}
139-
body, err := mailRender.HTMLString(string(mailNotifyCollaborator), data)
140-
if err != nil {
141-
log.Error(3, "HTMLString: %v", err)
129+
130+
var content bytes.Buffer
131+
132+
if err := templates.ExecuteTemplate(&content, string(mailNotifyCollaborator), data); err != nil {
133+
log.Error(3, "Template: %v", err)
142134
return
143135
}
144136

145-
msg := mailer.NewMessage([]string{u.Email}, subject, body)
137+
msg := mailer.NewMessage([]string{u.Email}, subject, content.String())
146138
msg.Info = fmt.Sprintf("UID: %d, add collaborator", u.ID)
147139

148140
mailer.SendAsync(msg)
@@ -161,11 +153,14 @@ func composeIssueMessage(issue *Issue, doer *User, tplName base.TplName, tos []s
161153
body := string(markdown.RenderSpecialLink([]byte(issue.Content), issue.Repo.HTMLURL(), issue.Repo.ComposeMetas()))
162154
data := composeTplData(subject, body, issue.HTMLURL())
163155
data["Doer"] = doer
164-
content, err := mailRender.HTMLString(string(tplName), data)
165-
if err != nil {
166-
log.Error(3, "HTMLString (%s): %v", tplName, err)
156+
157+
var content bytes.Buffer
158+
159+
if err := templates.ExecuteTemplate(&content, string(tplName), data); err != nil {
160+
log.Error(3, "Template: %v", err)
167161
}
168-
msg := mailer.NewMessageFrom(tos, fmt.Sprintf(`"%s" <%s>`, doer.DisplayName(), setting.MailService.FromEmail), subject, content)
162+
163+
msg := mailer.NewMessageFrom(tos, fmt.Sprintf(`"%s" <%s>`, doer.DisplayName(), setting.MailService.FromEmail), subject, content.String())
169164
msg.Info = fmt.Sprintf("Subject: %s, %s", subject, info)
170165
return msg
171166
}

modules/public/public.go

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package public
66

77
//go:generate go-bindata -tags "bindata" -ignore "\\.go|\\.less" -pkg "public" -o "bindata.go" ../../public/...
88
//go:generate go fmt bindata.go
9+
//go:generate sed -i.bak s/..\/..\/public\/// bindata.go
10+
//go:generate rm -f bindata.go.bak
911

1012
// Options represents the available options to configure the macaron handler.
1113
type Options struct {

modules/public/static.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func Static(opts *Options) macaron.Handler {
2222
AssetDir: AssetDir,
2323
AssetInfo: AssetInfo,
2424
AssetNames: AssetNames,
25-
Prefix: "../../public",
25+
Prefix: "",
2626
}),
2727
},
2828
)

0 commit comments

Comments
 (0)