From df824fa31b4a760bc43d2a2556e5cf134884d1a3 Mon Sep 17 00:00:00 2001 From: spf13 Date: Mon, 16 Nov 2015 21:55:18 -0500 Subject: [PATCH] Hugo server renders and serves from memory by default (30%+ improvement) --- commands/server.go | 30 ++++++++++++++++++++++++++---- docs/content/meta/release-notes.md | 1 + hugofs/fs.go | 2 -- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/commands/server.go b/commands/server.go index 49f0e1e59..7c0ef78dd 100644 --- a/commands/server.go +++ b/commands/server.go @@ -37,17 +37,22 @@ var serverInterface string var serverWatch bool var serverAppend bool var disableLiveReload bool +var renderToDisk bool //var serverCmdV *cobra.Command var serverCmd = &cobra.Command{ - Use: "server", - Short: "A high performance webserver", - Long: `Hugo provides its own webserver which builds and serves the site. + Use: "server", + Aliases: []string{"serve"}, + Short: "A high performance webserver", + Long: `Hugo provides it's own webserver which builds and serves the site. While hugo server is high performance, it is a webserver with limited options. Many run it in production, but the standard behavior is for people to use it in development and use a more full featured server such as Nginx or Caddy. +'hugo server' will avoid writing the rendered and served content to disk, +preferring to store it in memory. + Often server is paired with '--watch' which Hugo will look for changes to the source and continously rebuild and serve the website.`, //Run: server, @@ -79,6 +84,7 @@ func init() { serverCmd.Flags().BoolVarP(&serverWatch, "watch", "w", false, "watch filesystem for changes and recreate as needed") serverCmd.Flags().BoolVarP(&serverAppend, "appendPort", "", true, "append port to baseurl") serverCmd.Flags().BoolVar(&disableLiveReload, "disableLiveReload", false, "watch without enabling live browser reload on rebuild") + serverCmd.Flags().BoolVar(&renderToDisk, "renderToDisk", false, "render to Destination path (default is render to memory & serve from there)") serverCmd.Flags().BoolVarP(&NoTimes, "noTimes", "", false, "Don't sync modification time of files") serverCmd.Flags().String("memstats", "", "log memory usage to this file") serverCmd.Flags().Int("meminterval", 100, "interval to poll memory usage (requires --memstats)") @@ -126,6 +132,18 @@ func server(cmd *cobra.Command, args []string) { jww.ERROR.Println("memstats error:", err) } + // If a Destination is provided via flag write to disk + if Destination != "" { + renderToDisk = true + } + + // Hugo writes the output to memory instead of the disk + if !renderToDisk { + hugofs.DestinationFS = new(afero.MemMapFs) + // Rendering to memoryFS, publish to Root regardless of publishDir. + viper.Set("PublishDir", "/") + } + build(serverWatch) // Watch runs its own server as part of the routine @@ -148,7 +166,11 @@ func server(cmd *cobra.Command, args []string) { } func serve(port int) { - jww.FEEDBACK.Println("Serving pages from " + helpers.AbsPathify(viper.GetString("PublishDir"))) + if renderToDisk { + jww.FEEDBACK.Println("Serving pages from " + helpers.AbsPathify(viper.GetString("PublishDir"))) + } else { + jww.FEEDBACK.Println("Serving pages from memory") + } httpFs := &afero.HttpFs{SourceFs: hugofs.DestinationFS} fs := filesOnlyFs{httpFs.Dir(helpers.AbsPathify(viper.GetString("PublishDir")))} diff --git a/docs/content/meta/release-notes.md b/docs/content/meta/release-notes.md index 630395b0a..e81a60d3c 100644 --- a/docs/content/meta/release-notes.md +++ b/docs/content/meta/release-notes.md @@ -11,6 +11,7 @@ weight: 10 --- ## **0.15.0** ??? +* `hugo server` now builds ~30%+ faster by rendering to memory instead of disk * Have Jekyll site, but dreaming of porting it to Hugo? This release introduces a new `hugo import jekyll`command that makes this easier than ever. [1469](https://github.com/spf13/hugo/pull/1469) * We now use a custom-built `LazyFileReader` for reading file contents, which means we don't read media files in `/content` into memory anymore -- and file reading is now performed in parallel on multicore PCs. [1181](https://github.com/spf13/hugo/issues/1181) * Hugo is now built with `Go 1.5` which, among many other improvements, have fixed the last known data race in Hugo. [917] (https://github.com/spf13/hugo/issues/917) diff --git a/hugofs/fs.go b/hugofs/fs.go index 28b65086a..26e17ebb1 100644 --- a/hugofs/fs.go +++ b/hugofs/fs.go @@ -18,5 +18,3 @@ import "github.com/spf13/afero" var SourceFs afero.Fs = new(afero.OsFs) var DestinationFS afero.Fs = new(afero.OsFs) var OsFs afero.Fs = new(afero.OsFs) - -//var DestinationFS afero.Fs = new(afero.MemMapFs)