Skip to content

Commit aa4f918

Browse files
authored
Clarify the suffices and prefixes of setting.AppSubURL and setting.AppURL (#12999)
Also removes some unnecessary uses of fmt.Sprintf and adds documentation strings Signed-off-by: Andrew Thornton <[email protected]>
1 parent 39aa11f commit aa4f918

File tree

11 files changed

+53
-42
lines changed

11 files changed

+53
-42
lines changed

models/action_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestAction_GetRepoLink(t *testing.T) {
2626
repo := AssertExistsAndLoadBean(t, &Repository{}).(*Repository)
2727
owner := AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User)
2828
action := &Action{RepoID: repo.ID}
29-
setting.AppSubURL = "/suburl/"
29+
setting.AppSubURL = "/suburl"
3030
expected := path.Join(setting.AppSubURL, owner.Name, repo.Name)
3131
assert.Equal(t, expected, action.GetRepoLink())
3232
}

models/notification.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package models
66

77
import (
88
"fmt"
9-
"path"
9+
"strconv"
1010

1111
"code.gitea.io/gitea/modules/log"
1212
"code.gitea.io/gitea/modules/setting"
@@ -413,7 +413,7 @@ func (n *Notification) HTMLURL() string {
413413

414414
// APIURL formats a URL-string to the notification
415415
func (n *Notification) APIURL() string {
416-
return setting.AppURL + path.Join("api/v1/notifications/threads", fmt.Sprintf("%d", n.ID))
416+
return setting.AppURL + "api/v1/notifications/threads/" + strconv.FormatInt(n.ID, 10)
417417
}
418418

419419
// NotificationList contains a list of notifications

models/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ func (repo *Repository) CommitLink(commitID string) (result string) {
318318

319319
// APIURL returns the repository API URL
320320
func (repo *Repository) APIURL() string {
321-
return setting.AppURL + path.Join("api/v1/repos", repo.FullName())
321+
return setting.AppURL + "api/v1/repos/" + repo.FullName()
322322
}
323323

324324
// GetCommitsCountCacheKey returns cache key used for commits count caching.

models/user_avatar.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (u *User) generateRandomAvatar(e Engine) error {
6363
// the local explore page. Function returns immediately.
6464
// When applicable, the link is for an avatar of the indicated size (in pixels).
6565
func (u *User) SizedRelAvatarLink(size int) string {
66-
return strings.TrimSuffix(setting.AppSubURL, "/") + "/user/avatar/" + u.Name + "/" + strconv.Itoa(size)
66+
return setting.AppSubURL + "/user/avatar/" + u.Name + "/" + strconv.Itoa(size)
6767
}
6868

6969
// RealSizedAvatarLink returns a link to the user's avatar. When

modules/markup/html.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,7 @@ func isLinkStr(link string) bool {
8989

9090
func getIssueFullPattern() *regexp.Regexp {
9191
if issueFullPattern == nil {
92-
appURL := setting.AppURL
93-
if len(appURL) > 0 && appURL[len(appURL)-1] != '/' {
94-
appURL += "/"
95-
}
96-
issueFullPattern = regexp.MustCompile(appURL +
92+
issueFullPattern = regexp.MustCompile(regexp.QuoteMeta(setting.AppURL) +
9793
`\w+/\w+/(?:issues|pulls)/((?:\w{1,10}-)?[1-9][0-9]*)([\?|#]\S+.(\S+)?)?\b`)
9894
}
9995
return issueFullPattern
@@ -636,6 +632,9 @@ func mentionProcessor(ctx *postProcessCtx, node *html.Node) {
636632
mention := node.Data[loc.Start:loc.End]
637633
var teams string
638634
teams, ok := ctx.metas["teams"]
635+
// FIXME: util.URLJoin may not be necessary here:
636+
// - setting.AppURL is defined to have a terminal '/' so unless mention[1:]
637+
// is an AppSubURL link we can probably fallback to concatenation.
639638
// team mention should follow @orgName/teamName style
640639
if ok && strings.Contains(mention, "/") {
641640
mentionOrgAndTeam := strings.Split(mention, "/")

modules/setting/setting.go

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,31 @@ const (
6666

6767
// settings
6868
var (
69-
// AppVer settings
70-
AppVer string
71-
AppBuiltWith string
72-
AppStartTime time.Time
73-
AppName string
74-
AppURL string
75-
AppSubURL string
76-
AppSubURLDepth int // Number of slashes
77-
AppPath string
78-
AppDataPath string
79-
AppWorkPath string
69+
// AppVer is the version of the current build of Gitea. It is set in main.go from main.Version.
70+
AppVer string
71+
// AppBuiltWith represents a human readable version go runtime build version and build tags. (See main.go formatBuiltWith().)
72+
AppBuiltWith string
73+
// AppStartTime store time gitea has started
74+
AppStartTime time.Time
75+
// AppName is the Application name, used in the page title.
76+
// It maps to ini:"APP_NAME"
77+
AppName string
78+
// AppURL is the Application ROOT_URL. It always has a '/' suffix
79+
// It maps to ini:"ROOT_URL"
80+
AppURL string
81+
// AppSubURL represents the sub-url mounting point for gitea. It is either "" or starts with '/' and ends without '/', such as '/{subpath}'.
82+
// This value is empty if site does not have sub-url.
83+
AppSubURL string
84+
// AppPath represents the path to the gitea binary
85+
AppPath string
86+
// AppWorkPath is the "working directory" of Gitea. It maps to the environment variable GITEA_WORK_DIR.
87+
// If that is not set it is the default set here by the linker or failing that the directory of AppPath.
88+
//
89+
// AppWorkPath is used as the base path for several other paths.
90+
AppWorkPath string
91+
// AppDataPath is the default path for storing data.
92+
// It maps to ini:"APP_DATA_PATH" and defaults to AppWorkPath + "/data"
93+
AppDataPath string
8094

8195
// Server settings
8296
Protocol Scheme
@@ -594,8 +608,9 @@ func NewContext() {
594608
if (Protocol == HTTP && HTTPPort != "80") || (Protocol == HTTPS && HTTPPort != "443") {
595609
defaultAppURL += ":" + HTTPPort
596610
}
597-
AppURL = sec.Key("ROOT_URL").MustString(defaultAppURL)
598-
AppURL = strings.TrimSuffix(AppURL, "/") + "/"
611+
AppURL = sec.Key("ROOT_URL").MustString(defaultAppURL + "/")
612+
// This should be TrimRight to ensure that there is only a single '/' at the end of AppURL.
613+
AppURL = strings.TrimRight(AppURL, "/") + "/"
599614

600615
// Check if has app suburl.
601616
appURL, err := url.Parse(AppURL)
@@ -606,7 +621,7 @@ func NewContext() {
606621
// This value is empty if site does not have sub-url.
607622
AppSubURL = strings.TrimSuffix(appURL.Path, "/")
608623
StaticURLPrefix = strings.TrimSuffix(sec.Key("STATIC_URL_PREFIX").MustString(AppSubURL), "/")
609-
AppSubURLDepth = strings.Count(AppSubURL, "/")
624+
610625
// Check if Domain differs from AppURL domain than update it to AppURL's domain
611626
urlHostname := appURL.Hostname()
612627
if urlHostname != Domain && net.ParseIP(urlHostname) == nil && urlHostname != "" {

routers/admin/admin.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ func WorkerCancel(ctx *context.Context) {
364364
mq.CancelWorkers(pid)
365365
ctx.Flash.Info(ctx.Tr("admin.monitor.queue.pool.cancelling"))
366366
ctx.JSON(200, map[string]interface{}{
367-
"redirect": setting.AppSubURL + fmt.Sprintf("/admin/monitor/queue/%d", qid),
367+
"redirect": setting.AppSubURL + "/admin/monitor/queue/" + strconv.FormatInt(qid, 10),
368368
})
369369
}
370370

@@ -387,7 +387,7 @@ func Flush(ctx *context.Context) {
387387
log.Error("Flushing failure for %s: Error %v", mq.Name, err)
388388
}
389389
}()
390-
ctx.Redirect(setting.AppSubURL + fmt.Sprintf("/admin/monitor/queue/%d", qid))
390+
ctx.Redirect(setting.AppSubURL + "/admin/monitor/queue/" + strconv.FormatInt(qid, 10))
391391
}
392392

393393
// AddWorkers adds workers to a worker group
@@ -401,23 +401,23 @@ func AddWorkers(ctx *context.Context) {
401401
number := ctx.QueryInt("number")
402402
if number < 1 {
403403
ctx.Flash.Error(ctx.Tr("admin.monitor.queue.pool.addworkers.mustnumbergreaterzero"))
404-
ctx.Redirect(setting.AppSubURL + fmt.Sprintf("/admin/monitor/queue/%d", qid))
404+
ctx.Redirect(setting.AppSubURL + "/admin/monitor/queue/" + strconv.FormatInt(qid, 10))
405405
return
406406
}
407407
timeout, err := time.ParseDuration(ctx.Query("timeout"))
408408
if err != nil {
409409
ctx.Flash.Error(ctx.Tr("admin.monitor.queue.pool.addworkers.musttimeoutduration"))
410-
ctx.Redirect(setting.AppSubURL + fmt.Sprintf("/admin/monitor/queue/%d", qid))
410+
ctx.Redirect(setting.AppSubURL + "/admin/monitor/queue/" + strconv.FormatInt(qid, 10))
411411
return
412412
}
413413
if _, ok := mq.Managed.(queue.ManagedPool); !ok {
414414
ctx.Flash.Error(ctx.Tr("admin.monitor.queue.pool.none"))
415-
ctx.Redirect(setting.AppSubURL + fmt.Sprintf("/admin/monitor/queue/%d", qid))
415+
ctx.Redirect(setting.AppSubURL + "/admin/monitor/queue/" + strconv.FormatInt(qid, 10))
416416
return
417417
}
418418
mq.AddWorkers(number, timeout)
419419
ctx.Flash.Success(ctx.Tr("admin.monitor.queue.pool.added"))
420-
ctx.Redirect(setting.AppSubURL + fmt.Sprintf("/admin/monitor/queue/%d", qid))
420+
ctx.Redirect(setting.AppSubURL + "/admin/monitor/queue/" + strconv.FormatInt(qid, 10))
421421
}
422422

423423
// SetQueueSettings sets the maximum number of workers and other settings for this queue
@@ -430,7 +430,7 @@ func SetQueueSettings(ctx *context.Context) {
430430
}
431431
if _, ok := mq.Managed.(queue.ManagedPool); !ok {
432432
ctx.Flash.Error(ctx.Tr("admin.monitor.queue.pool.none"))
433-
ctx.Redirect(setting.AppSubURL + fmt.Sprintf("/admin/monitor/queue/%d", qid))
433+
ctx.Redirect(setting.AppSubURL + "/admin/monitor/queue/" + strconv.FormatInt(qid, 10))
434434
return
435435
}
436436

@@ -445,7 +445,7 @@ func SetQueueSettings(ctx *context.Context) {
445445
maxNumber, err = strconv.Atoi(maxNumberStr)
446446
if err != nil {
447447
ctx.Flash.Error(ctx.Tr("admin.monitor.queue.settings.maxnumberworkers.error"))
448-
ctx.Redirect(setting.AppSubURL + fmt.Sprintf("/admin/monitor/queue/%d", qid))
448+
ctx.Redirect(setting.AppSubURL + "/admin/monitor/queue/" + strconv.FormatInt(qid, 10))
449449
return
450450
}
451451
if maxNumber < -1 {
@@ -459,7 +459,7 @@ func SetQueueSettings(ctx *context.Context) {
459459
number, err = strconv.Atoi(numberStr)
460460
if err != nil || number < 0 {
461461
ctx.Flash.Error(ctx.Tr("admin.monitor.queue.settings.numberworkers.error"))
462-
ctx.Redirect(setting.AppSubURL + fmt.Sprintf("/admin/monitor/queue/%d", qid))
462+
ctx.Redirect(setting.AppSubURL + "/admin/monitor/queue/" + strconv.FormatInt(qid, 10))
463463
return
464464
}
465465
} else {
@@ -470,7 +470,7 @@ func SetQueueSettings(ctx *context.Context) {
470470
timeout, err = time.ParseDuration(timeoutStr)
471471
if err != nil {
472472
ctx.Flash.Error(ctx.Tr("admin.monitor.queue.settings.timeout.error"))
473-
ctx.Redirect(setting.AppSubURL + fmt.Sprintf("/admin/monitor/queue/%d", qid))
473+
ctx.Redirect(setting.AppSubURL + "/admin/monitor/queue/" + strconv.FormatInt(qid, 10))
474474
return
475475
}
476476
} else {
@@ -479,5 +479,5 @@ func SetQueueSettings(ctx *context.Context) {
479479

480480
mq.SetPoolSettings(maxNumber, number, timeout)
481481
ctx.Flash.Success(ctx.Tr("admin.monitor.queue.settings.changed"))
482-
ctx.Redirect(setting.AppSubURL + fmt.Sprintf("/admin/monitor/queue/%d", qid))
482+
ctx.Redirect(setting.AppSubURL + "/admin/monitor/queue/" + strconv.FormatInt(qid, 10))
483483
}

routers/api/v1/org/member.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package org
66

77
import (
8-
"fmt"
98
"net/http"
109

1110
"code.gitea.io/gitea/models"
@@ -153,8 +152,7 @@ func IsMember(ctx *context.APIContext) {
153152
}
154153
}
155154

156-
redirectURL := fmt.Sprintf("%sapi/v1/orgs/%s/public_members/%s",
157-
setting.AppURL, ctx.Org.Organization.Name, userToCheck.Name)
155+
redirectURL := setting.AppURL + "api/v1/orgs/" + ctx.Org.Organization.Name + "/public_members/" + userToCheck.Name
158156
ctx.Redirect(redirectURL, 302)
159157
}
160158

routers/repo/search.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ func Search(ctx *context.Context) {
4040
ctx.Data["Keyword"] = keyword
4141
ctx.Data["Language"] = language
4242
ctx.Data["queryType"] = queryType
43-
ctx.Data["SourcePath"] = setting.AppSubURL + "/" +
44-
path.Join(ctx.Repo.Repository.Owner.Name, ctx.Repo.Repository.Name)
43+
ctx.Data["SourcePath"] = path.Join(setting.AppSubURL, ctx.Repo.Repository.Owner.Name, ctx.Repo.Repository.Name)
4544
ctx.Data["SearchResults"] = searchResults
4645
ctx.Data["SearchResultLanguages"] = searchResultLanguages
4746
ctx.Data["RequireHighlightJS"] = true

routers/user/notification.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func NotificationStatusPost(c *context.Context) {
174174
if c.Written() {
175175
return
176176
}
177-
c.Data["Link"] = fmt.Sprintf("%snotifications", setting.AppURL)
177+
c.Data["Link"] = setting.AppURL + "notifications"
178178

179179
c.HTML(http.StatusOK, tplNotificationDiv)
180180
}

routers/utils/utils_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func TestIsValidSlackChannel(t *testing.T) {
3535
}
3636

3737
func TestIsExternalURL(t *testing.T) {
38-
setting.AppURL = "https://try.gitea.io"
38+
setting.AppURL = "https://try.gitea.io/"
3939
type test struct {
4040
Expected bool
4141
RawURL string

0 commit comments

Comments
 (0)