diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-11-12 10:03:56 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-11-17 11:01:46 +0100 |
commit | 60dfb9a6e076200ab3ca3fd30e34bb3c14e0a893 (patch) | |
tree | 810d3d7ca40a55045fec4a0718eb7728621495e4 /commands/server.go | |
parent | 2e0465764b5dacc511b977b1c9aa07324ad0ee9c (diff) |
Add support for multiple staticDirs
This commit adds support for multiple statDirs both on the global and language level.
A simple `config.toml` example:
```bash
staticDir = ["static1", "static2"]
[languages]
[languages.no]
staticDir = ["staticDir_override", "static_no"]
baseURL = "https://example.no"
languageName = "Norsk"
weight = 1
title = "På norsk"
[languages.en]
staticDir2 = "static_en"
baseURL = "https://example.com"
languageName = "English"
weight = 2
title = "In English"
```
In the above, with no theme used:
the English site will get its static files as a union of "static1", "static2" and "static_en". On file duplicates, the right-most version will win.
the Norwegian site will get its static files as a union of "staticDir_override" and "static_no".
This commit also concludes the Multihost support in #4027.
Fixes #36
Closes #4027
Diffstat (limited to 'commands/server.go')
-rw-r--r-- | commands/server.go | 71 |
1 files changed, 40 insertions, 31 deletions
diff --git a/commands/server.go b/commands/server.go index bd45e7054..666f255e3 100644 --- a/commands/server.go +++ b/commands/server.go @@ -25,6 +25,8 @@ import ( "strings" "time" + "github.com/gohugoio/hugo/livereload" + "github.com/gohugoio/hugo/config" "github.com/gohugoio/hugo/helpers" @@ -189,7 +191,7 @@ func server(cmd *cobra.Command, args []string) error { if err != nil { return err } - c.Cfg.Set("baseURL", baseURL) + c.Set("baseURL", baseURL) } if err := memStats(); err != nil { @@ -218,16 +220,22 @@ func server(cmd *cobra.Command, args []string) error { // Watch runs its own server as part of the routine if serverWatch { - watchDirs := c.getDirList() + + watchDirs, err := c.getDirList() + if err != nil { + return err + } + baseWatchDir := c.Cfg.GetString("workingDir") + relWatchDirs := make([]string, len(watchDirs)) for i, dir := range watchDirs { - watchDirs[i], _ = helpers.GetRelativePath(dir, baseWatchDir) + relWatchDirs[i], _ = helpers.GetRelativePath(dir, baseWatchDir) } - rootWatchDirs := strings.Join(helpers.UniqueStrings(helpers.ExtractRootPaths(watchDirs)), ",") + rootWatchDirs := strings.Join(helpers.UniqueStrings(helpers.ExtractRootPaths(relWatchDirs)), ",") jww.FEEDBACK.Printf("Watching for changes in %s%s{%s}\n", baseWatchDir, helpers.FilePathSeparator, rootWatchDirs) - err := c.newWatcher(serverPort) + err = c.newWatcher(true, watchDirs...) if err != nil { return err @@ -238,7 +246,7 @@ func server(cmd *cobra.Command, args []string) error { } type fileServer struct { - basePort int + ports []int baseURLs []string roots []string c *commandeer @@ -247,7 +255,7 @@ type fileServer struct { func (f *fileServer) createEndpoint(i int) (*http.ServeMux, string, error) { baseURL := f.baseURLs[i] root := f.roots[i] - port := f.basePort + i + port := f.ports[i] publishDir := f.c.Cfg.GetString("publishDir") @@ -257,11 +265,12 @@ func (f *fileServer) createEndpoint(i int) (*http.ServeMux, string, error) { absPublishDir := f.c.PathSpec().AbsPathify(publishDir) - // TODO(bep) multihost unify feedback - if renderToDisk { - jww.FEEDBACK.Println("Serving pages from " + absPublishDir) - } else { - jww.FEEDBACK.Println("Serving pages from memory") + if i == 0 { + if renderToDisk { + jww.FEEDBACK.Println("Serving pages from " + absPublishDir) + } else { + jww.FEEDBACK.Println("Serving pages from memory") + } } httpFs := afero.NewHttpFs(f.c.Fs.Destination) @@ -270,7 +279,7 @@ func (f *fileServer) createEndpoint(i int) (*http.ServeMux, string, error) { doLiveReload := !buildWatch && !f.c.Cfg.GetBool("disableLiveReload") fastRenderMode := doLiveReload && !f.c.Cfg.GetBool("disableFastRender") - if fastRenderMode { + if i == 0 && fastRenderMode { jww.FEEDBACK.Println("Running in Fast Render Mode. For full rebuilds on change: hugo server --disableFastRender") } @@ -311,49 +320,50 @@ func (f *fileServer) createEndpoint(i int) (*http.ServeMux, string, error) { return mu, endpoint, nil } -func (c *commandeer) roots() []string { - var roots []string - languages := c.languages() - isMultiHost := languages.IsMultihost() - if !isMultiHost { - return roots - } - - for _, l := range languages { - roots = append(roots, l.Lang) - } - return roots -} +func (c *commandeer) serve() { -func (c *commandeer) serve(port int) { - // TODO(bep) multihost isMultiHost := Hugo.IsMultihost() var ( baseURLs []string roots []string + ports []int ) if isMultiHost { for _, s := range Hugo.Sites { baseURLs = append(baseURLs, s.BaseURL.String()) roots = append(roots, s.Language.Lang) + ports = append(ports, s.Info.ServerPort()) } } else { - baseURLs = []string{Hugo.Sites[0].BaseURL.String()} + s := Hugo.Sites[0] + baseURLs = []string{s.BaseURL.String()} roots = []string{""} + ports = append(ports, s.Info.ServerPort()) } srv := &fileServer{ - basePort: port, + ports: ports, baseURLs: baseURLs, roots: roots, c: c, } + doLiveReload := !c.Cfg.GetBool("disableLiveReload") + + if doLiveReload { + livereload.Initialize() + } + for i, _ := range baseURLs { mu, endpoint, err := srv.createEndpoint(i) + if doLiveReload { + mu.HandleFunc("/livereload.js", livereload.ServeJS) + mu.HandleFunc("/livereload", livereload.Handler) + } + jww.FEEDBACK.Printf("Web Server is available at %s (bind address %s)\n", endpoint, serverInterface) go func() { err = http.ListenAndServe(endpoint, mu) if err != nil { @@ -363,7 +373,6 @@ func (c *commandeer) serve(port int) { }() } - // TODO(bep) multihost jww.FEEDBACK.Printf("Web Server is available at %s (bind address %s)\n", u.String(), serverInterface) jww.FEEDBACK.Println("Press Ctrl+C to stop") } |