mirror of https://github.com/jlelse/GoBlog
29 changed files with 227 additions and 110 deletions
@ -1,13 +0,0 @@
|
||||
package main |
||||
|
||||
import "strings" |
||||
|
||||
func (blog *configBlog) getRelativePath(path string) string { |
||||
if !strings.HasPrefix(path, "/") { |
||||
path = "/" + path |
||||
} |
||||
if blog.Path != "/" { |
||||
return blog.Path + path |
||||
} |
||||
return path |
||||
} |
@ -0,0 +1,45 @@
|
||||
package main |
||||
|
||||
import "strings" |
||||
|
||||
func (a *goBlog) getRelativePath(blog, path string) string { |
||||
// Get blog
|
||||
bc := a.cfg.Blogs[blog] |
||||
if bc == nil { |
||||
return "" |
||||
} |
||||
// Get relative path
|
||||
return bc.getRelativePath(path) |
||||
} |
||||
|
||||
func (blog *configBlog) getRelativePath(path string) string { |
||||
// Check if path is absolute
|
||||
if !strings.HasPrefix(path, "/") { |
||||
path = "/" + path |
||||
} |
||||
// Check if blog uses subpath
|
||||
if blog.Path != "" && blog.Path != "/" { |
||||
// Check if path is root
|
||||
if path == "/" { |
||||
path = blog.Path |
||||
} else { |
||||
path = blog.Path + path |
||||
} |
||||
} |
||||
return path |
||||
} |
||||
|
||||
func (a *goBlog) getFullAddress(path string) string { |
||||
// Call method with just the relevant config
|
||||
return a.cfg.Server.getFullAddress(path) |
||||
} |
||||
|
||||
func (cfg *configServer) getFullAddress(path string) string { |
||||
// Remove trailing slash
|
||||
pa := strings.TrimSuffix(cfg.PublicAddress, "/") |
||||
// Check if path is root => blank path
|
||||
if path == "/" { |
||||
path = "" |
||||
} |
||||
return pa + path |
||||
} |
@ -0,0 +1,108 @@
|
||||
package main |
||||
|
||||
import ( |
||||
"reflect" |
||||
"testing" |
||||
) |
||||
|
||||
func Test_getFullAddress(t *testing.T) { |
||||
cfg1 := &configServer{ |
||||
PublicAddress: "https://example.com", |
||||
} |
||||
|
||||
cfg2 := &configServer{ |
||||
PublicAddress: "https://example.com/", |
||||
} |
||||
|
||||
app := &goBlog{ |
||||
cfg: &config{ |
||||
Server: cfg1, |
||||
}, |
||||
} |
||||
|
||||
if got := cfg1.getFullAddress("/test"); !reflect.DeepEqual(got, "https://example.com/test") { |
||||
t.Errorf("Wrong full path, got: %v", got) |
||||
} |
||||
|
||||
if got := cfg2.getFullAddress("/test"); !reflect.DeepEqual(got, "https://example.com/test") { |
||||
t.Errorf("Wrong full path, got: %v", got) |
||||
} |
||||
|
||||
if got := app.getFullAddress("/test"); !reflect.DeepEqual(got, "https://example.com/test") { |
||||
t.Errorf("Wrong full path, got: %v", got) |
||||
} |
||||
|
||||
if got := cfg1.getFullAddress("/"); !reflect.DeepEqual(got, "https://example.com") { |
||||
t.Errorf("Wrong full path, got: %v", got) |
||||
} |
||||
|
||||
if got := cfg1.getFullAddress(""); !reflect.DeepEqual(got, "https://example.com") { |
||||
t.Errorf("Wrong full path, got: %v", got) |
||||
} |
||||
} |
||||
|
||||
func Test_getRelativeBlogPath(t *testing.T) { |
||||
blog1 := &configBlog{ |
||||
Path: "", |
||||
} |
||||
|
||||
blog2 := &configBlog{ |
||||
Path: "", |
||||
} |
||||
|
||||
blog3 := &configBlog{ |
||||
Path: "/de", |
||||
} |
||||
|
||||
if got := blog1.getRelativePath(""); !reflect.DeepEqual(got, "/") { |
||||
t.Errorf("Wrong relative blog path, got: %v", got) |
||||
} |
||||
|
||||
if got := blog2.getRelativePath(""); !reflect.DeepEqual(got, "/") { |
||||
t.Errorf("Wrong relative blog path, got: %v", got) |
||||
} |
||||
|
||||
if got := blog3.getRelativePath(""); !reflect.DeepEqual(got, "/de") { |
||||
t.Errorf("Wrong relative blog path, got: %v", got) |
||||
} |
||||
|
||||
if got := blog1.getRelativePath("test"); !reflect.DeepEqual(got, "/test") { |
||||
t.Errorf("Wrong relative blog path, got: %v", got) |
||||
} |
||||
|
||||
if got := blog2.getRelativePath("test"); !reflect.DeepEqual(got, "/test") { |
||||
t.Errorf("Wrong relative blog path, got: %v", got) |
||||
} |
||||
|
||||
if got := blog3.getRelativePath("test"); !reflect.DeepEqual(got, "/de/test") { |
||||
t.Errorf("Wrong relative blog path, got: %v", got) |
||||
} |
||||
|
||||
if got := blog1.getRelativePath("/test"); !reflect.DeepEqual(got, "/test") { |
||||
t.Errorf("Wrong relative blog path, got: %v", got) |
||||
} |
||||
|
||||
if got := blog2.getRelativePath("/test"); !reflect.DeepEqual(got, "/test") { |
||||
t.Errorf("Wrong relative blog path, got: %v", got) |
||||
} |
||||
|
||||
if got := blog3.getRelativePath("/test"); !reflect.DeepEqual(got, "/de/test") { |
||||
t.Errorf("Wrong relative blog path, got: %v", got) |
||||
} |
||||
|
||||
app := &goBlog{ |
||||
cfg: &config{ |
||||
Blogs: map[string]*configBlog{ |
||||
"de": blog3, |
||||
}, |
||||
}, |
||||
} |
||||
|
||||
if got := app.getRelativePath("de", "/test"); !reflect.DeepEqual(got, "/de/test") { |
||||
t.Errorf("Wrong relative blog path, got: %v", got) |
||||
} |
||||
|
||||
if got := app.getRelativePath("", "/test"); !reflect.DeepEqual(got, "") { |
||||
t.Errorf("Wrong relative blog path, got: %v", got) |
||||
} |
||||
} |
Loading…
Reference in new issue