Skip to content

Commit e1927c4

Browse files
author
Holger Huo
committed
fix: host-based mapping with local backend
1 parent 596a373 commit e1927c4

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

handler/router.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,20 @@ func Convert(c *fiber.Ctx) error {
7373
// Rewrite the target backend if a mapping rule matches the hostname
7474
if hostMap, hostMapFound := config.Config.ImageMap[reqHost]; hostMapFound {
7575
log.Debugf("Found host mapping %s -> %s", reqHostname, hostMap)
76-
targetHostUrl, _ := url.Parse(hostMap)
77-
targetHostName = targetHostUrl.Host
78-
targetHost = targetHostUrl.Scheme + "://" + targetHostUrl.Host
79-
proxyMode = true
76+
77+
// Check if hostMap is a URL or local path
78+
if strings.HasPrefix(hostMap, "http://") || strings.HasPrefix(hostMap, "https://") {
79+
// Remote URL mapping
80+
targetHostUrl, _ := url.Parse(hostMap)
81+
targetHostName = targetHostUrl.Host
82+
targetHost = targetHostUrl.Scheme + "://" + targetHostUrl.Host
83+
proxyMode = true
84+
} else {
85+
// Local path mapping
86+
targetHost = hostMap
87+
proxyMode = false
88+
mapMode = true
89+
}
8090
} else {
8191
// There's not matching host mapping, now check for any URI map that apply
8292
httpRegexpMatcher := regexp.MustCompile(config.HttpRegexp)
@@ -121,7 +131,11 @@ func Convert(c *fiber.Ctx) error {
121131
// Since here we've already sent non-image file, "raw" is not supported by default in the following code
122132
if config.AllowAllExtensions && !helper.CheckImageExtension(filename) {
123133
if !proxyMode {
124-
return c.SendFile(path.Join(config.Config.ImgPath, reqURI))
134+
if !mapMode {
135+
return c.SendFile(path.Join(config.Config.ImgPath, reqURI))
136+
} else {
137+
return c.SendFile(path.Join(targetHost, reqURI))
138+
}
125139
} else {
126140
// If the file is not in the ImgPath, we'll have to use the proxy mode to download it
127141
_ = fetchRemoteImg(realRemoteAddr, targetHostName)
@@ -145,7 +159,7 @@ func Convert(c *fiber.Ctx) error {
145159
// by default images are hosted in ImgPath
146160
rawImageAbs = path.Join(config.Config.ImgPath, reqURI)
147161
} else {
148-
rawImageAbs = reqURI
162+
rawImageAbs = path.Join(targetHost, reqURI)
149163
}
150164
// detect if source file has changed
151165
if metadata.Checksum != helper.HashFile(rawImageAbs) {

0 commit comments

Comments
 (0)