mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
Support subdir in baseurl.
Mainly this was a change to helpers.MakePermalink, but to get the local server to run correctly, we needed to redirect the path of the request from /foo to /. In addition, I added tests for the server's code for fixing up the base url with different config file & CLI options.
This commit is contained in:
parent
4b979b17cc
commit
a31edb3388
5 changed files with 139 additions and 21 deletions
|
@ -17,6 +17,7 @@ import (
|
|||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -54,10 +55,6 @@ func init() {
|
|||
func server(cmd *cobra.Command, args []string) {
|
||||
InitializeConfig()
|
||||
|
||||
if BaseUrl == "" {
|
||||
BaseUrl = "http://localhost"
|
||||
}
|
||||
|
||||
if cmd.Flags().Lookup("disableLiveReload").Changed {
|
||||
viper.Set("DisableLiveReload", disableLiveReload)
|
||||
}
|
||||
|
@ -66,10 +63,6 @@ func server(cmd *cobra.Command, args []string) {
|
|||
viper.Set("Watch", true)
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(BaseUrl, "http://") {
|
||||
BaseUrl = "http://" + BaseUrl
|
||||
}
|
||||
|
||||
l, err := net.Listen("tcp", ":"+strconv.Itoa(serverPort))
|
||||
if err == nil {
|
||||
l.Close()
|
||||
|
@ -85,11 +78,11 @@ func server(cmd *cobra.Command, args []string) {
|
|||
|
||||
viper.Set("port", serverPort)
|
||||
|
||||
if serverAppend {
|
||||
viper.Set("BaseUrl", strings.TrimSuffix(BaseUrl, "/")+":"+strconv.Itoa(serverPort))
|
||||
} else {
|
||||
viper.Set("BaseUrl", strings.TrimSuffix(BaseUrl, "/"))
|
||||
BaseUrl, err := fixUrl(BaseUrl)
|
||||
if err != nil {
|
||||
jww.ERROR.Fatal(err)
|
||||
}
|
||||
viper.Set("BaseUrl", BaseUrl)
|
||||
|
||||
build(serverWatch)
|
||||
|
||||
|
@ -110,10 +103,57 @@ func serve(port int) {
|
|||
jww.FEEDBACK.Printf("Web Server is available at %s\n", viper.GetString("BaseUrl"))
|
||||
fmt.Println("Press ctrl+c to stop")
|
||||
|
||||
http.Handle("/", http.FileServer(http.Dir(helpers.AbsPathify(viper.GetString("PublishDir")))))
|
||||
err := http.ListenAndServe(":"+strconv.Itoa(port), nil)
|
||||
fileserver := http.FileServer(http.Dir(helpers.AbsPathify(viper.GetString("PublishDir"))))
|
||||
|
||||
u, err := url.Parse(viper.GetString("BaseUrl"))
|
||||
if err != nil {
|
||||
jww.ERROR.Fatalf("Invalid BaseUrl: %s", err)
|
||||
}
|
||||
if u.Path == "" || u.Path == "/" {
|
||||
http.Handle("/", fileserver)
|
||||
} else {
|
||||
http.Handle(u.Path+"/", http.StripPrefix(u.Path+"/", fileserver))
|
||||
}
|
||||
|
||||
err = http.ListenAndServe(":"+strconv.Itoa(port), nil)
|
||||
if err != nil {
|
||||
jww.ERROR.Printf("Error: %s\n", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func fixUrl(s string) (string, error) {
|
||||
useLocalhost := false
|
||||
if s == "" {
|
||||
s = viper.GetString("BaseUrl")
|
||||
useLocalhost = true
|
||||
}
|
||||
if !strings.HasPrefix(s, "http://") {
|
||||
s = "http://" + s
|
||||
}
|
||||
u, err := url.Parse(s)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if serverAppend {
|
||||
if useLocalhost {
|
||||
u.Host = fmt.Sprintf("localhost:%d", serverPort)
|
||||
return u.String(), nil
|
||||
}
|
||||
host := u.Host
|
||||
if strings.Contains(host, ":") {
|
||||
host, _, err = net.SplitHostPort(u.Host)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Failed to split BaseUrl hostpost: %s", err)
|
||||
}
|
||||
}
|
||||
u.Host = fmt.Sprintf("%s:%d", host, serverPort)
|
||||
return u.String(), nil
|
||||
}
|
||||
|
||||
if useLocalhost {
|
||||
u.Host = "localhost"
|
||||
}
|
||||
return u.String(), nil
|
||||
}
|
||||
|
|
42
commands/server_test.go
Normal file
42
commands/server_test.go
Normal file
|
@ -0,0 +1,42 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func TestFixUrl(t *testing.T) {
|
||||
type data struct {
|
||||
TestName string
|
||||
CliBaseUrl string
|
||||
CfgBaseUrl string
|
||||
AppendPort bool
|
||||
Port int
|
||||
Result string
|
||||
}
|
||||
tests := []data{
|
||||
{"Basic localhost", "", "http://foo.com", true, 1313, "http://localhost:1313"},
|
||||
{"Basic subdir", "", "http://foo.com/bar", true, 1313, "http://localhost:1313/bar"},
|
||||
{"Basic production", "http://foo.com", "http://foo.com", false, 80, "http://foo.com"},
|
||||
{"Production subdir", "http://foo.com/bar", "http://foo.com/bar", false, 80, "http://foo.com/bar"},
|
||||
{"No http", "", "foo.com", true, 1313, "http://localhost:1313"},
|
||||
{"Override configured port", "", "foo.com:2020", true, 1313, "http://localhost:1313"},
|
||||
{"No http production", "foo.com", "foo.com", false, 80, "http://foo.com"},
|
||||
{"No http production with port", "foo.com", "foo.com", true, 2020, "http://foo.com:2020"},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
BaseUrl = test.CliBaseUrl
|
||||
viper.Set("BaseUrl", test.CfgBaseUrl)
|
||||
serverAppend = test.AppendPort
|
||||
serverPort = test.Port
|
||||
result, err := fixUrl(BaseUrl)
|
||||
if err != nil {
|
||||
t.Errorf("Test #%d %s: unexpected error %s", err)
|
||||
}
|
||||
if result != test.Result {
|
||||
t.Errorf("Test #%d %s: expected %q, got %q", i, test.TestName, test.Result, result)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -97,3 +97,25 @@ func TestUrlize(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMakePermalink(t *testing.T) {
|
||||
type test struct {
|
||||
host, link, output string
|
||||
}
|
||||
|
||||
data := []test{
|
||||
{"http://abc.com/foo", "post/bar", "http://abc.com/foo/post/bar"},
|
||||
{"http://abc.com/foo/", "post/bar", "http://abc.com/foo/post/bar"},
|
||||
{"http://abc.com", "post/bar", "http://abc.com/post/bar"},
|
||||
{"http://abc.com", "bar", "http://abc.com/bar"},
|
||||
{"http://abc.com/foo/bar", "post/bar", "http://abc.com/foo/bar/post/bar"},
|
||||
{"http://abc.com/foo/bar", "post/bar/", "http://abc.com/foo/bar/post/bar/"},
|
||||
}
|
||||
|
||||
for i, d := range data {
|
||||
output := MakePermalink(d.host, d.link).String()
|
||||
if d.output != output {
|
||||
t.Errorf("Test #%d failed. Expected %q got %q", i, d.output, output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,8 +14,10 @@
|
|||
package helpers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/PuerkitoBio/purell"
|
||||
)
|
||||
|
@ -57,11 +59,23 @@ func MakePermalink(host, plink string) *url.URL {
|
|||
panic(err)
|
||||
}
|
||||
|
||||
path, err := url.Parse(plink)
|
||||
p, err := url.Parse(plink)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return base.ResolveReference(path)
|
||||
|
||||
if p.Host != "" {
|
||||
panic(fmt.Errorf("Can't make permalink from absolute link %q", plink))
|
||||
}
|
||||
|
||||
base.Path = path.Join(base.Path, p.Path)
|
||||
|
||||
// path.Join will strip off the last /, so put it back if it was there.
|
||||
if strings.HasSuffix(p.Path, "/") && !strings.HasSuffix(base.Path, "/") {
|
||||
base.Path = base.Path + "/"
|
||||
}
|
||||
|
||||
return base
|
||||
}
|
||||
|
||||
func UrlPrep(ugly bool, in string) string {
|
||||
|
|
|
@ -33,7 +33,7 @@ func TestPermalink(t *testing.T) {
|
|||
{"x/y/z/boofar.md", "x/y/z", "", "", "/z/y/q/", false, "/z/y/q/", "/z/y/q/"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
for i, test := range tests {
|
||||
viper.Set("uglyurls", test.uglyurls)
|
||||
p := &Page{
|
||||
Node: Node{
|
||||
|
@ -56,22 +56,22 @@ func TestPermalink(t *testing.T) {
|
|||
|
||||
u, err := p.Permalink()
|
||||
if err != nil {
|
||||
t.Errorf("Unable to process permalink: %s", err)
|
||||
t.Errorf("Test %d: Unable to process permalink: %s", i, err)
|
||||
}
|
||||
|
||||
expected := test.expectedAbs
|
||||
if u != expected {
|
||||
t.Errorf("Expected abs url: %s, got: %s", expected, u)
|
||||
t.Errorf("Test %d: Expected abs url: %s, got: %s", i, expected, u)
|
||||
}
|
||||
|
||||
u, err = p.RelPermalink()
|
||||
if err != nil {
|
||||
t.Errorf("Unable to process permalink: %s", err)
|
||||
t.Errorf("Test %d: Unable to process permalink: %s", i, err)
|
||||
}
|
||||
|
||||
expected = test.expectedRel
|
||||
if u != expected {
|
||||
t.Errorf("Expected abs url: %s, got: %s", expected, u)
|
||||
t.Errorf("Test %d: Expected abs url: %s, got: %s", i, expected, u)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue