Compare commits
9 Commits
v1.16.7
...
templates-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
097c067fcb | ||
|
|
34e64ffec2 | ||
|
|
ce295d724f | ||
|
|
569c9af0cc | ||
|
|
80cf010cc1 | ||
|
|
d660fb61cc | ||
|
|
206c8c45f2 | ||
|
|
fdca271e20 | ||
|
|
f952674fbe |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -29,6 +29,7 @@ _testmain.go
|
||||
coverage.out
|
||||
|
||||
/modules/public/bindata.go
|
||||
/modules/templates/bindata.go
|
||||
|
||||
*.db
|
||||
*.log
|
||||
|
||||
64
cmd/web.go
64
cmd/web.go
@@ -7,7 +7,6 @@ package cmd
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/fcgi"
|
||||
@@ -15,7 +14,6 @@ import (
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/git"
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/auth"
|
||||
"code.gitea.io/gitea/modules/bindata"
|
||||
@@ -23,7 +21,7 @@ import (
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/public"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/template"
|
||||
"code.gitea.io/gitea/modules/templates"
|
||||
"code.gitea.io/gitea/routers"
|
||||
"code.gitea.io/gitea/routers/admin"
|
||||
apiv1 "code.gitea.io/gitea/routers/api/v1"
|
||||
@@ -39,10 +37,7 @@ import (
|
||||
"github.com/go-macaron/i18n"
|
||||
"github.com/go-macaron/session"
|
||||
"github.com/go-macaron/toolbox"
|
||||
"github.com/go-xorm/xorm"
|
||||
version "github.com/mcuadros/go-version"
|
||||
"github.com/urfave/cli"
|
||||
ini "gopkg.in/ini.v1"
|
||||
macaron "gopkg.in/macaron.v1"
|
||||
)
|
||||
|
||||
@@ -74,45 +69,6 @@ type VerChecker struct {
|
||||
Expected string
|
||||
}
|
||||
|
||||
// checkVersion checks if binary matches the version of templates files.
|
||||
func checkVersion() {
|
||||
// Templates.
|
||||
data, err := ioutil.ReadFile(setting.StaticRootPath + "/templates/.VERSION")
|
||||
if err != nil {
|
||||
log.Fatal(4, "Fail to read 'templates/.VERSION': %v", err)
|
||||
}
|
||||
tplVer := string(data)
|
||||
if tplVer != setting.AppVer {
|
||||
if version.Compare(tplVer, setting.AppVer, ">") {
|
||||
log.Fatal(4, "Binary version is lower than template file version, did you forget to recompile Gogs?")
|
||||
} else {
|
||||
log.Fatal(4, "Binary version is higher than template file version, did you forget to update template files?")
|
||||
}
|
||||
}
|
||||
|
||||
// Check dependency version.
|
||||
checkers := []VerChecker{
|
||||
{"github.com/go-xorm/xorm", func() string { return xorm.Version }, "0.5.5"},
|
||||
{"github.com/go-macaron/binding", binding.Version, "0.3.2"},
|
||||
{"github.com/go-macaron/cache", cache.Version, "0.1.2"},
|
||||
{"github.com/go-macaron/csrf", csrf.Version, "0.1.0"},
|
||||
{"github.com/go-macaron/i18n", i18n.Version, "0.3.0"},
|
||||
{"github.com/go-macaron/session", session.Version, "0.1.6"},
|
||||
{"github.com/go-macaron/toolbox", toolbox.Version, "0.1.0"},
|
||||
{"gopkg.in/ini.v1", ini.Version, "1.8.4"},
|
||||
{"gopkg.in/macaron.v1", macaron.Version, "1.1.7"},
|
||||
{"code.gitea.io/git", git.Version, "0.4.1"},
|
||||
}
|
||||
for _, c := range checkers {
|
||||
if !version.Compare(c.Version(), c.Expected, ">=") {
|
||||
log.Fatal(4, `Dependency outdated!
|
||||
Package '%s' current version (%s) is below requirement (%s),
|
||||
please use following command to update this package and recompile Gogs:
|
||||
go get -u %[1]s`, c.ImportPath, c.Version(), c.Expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// newMacaron initializes Macaron instance.
|
||||
func newMacaron() *macaron.Macaron {
|
||||
m := macaron.New()
|
||||
@@ -139,16 +95,17 @@ func newMacaron() *macaron.Macaron {
|
||||
SkipLogging: setting.DisableRouterLog,
|
||||
},
|
||||
))
|
||||
m.Use(templates.Renderer(
|
||||
&templates.Options{
|
||||
Directory: path.Join(setting.StaticRootPath, "templates"),
|
||||
Custom: []string{path.Join(setting.CustomPath, "templates")},
|
||||
},
|
||||
))
|
||||
|
||||
funcMap := template.NewFuncMap()
|
||||
m.Use(macaron.Renderer(macaron.RenderOptions{
|
||||
Directory: path.Join(setting.StaticRootPath, "templates"),
|
||||
AppendDirectories: []string{path.Join(setting.CustomPath, "templates")},
|
||||
Funcs: funcMap,
|
||||
IndentJSON: macaron.Env != macaron.PROD,
|
||||
models.InitMailRender(templates.Mailer(&templates.Options{
|
||||
Directory: path.Join(setting.StaticRootPath, "templates", "mail"),
|
||||
Custom: []string{path.Join(setting.CustomPath, "templates", "mail")},
|
||||
}))
|
||||
models.InitMailRender(path.Join(setting.StaticRootPath, "templates/mail"),
|
||||
path.Join(setting.CustomPath, "templates/mail"), funcMap)
|
||||
|
||||
localeNames, err := bindata.AssetDir("conf/locale")
|
||||
if err != nil {
|
||||
@@ -200,7 +157,6 @@ func runWeb(ctx *cli.Context) error {
|
||||
setting.CustomConf = ctx.String("config")
|
||||
}
|
||||
routers.GlobalInit()
|
||||
checkVersion()
|
||||
|
||||
m := newMacaron()
|
||||
|
||||
|
||||
@@ -5,18 +5,18 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"path"
|
||||
|
||||
"gopkg.in/gomail.v2"
|
||||
"gopkg.in/macaron.v1"
|
||||
|
||||
"code.gitea.io/gitea/modules/base"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/mailer"
|
||||
"code.gitea.io/gitea/modules/markdown"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"gopkg.in/gomail.v2"
|
||||
"gopkg.in/macaron.v1"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -31,27 +31,11 @@ const (
|
||||
mailNotifyCollaborator base.TplName = "notify/collaborator"
|
||||
)
|
||||
|
||||
type mailRenderInterface interface {
|
||||
HTMLString(string, interface{}, ...macaron.HTMLOptions) (string, error)
|
||||
}
|
||||
|
||||
var mailRender mailRenderInterface
|
||||
var templates *template.Template
|
||||
|
||||
// InitMailRender initializes the macaron mail renderer
|
||||
func InitMailRender(dir, appendDir string, funcMap []template.FuncMap) {
|
||||
opt := &macaron.RenderOptions{
|
||||
Directory: dir,
|
||||
AppendDirectories: []string{appendDir},
|
||||
Funcs: funcMap,
|
||||
Extensions: []string{".tmpl", ".html"},
|
||||
}
|
||||
ts := macaron.NewTemplateSet()
|
||||
ts.Set(macaron.DEFAULT_TPL_SET_NAME, opt)
|
||||
|
||||
mailRender = &macaron.TplRender{
|
||||
TemplateSet: ts,
|
||||
Opt: opt,
|
||||
}
|
||||
func InitMailRender(tmpls *template.Template) {
|
||||
templates = tmpls
|
||||
}
|
||||
|
||||
// SendTestMail sends a test mail
|
||||
@@ -67,13 +51,15 @@ func SendUserMail(c *macaron.Context, u *User, tpl base.TplName, code, subject,
|
||||
"ResetPwdCodeLives": setting.Service.ResetPwdCodeLives / 60,
|
||||
"Code": code,
|
||||
}
|
||||
body, err := mailRender.HTMLString(string(tpl), data)
|
||||
if err != nil {
|
||||
log.Error(3, "HTMLString: %v", err)
|
||||
|
||||
var content bytes.Buffer
|
||||
|
||||
if err := templates.ExecuteTemplate(&content, string(tpl), data); err != nil {
|
||||
log.Error(3, "Template: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
msg := mailer.NewMessage([]string{u.Email}, subject, body)
|
||||
msg := mailer.NewMessage([]string{u.Email}, subject, content.String())
|
||||
msg.Info = fmt.Sprintf("UID: %d, %s", u.ID, info)
|
||||
|
||||
mailer.SendAsync(msg)
|
||||
@@ -97,13 +83,15 @@ func SendActivateEmailMail(c *macaron.Context, u *User, email *EmailAddress) {
|
||||
"Code": u.GenerateEmailActivateCode(email.Email),
|
||||
"Email": email.Email,
|
||||
}
|
||||
body, err := mailRender.HTMLString(string(mailAuthActivateEmail), data)
|
||||
if err != nil {
|
||||
log.Error(3, "HTMLString: %v", err)
|
||||
|
||||
var content bytes.Buffer
|
||||
|
||||
if err := templates.ExecuteTemplate(&content, string(mailAuthActivateEmail), data); err != nil {
|
||||
log.Error(3, "Template: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
msg := mailer.NewMessage([]string{email.Email}, c.Tr("mail.activate_email"), body)
|
||||
msg := mailer.NewMessage([]string{email.Email}, c.Tr("mail.activate_email"), content.String())
|
||||
msg.Info = fmt.Sprintf("UID: %d, activate email", u.ID)
|
||||
|
||||
mailer.SendAsync(msg)
|
||||
@@ -114,13 +102,15 @@ func SendRegisterNotifyMail(c *macaron.Context, u *User) {
|
||||
data := map[string]interface{}{
|
||||
"Username": u.DisplayName(),
|
||||
}
|
||||
body, err := mailRender.HTMLString(string(mailAuthRegisterNotify), data)
|
||||
if err != nil {
|
||||
log.Error(3, "HTMLString: %v", err)
|
||||
|
||||
var content bytes.Buffer
|
||||
|
||||
if err := templates.ExecuteTemplate(&content, string(mailAuthRegisterNotify), data); err != nil {
|
||||
log.Error(3, "Template: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
msg := mailer.NewMessage([]string{u.Email}, c.Tr("mail.register_notify"), body)
|
||||
msg := mailer.NewMessage([]string{u.Email}, c.Tr("mail.register_notify"), content.String())
|
||||
msg.Info = fmt.Sprintf("UID: %d, registration notify", u.ID)
|
||||
|
||||
mailer.SendAsync(msg)
|
||||
@@ -136,13 +126,15 @@ func SendCollaboratorMail(u, doer *User, repo *Repository) {
|
||||
"RepoName": repoName,
|
||||
"Link": repo.HTMLURL(),
|
||||
}
|
||||
body, err := mailRender.HTMLString(string(mailNotifyCollaborator), data)
|
||||
if err != nil {
|
||||
log.Error(3, "HTMLString: %v", err)
|
||||
|
||||
var content bytes.Buffer
|
||||
|
||||
if err := templates.ExecuteTemplate(&content, string(mailNotifyCollaborator), data); err != nil {
|
||||
log.Error(3, "Template: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
msg := mailer.NewMessage([]string{u.Email}, subject, body)
|
||||
msg := mailer.NewMessage([]string{u.Email}, subject, content.String())
|
||||
msg.Info = fmt.Sprintf("UID: %d, add collaborator", u.ID)
|
||||
|
||||
mailer.SendAsync(msg)
|
||||
@@ -161,11 +153,14 @@ func composeIssueMessage(issue *Issue, doer *User, tplName base.TplName, tos []s
|
||||
body := string(markdown.RenderSpecialLink([]byte(issue.Content), issue.Repo.HTMLURL(), issue.Repo.ComposeMetas()))
|
||||
data := composeTplData(subject, body, issue.HTMLURL())
|
||||
data["Doer"] = doer
|
||||
content, err := mailRender.HTMLString(string(tplName), data)
|
||||
if err != nil {
|
||||
log.Error(3, "HTMLString (%s): %v", tplName, err)
|
||||
|
||||
var content bytes.Buffer
|
||||
|
||||
if err := templates.ExecuteTemplate(&content, string(tplName), data); err != nil {
|
||||
log.Error(3, "Template: %v", err)
|
||||
}
|
||||
msg := mailer.NewMessageFrom(tos, fmt.Sprintf(`"%s" <%s>`, doer.DisplayName(), setting.MailService.FromEmail), subject, content)
|
||||
|
||||
msg := mailer.NewMessageFrom(tos, fmt.Sprintf(`"%s" <%s>`, doer.DisplayName(), setting.MailService.FromEmail), subject, content.String())
|
||||
msg.Info = fmt.Sprintf("Subject: %s, %s", subject, info)
|
||||
return msg
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ package public
|
||||
|
||||
//go:generate go-bindata -tags "bindata" -ignore "\\.go|\\.less" -pkg "public" -o "bindata.go" ../../public/...
|
||||
//go:generate go fmt bindata.go
|
||||
//go:generate sed -i.bak s/..\/..\/public\/// bindata.go
|
||||
//go:generate rm -f bindata.go.bak
|
||||
|
||||
// Options represents the available options to configure the macaron handler.
|
||||
type Options struct {
|
||||
|
||||
@@ -22,7 +22,7 @@ func Static(opts *Options) macaron.Handler {
|
||||
AssetDir: AssetDir,
|
||||
AssetInfo: AssetInfo,
|
||||
AssetNames: AssetNames,
|
||||
Prefix: "../../public",
|
||||
Prefix: "",
|
||||
}),
|
||||
},
|
||||
)
|
||||
|
||||
88
modules/templates/dynamic.go
Normal file
88
modules/templates/dynamic.go
Normal file
@@ -0,0 +1,88 @@
|
||||
// +build !bindata
|
||||
|
||||
// Copyright 2016 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 templates
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
template_func "code.gitea.io/gitea/modules/template"
|
||||
"gopkg.in/macaron.v1"
|
||||
)
|
||||
|
||||
// Renderer implements the macaron handler for serving the templates.
|
||||
func Renderer(opts *Options) macaron.Handler {
|
||||
return macaron.Renderer(macaron.RenderOptions{
|
||||
Directory: opts.Directory,
|
||||
AppendDirectories: opts.Custom,
|
||||
Funcs: template_func.NewFuncMap(),
|
||||
})
|
||||
}
|
||||
|
||||
// Mailer provides the templates required for sending notification mails.
|
||||
func Mailer(opts *Options) *template.Template {
|
||||
templates := template.New("")
|
||||
|
||||
for _, funcs := range template_func.NewFuncMap() {
|
||||
templates.Funcs(funcs)
|
||||
}
|
||||
|
||||
if _, err := os.Stat(opts.Directory); err == nil {
|
||||
if err := filepath.Walk(opts.Directory, func(path string, info os.FileInfo, _ error) error {
|
||||
if info.IsDir() || !strings.HasSuffix(path, ".tmpl") {
|
||||
return nil
|
||||
}
|
||||
name := strings.TrimSuffix(strings.TrimPrefix(path, opts.Directory+"/"), ".tmpl")
|
||||
log.Info("Found new template: %s", name)
|
||||
ts, err := loadTemplate(name, path)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
_, err = templates.Parse(ts)
|
||||
return err
|
||||
}); err != nil {
|
||||
log.Error(3, "Unable to parse template directory %s. %v", opts.Directory, err)
|
||||
}
|
||||
}
|
||||
|
||||
for _, asset := range opts.Custom {
|
||||
if _, err := os.Stat(asset); err == nil {
|
||||
if err := filepath.Walk(asset, func(path string, info os.FileInfo, _ error) error {
|
||||
if info.IsDir() || !strings.HasSuffix(path, ".tmpl") {
|
||||
return nil
|
||||
}
|
||||
name := strings.TrimSuffix(strings.TrimPrefix(path, asset+"/"), ".tmpl")
|
||||
log.Info("Found new template: %s", name)
|
||||
ts, err := loadTemplate(name, path)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
_, err = templates.Parse(ts)
|
||||
return err
|
||||
}); err != nil {
|
||||
log.Error(3, "Unable to parse template directory %s. %v", asset, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.Error(3, templates.DefinedTemplates())
|
||||
|
||||
return templates
|
||||
}
|
||||
|
||||
func loadTemplate(name, path string) (string, error) {
|
||||
t, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return fmt.Sprintf(`{{define "%s"}}%s{{end}}`, name, t), nil
|
||||
}
|
||||
70
modules/templates/static.go
Normal file
70
modules/templates/static.go
Normal file
@@ -0,0 +1,70 @@
|
||||
// +build bindata
|
||||
|
||||
// Copyright 2016 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 templates
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
template_func "code.gitea.io/gitea/modules/template"
|
||||
"github.com/go-macaron/bindata"
|
||||
"gopkg.in/macaron.v1"
|
||||
)
|
||||
|
||||
// Renderer implements the macaron handler for serving the templates.
|
||||
func Renderer(opts *Options) macaron.Handler {
|
||||
return macaron.Renderer(macaron.RenderOptions{
|
||||
AppendDirectories: opts.Custom,
|
||||
Funcs: template_func.NewFuncMap(),
|
||||
TemplateFileSystem: bindata.Templates(
|
||||
bindata.Options{
|
||||
Asset: Asset,
|
||||
AssetDir: AssetDir,
|
||||
AssetInfo: AssetInfo,
|
||||
AssetNames: AssetNames,
|
||||
Prefix: "",
|
||||
},
|
||||
),
|
||||
})
|
||||
}
|
||||
|
||||
// Mailer provides the templates required for sending notification mails.
|
||||
func Mailer(opts *Options) *template.Template {
|
||||
templates := template.New("")
|
||||
|
||||
for _, funcs := range template_func.NewFuncMap() {
|
||||
templates.Funcs(funcs)
|
||||
}
|
||||
|
||||
assets, err := AssetDir("mail")
|
||||
|
||||
if err != nil {
|
||||
log.Error(3, "Unable to read mail asset dir. %s", err)
|
||||
}
|
||||
|
||||
for _, asset := range assets {
|
||||
bytes, err := Asset(asset)
|
||||
|
||||
if err != nil {
|
||||
log.Error(3, "Unable to parse template %s. %s", asset, err)
|
||||
}
|
||||
|
||||
templates.New(asset).Parse(string(bytes))
|
||||
}
|
||||
|
||||
for _, asset := range opts.Custom {
|
||||
if _, err := os.Stat(opts.Directory); err == nil {
|
||||
if _, err := templates.ParseGlob(path.Join(asset, "*", "*.tmpl")); err != nil {
|
||||
log.Error(3, "Unable to parse template directory %s. %s", opts.Directory, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return templates
|
||||
}
|
||||
16
modules/templates/templates.go
Normal file
16
modules/templates/templates.go
Normal file
@@ -0,0 +1,16 @@
|
||||
// Copyright 2016 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 templates
|
||||
|
||||
//go:generate go-bindata -tags "bindata" -ignore "\\.go" -pkg "templates" -o "bindata.go" ../../templates/...
|
||||
//go:generate go fmt bindata.go
|
||||
//go:generate sed -i.bak s/..\/..\/templates\/// bindata.go
|
||||
//go:generate rm -f bindata.go.bak
|
||||
|
||||
// Options represents the available options to configure the macaron handler.
|
||||
type Options struct {
|
||||
Directory string
|
||||
Custom []string
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
0.9.99.0915
|
||||
Reference in New Issue
Block a user