2016-03-21 19:28:42 -04:00
// Copyright 2016 The Hugo Authors. All rights reserved.
2013-09-29 02:09:03 -04:00
//
2015-11-23 22:16:36 -05:00
// Licensed under the Apache License, Version 2.0 (the "License");
2013-09-29 02:09:03 -04:00
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2015-11-23 22:16:36 -05:00
// http://www.apache.org/licenses/LICENSE-2.0
2013-09-29 02:09:03 -04:00
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package commands
import (
"fmt"
2014-05-15 15:07:46 -04:00
"net"
2013-09-29 02:09:03 -04:00
"net/http"
2014-08-22 07:59:59 -04:00
"net/url"
2014-01-26 04:48:00 -05:00
"os"
2017-11-02 03:25:20 -04:00
"path/filepath"
2014-09-22 09:45:05 -04:00
"runtime"
2013-09-29 02:09:03 -04:00
"strconv"
2013-11-22 00:28:05 -05:00
"strings"
2014-09-22 09:45:05 -04:00
"time"
2014-03-31 13:23:34 -04:00
2017-11-12 04:03:56 -05:00
"github.com/gohugoio/hugo/livereload"
2017-06-13 12:42:45 -04:00
"github.com/gohugoio/hugo/config"
2017-11-02 03:25:20 -04:00
2017-06-13 12:42:45 -04:00
"github.com/gohugoio/hugo/helpers"
2017-06-13 13:07:35 -04:00
"github.com/spf13/afero"
"github.com/spf13/cobra"
2014-03-31 13:23:34 -04:00
jww "github.com/spf13/jwalterweatherman"
2013-09-29 02:09:03 -04:00
)
2016-02-05 12:41:40 -05:00
var (
disableLiveReload bool
2017-06-26 15:34:16 -04:00
navigateToChanged bool
2016-02-05 12:41:40 -05:00
renderToDisk bool
serverAppend bool
serverInterface string
serverPort int
2017-09-23 11:08:48 -04:00
liveReloadPort int
2016-02-05 12:41:40 -05:00
serverWatch bool
2017-09-22 11:23:15 -04:00
noHTTPCache bool
Only re-render the view(s) you're working on
Hugo already, in its server mode, support partial rebuilds. To put it simply: If you change `about.md`, only that content page is read and processed, then Hugo does some processing (taxonomies etc.) and the full site is rendered.
This commit covers the rendering part: We now only re-render the pages you work on, i.e. the last n pages you watched in the browser (which obviously also includes the page in the example above).
To be more specific: When you are running the hugo server in watch (aka. livereload) mode, and change a template or a content file, then we do a partial re-rendering of the following:
* The current content page (if it is a content change)
* The home page
* Up to the last 10 pages you visited on the site.
This should in most cases be enough, but if you navigate to something completely different, you may see stale content. Doing an edit will then refresh that page.
Note that this feature is enabled by default. To turn it off, run `hugo server --disableFastRender`.
Fixes #3962
See #1643
2017-10-14 07:40:43 -04:00
disableFastRender bool
2016-02-05 12:41:40 -05:00
)
2013-09-29 02:09:03 -04:00
var serverCmd = & cobra . Command {
2015-11-16 21:55:18 -05:00
Use : "server" ,
Aliases : [ ] string { "serve" } ,
Short : "A high performance webserver" ,
2015-11-21 08:31:10 -05:00
Long : ` Hugo provides its own webserver which builds and serves the site .
2015-11-12 10:23:41 -05:00
While hugo server is high performance , it is a webserver with limited options .
2015-11-21 08:31:10 -05:00
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 .
2015-11-12 10:23:41 -05:00
2015-11-16 21:55:18 -05:00
' hugo server ' will avoid writing the rendered and served content to disk ,
preferring to store it in memory .
2015-11-20 10:13:03 -05:00
By default hugo will also watch your files for any changes you make and
automatically rebuild the site . It will then live reload any open browser pages
2015-11-21 08:31:10 -05:00
and push the latest content to them . As most Hugo sites are built in a fraction
of a second , you will be able to save and see your changes nearly instantly . ` ,
2015-12-02 05:42:53 -05:00
//RunE: server,
2014-05-16 17:49:27 -04:00
}
2015-10-23 12:21:37 -04:00
type filesOnlyFs struct {
fs http . FileSystem
}
type noDirFile struct {
http . File
}
func ( fs filesOnlyFs ) Open ( name string ) ( http . File , error ) {
f , err := fs . fs . Open ( name )
if err != nil {
return nil , err
}
return noDirFile { f } , nil
}
func ( f noDirFile ) Readdir ( count int ) ( [ ] os . FileInfo , error ) {
return nil , nil
}
2014-05-16 17:49:27 -04:00
func init ( ) {
2016-02-05 12:41:40 -05:00
initHugoBuilderFlags ( serverCmd )
2015-05-01 22:28:21 -04:00
serverCmd . Flags ( ) . IntVarP ( & serverPort , "port" , "p" , 1313 , "port on which the server will listen" )
2017-09-23 11:08:48 -04:00
serverCmd . Flags ( ) . IntVar ( & liveReloadPort , "liveReloadPort" , - 1 , "port for live reloading (i.e. 443 in HTTPS proxy situations)" )
2015-05-01 22:28:21 -04:00
serverCmd . Flags ( ) . StringVarP ( & serverInterface , "bind" , "" , "127.0.0.1" , "interface to which the server will bind" )
2015-11-20 10:13:03 -05:00
serverCmd . Flags ( ) . BoolVarP ( & serverWatch , "watch" , "w" , true , "watch filesystem for changes and recreate as needed" )
2017-09-26 12:58:44 -04:00
serverCmd . Flags ( ) . BoolVar ( & noHTTPCache , "noHTTPCache" , false , "prevent HTTP caching" )
2016-10-24 14:56:00 -04:00
serverCmd . Flags ( ) . BoolVarP ( & serverAppend , "appendPort" , "" , true , "append port to baseURL" )
2014-05-16 17:49:27 -04:00
serverCmd . Flags ( ) . BoolVar ( & disableLiveReload , "disableLiveReload" , false , "watch without enabling live browser reload on rebuild" )
2017-06-26 15:34:16 -04:00
serverCmd . Flags ( ) . BoolVar ( & navigateToChanged , "navigateToChanged" , false , "navigate to changed content file on live browser reload" )
2015-11-16 21:55:18 -05:00
serverCmd . Flags ( ) . BoolVar ( & renderToDisk , "renderToDisk" , false , "render to Destination path (default is render to memory & serve from there)" )
Only re-render the view(s) you're working on
Hugo already, in its server mode, support partial rebuilds. To put it simply: If you change `about.md`, only that content page is read and processed, then Hugo does some processing (taxonomies etc.) and the full site is rendered.
This commit covers the rendering part: We now only re-render the pages you work on, i.e. the last n pages you watched in the browser (which obviously also includes the page in the example above).
To be more specific: When you are running the hugo server in watch (aka. livereload) mode, and change a template or a content file, then we do a partial re-rendering of the following:
* The current content page (if it is a content change)
* The home page
* Up to the last 10 pages you visited on the site.
This should in most cases be enough, but if you navigate to something completely different, you may see stale content. Doing an edit will then refresh that page.
Note that this feature is enabled by default. To turn it off, run `hugo server --disableFastRender`.
Fixes #3962
See #1643
2017-10-14 07:40:43 -04:00
serverCmd . Flags ( ) . BoolVar ( & disableFastRender , "disableFastRender" , false , "enables full re-renders on changes" )
2014-09-22 09:45:05 -04:00
serverCmd . Flags ( ) . String ( "memstats" , "" , "log memory usage to this file" )
2016-08-02 13:48:07 -04:00
serverCmd . Flags ( ) . String ( "meminterval" , "100ms" , "interval to poll memory usage (requires --memstats), valid time units are \"ns\", \"us\" (or \"µs\"), \"ms\", \"s\", \"m\", \"h\"." )
2015-12-02 05:42:53 -05:00
serverCmd . RunE = server
2016-03-06 08:10:06 -05:00
2013-09-29 02:09:03 -04:00
}
2015-12-02 05:42:53 -05:00
func server ( cmd * cobra . Command , args [ ] string ) error {
2017-01-11 12:53:51 -05:00
cfg , err := InitializeConfig ( serverCmd )
2017-01-03 11:28:51 -05:00
if err != nil {
2015-12-02 13:56:36 -05:00
return err
}
2013-09-29 02:09:03 -04:00
2017-03-25 09:37:04 -04:00
c , err := newCommandeer ( cfg )
if err != nil {
return err
}
2017-01-10 04:55:03 -05:00
2017-05-24 04:51:48 -04:00
if cmd . Flags ( ) . Changed ( "disableLiveReload" ) {
2017-02-04 22:20:06 -05:00
c . Set ( "disableLiveReload" , disableLiveReload )
2014-05-16 17:49:27 -04:00
}
2017-06-26 15:34:16 -04:00
if cmd . Flags ( ) . Changed ( "navigateToChanged" ) {
c . Set ( "navigateToChanged" , navigateToChanged )
}
Only re-render the view(s) you're working on
Hugo already, in its server mode, support partial rebuilds. To put it simply: If you change `about.md`, only that content page is read and processed, then Hugo does some processing (taxonomies etc.) and the full site is rendered.
This commit covers the rendering part: We now only re-render the pages you work on, i.e. the last n pages you watched in the browser (which obviously also includes the page in the example above).
To be more specific: When you are running the hugo server in watch (aka. livereload) mode, and change a template or a content file, then we do a partial re-rendering of the following:
* The current content page (if it is a content change)
* The home page
* Up to the last 10 pages you visited on the site.
This should in most cases be enough, but if you navigate to something completely different, you may see stale content. Doing an edit will then refresh that page.
Note that this feature is enabled by default. To turn it off, run `hugo server --disableFastRender`.
Fixes #3962
See #1643
2017-10-14 07:40:43 -04:00
if cmd . Flags ( ) . Changed ( "disableFastRender" ) {
c . Set ( "disableFastRender" , disableFastRender )
}
2014-05-16 17:49:27 -04:00
if serverWatch {
2017-02-04 22:20:06 -05:00
c . Set ( "watch" , true )
2014-05-16 17:49:27 -04:00
}
2017-02-04 22:20:06 -05:00
if c . Cfg . GetBool ( "watch" ) {
2015-07-17 20:42:09 -04:00
serverWatch = true
2017-01-10 04:55:03 -05:00
c . watchConfig ( )
2015-07-17 20:42:09 -04:00
}
2017-11-02 03:25:20 -04:00
languages := c . languages ( )
serverPorts := make ( [ ] int , 1 )
if languages . IsMultihost ( ) {
serverPorts = make ( [ ] int , len ( languages ) )
}
currentServerPort := serverPort
for i := 0 ; i < len ( serverPorts ) ; i ++ {
l , err := net . Listen ( "tcp" , net . JoinHostPort ( serverInterface , strconv . Itoa ( currentServerPort ) ) )
if err == nil {
l . Close ( )
serverPorts [ i ] = currentServerPort
} else {
if i == 0 && serverCmd . Flags ( ) . Changed ( "port" ) {
// port set explicitly by user -- he/she probably meant it!
return newSystemErrorF ( "Server startup failed: %s" , err )
}
jww . ERROR . Println ( "port" , serverPort , "already in use, attempting to use an available port" )
sp , err := helpers . FindAvailablePort ( )
if err != nil {
return newSystemError ( "Unable to find alternative port to use:" , err )
}
serverPorts [ i ] = sp . Port
2014-05-15 15:07:46 -04:00
}
2017-11-02 03:25:20 -04:00
currentServerPort = serverPorts [ i ] + 1
2014-05-15 15:07:46 -04:00
}
2017-02-04 22:20:06 -05:00
c . Set ( "port" , serverPort )
2017-09-23 11:08:48 -04:00
if liveReloadPort != - 1 {
c . Set ( "liveReloadPort" , liveReloadPort )
} else {
2017-11-02 03:25:20 -04:00
c . Set ( "liveReloadPort" , serverPorts [ 0 ] )
2017-09-23 11:08:48 -04:00
}
2014-05-28 19:01:24 -04:00
2017-11-02 03:25:20 -04:00
if languages . IsMultihost ( ) {
for i , language := range languages {
baseURL , err = fixURL ( language , baseURL , serverPorts [ i ] )
if err != nil {
return err
}
language . Set ( "baseURL" , baseURL )
}
} else {
baseURL , err = fixURL ( c . Cfg , baseURL , serverPorts [ 0 ] )
if err != nil {
return err
}
2017-11-12 04:03:56 -05:00
c . Set ( "baseURL" , baseURL )
2014-01-29 17:50:31 -05:00
}
2013-11-22 00:28:05 -05:00
2014-09-22 09:45:05 -04:00
if err := memStats ( ) ; err != nil {
jww . ERROR . Println ( "memstats error:" , err )
}
2015-11-16 21:55:18 -05:00
// If a Destination is provided via flag write to disk
2016-02-05 16:58:17 -05:00
if destination != "" {
2015-11-16 21:55:18 -05:00
renderToDisk = true
}
// Hugo writes the output to memory instead of the disk
if ! renderToDisk {
2017-01-10 04:55:03 -05:00
cfg . Fs . Destination = new ( afero . MemMapFs )
2015-11-16 21:55:18 -05:00
// Rendering to memoryFS, publish to Root regardless of publishDir.
2017-02-04 22:20:06 -05:00
c . Set ( "publishDir" , "/" )
2015-11-16 21:55:18 -05:00
}
2017-01-10 04:55:03 -05:00
if err := c . build ( serverWatch ) ; err != nil {
2015-12-02 05:42:53 -05:00
return err
}
2013-10-09 18:24:40 -04:00
2017-04-04 12:05:19 -04:00
for _ , s := range Hugo . Sites {
s . RegisterMediaTypes ( )
}
2013-09-29 02:09:03 -04:00
// Watch runs its own server as part of the routine
if serverWatch {
2017-11-12 04:03:56 -05:00
watchDirs , err := c . getDirList ( )
if err != nil {
return err
}
2017-02-04 22:20:06 -05:00
baseWatchDir := c . Cfg . GetString ( "workingDir" )
2017-11-12 04:03:56 -05:00
relWatchDirs := make ( [ ] string , len ( watchDirs ) )
2015-11-23 10:32:06 -05:00
for i , dir := range watchDirs {
2017-11-12 04:03:56 -05:00
relWatchDirs [ i ] , _ = helpers . GetRelativePath ( dir , baseWatchDir )
2015-04-02 00:40:29 -04:00
}
2017-11-12 04:03:56 -05:00
rootWatchDirs := strings . Join ( helpers . UniqueStrings ( helpers . ExtractRootPaths ( relWatchDirs ) ) , "," )
2015-11-23 10:32:06 -05:00
2015-12-16 22:41:33 -05:00
jww . FEEDBACK . Printf ( "Watching for changes in %s%s{%s}\n" , baseWatchDir , helpers . FilePathSeparator , rootWatchDirs )
2017-11-12 04:03:56 -05:00
err = c . newWatcher ( true , watchDirs ... )
2015-12-02 05:42:53 -05:00
2013-09-29 02:09:03 -04:00
if err != nil {
2015-12-02 05:42:53 -05:00
return err
2013-09-29 02:09:03 -04:00
}
}
2015-12-02 05:42:53 -05:00
return nil
2013-09-29 02:09:03 -04:00
}
2017-11-02 03:25:20 -04:00
type fileServer struct {
2017-11-12 04:03:56 -05:00
ports [ ] int
2017-11-02 03:25:20 -04:00
baseURLs [ ] string
roots [ ] string
c * commandeer
}
func ( f * fileServer ) createEndpoint ( i int ) ( * http . ServeMux , string , error ) {
baseURL := f . baseURLs [ i ]
root := f . roots [ i ]
2017-11-12 04:03:56 -05:00
port := f . ports [ i ]
2017-11-02 03:25:20 -04:00
publishDir := f . c . Cfg . GetString ( "publishDir" )
if root != "" {
publishDir = filepath . Join ( publishDir , root )
}
absPublishDir := f . c . PathSpec ( ) . AbsPathify ( publishDir )
2017-11-12 04:03:56 -05:00
if i == 0 {
if renderToDisk {
jww . FEEDBACK . Println ( "Serving pages from " + absPublishDir )
} else {
jww . FEEDBACK . Println ( "Serving pages from memory" )
}
2015-11-16 21:55:18 -05:00
}
2014-01-26 04:48:00 -05:00
2017-11-02 03:25:20 -04:00
httpFs := afero . NewHttpFs ( f . c . Fs . Destination )
fs := filesOnlyFs { httpFs . Dir ( absPublishDir ) }
Only re-render the view(s) you're working on
Hugo already, in its server mode, support partial rebuilds. To put it simply: If you change `about.md`, only that content page is read and processed, then Hugo does some processing (taxonomies etc.) and the full site is rendered.
This commit covers the rendering part: We now only re-render the pages you work on, i.e. the last n pages you watched in the browser (which obviously also includes the page in the example above).
To be more specific: When you are running the hugo server in watch (aka. livereload) mode, and change a template or a content file, then we do a partial re-rendering of the following:
* The current content page (if it is a content change)
* The home page
* Up to the last 10 pages you visited on the site.
This should in most cases be enough, but if you navigate to something completely different, you may see stale content. Doing an edit will then refresh that page.
Note that this feature is enabled by default. To turn it off, run `hugo server --disableFastRender`.
Fixes #3962
See #1643
2017-10-14 07:40:43 -04:00
2017-11-02 03:25:20 -04:00
doLiveReload := ! buildWatch && ! f . c . Cfg . GetBool ( "disableLiveReload" )
fastRenderMode := doLiveReload && ! f . c . Cfg . GetBool ( "disableFastRender" )
Only re-render the view(s) you're working on
Hugo already, in its server mode, support partial rebuilds. To put it simply: If you change `about.md`, only that content page is read and processed, then Hugo does some processing (taxonomies etc.) and the full site is rendered.
This commit covers the rendering part: We now only re-render the pages you work on, i.e. the last n pages you watched in the browser (which obviously also includes the page in the example above).
To be more specific: When you are running the hugo server in watch (aka. livereload) mode, and change a template or a content file, then we do a partial re-rendering of the following:
* The current content page (if it is a content change)
* The home page
* Up to the last 10 pages you visited on the site.
This should in most cases be enough, but if you navigate to something completely different, you may see stale content. Doing an edit will then refresh that page.
Note that this feature is enabled by default. To turn it off, run `hugo server --disableFastRender`.
Fixes #3962
See #1643
2017-10-14 07:40:43 -04:00
2017-11-12 04:03:56 -05:00
if i == 0 && fastRenderMode {
Only re-render the view(s) you're working on
Hugo already, in its server mode, support partial rebuilds. To put it simply: If you change `about.md`, only that content page is read and processed, then Hugo does some processing (taxonomies etc.) and the full site is rendered.
This commit covers the rendering part: We now only re-render the pages you work on, i.e. the last n pages you watched in the browser (which obviously also includes the page in the example above).
To be more specific: When you are running the hugo server in watch (aka. livereload) mode, and change a template or a content file, then we do a partial re-rendering of the following:
* The current content page (if it is a content change)
* The home page
* Up to the last 10 pages you visited on the site.
This should in most cases be enough, but if you navigate to something completely different, you may see stale content. Doing an edit will then refresh that page.
Note that this feature is enabled by default. To turn it off, run `hugo server --disableFastRender`.
Fixes #3962
See #1643
2017-10-14 07:40:43 -04:00
jww . FEEDBACK . Println ( "Running in Fast Render Mode. For full rebuilds on change: hugo server --disableFastRender" )
}
2017-11-02 03:25:20 -04:00
// We're only interested in the path
u , err := url . Parse ( baseURL )
if err != nil {
return nil , "" , fmt . Errorf ( "Invalid baseURL: %s" , err )
}
2017-09-22 11:13:21 -04:00
decorate := func ( h http . Handler ) http . Handler {
return http . HandlerFunc ( func ( w http . ResponseWriter , r * http . Request ) {
2017-09-22 11:23:15 -04:00
if noHTTPCache {
2017-09-22 14:05:19 -04:00
w . Header ( ) . Set ( "Cache-Control" , "no-store, no-cache, must-revalidate, max-age=0" )
2017-09-22 11:13:21 -04:00
w . Header ( ) . Set ( "Pragma" , "no-cache" )
}
Only re-render the view(s) you're working on
Hugo already, in its server mode, support partial rebuilds. To put it simply: If you change `about.md`, only that content page is read and processed, then Hugo does some processing (taxonomies etc.) and the full site is rendered.
This commit covers the rendering part: We now only re-render the pages you work on, i.e. the last n pages you watched in the browser (which obviously also includes the page in the example above).
To be more specific: When you are running the hugo server in watch (aka. livereload) mode, and change a template or a content file, then we do a partial re-rendering of the following:
* The current content page (if it is a content change)
* The home page
* Up to the last 10 pages you visited on the site.
This should in most cases be enough, but if you navigate to something completely different, you may see stale content. Doing an edit will then refresh that page.
Note that this feature is enabled by default. To turn it off, run `hugo server --disableFastRender`.
Fixes #3962
See #1643
2017-10-14 07:40:43 -04:00
if fastRenderMode {
2017-10-19 04:09:04 -04:00
p := r . RequestURI
Only re-render the view(s) you're working on
Hugo already, in its server mode, support partial rebuilds. To put it simply: If you change `about.md`, only that content page is read and processed, then Hugo does some processing (taxonomies etc.) and the full site is rendered.
This commit covers the rendering part: We now only re-render the pages you work on, i.e. the last n pages you watched in the browser (which obviously also includes the page in the example above).
To be more specific: When you are running the hugo server in watch (aka. livereload) mode, and change a template or a content file, then we do a partial re-rendering of the following:
* The current content page (if it is a content change)
* The home page
* Up to the last 10 pages you visited on the site.
This should in most cases be enough, but if you navigate to something completely different, you may see stale content. Doing an edit will then refresh that page.
Note that this feature is enabled by default. To turn it off, run `hugo server --disableFastRender`.
Fixes #3962
See #1643
2017-10-14 07:40:43 -04:00
if strings . HasSuffix ( p , "/" ) || strings . HasSuffix ( p , "html" ) || strings . HasSuffix ( p , "htm" ) {
2017-11-02 03:25:20 -04:00
f . c . visitedURLs . Add ( p )
Only re-render the view(s) you're working on
Hugo already, in its server mode, support partial rebuilds. To put it simply: If you change `about.md`, only that content page is read and processed, then Hugo does some processing (taxonomies etc.) and the full site is rendered.
This commit covers the rendering part: We now only re-render the pages you work on, i.e. the last n pages you watched in the browser (which obviously also includes the page in the example above).
To be more specific: When you are running the hugo server in watch (aka. livereload) mode, and change a template or a content file, then we do a partial re-rendering of the following:
* The current content page (if it is a content change)
* The home page
* Up to the last 10 pages you visited on the site.
This should in most cases be enough, but if you navigate to something completely different, you may see stale content. Doing an edit will then refresh that page.
Note that this feature is enabled by default. To turn it off, run `hugo server --disableFastRender`.
Fixes #3962
See #1643
2017-10-14 07:40:43 -04:00
}
}
2017-09-22 11:13:21 -04:00
h . ServeHTTP ( w , r )
} )
}
fileserver := decorate ( http . FileServer ( fs ) )
2017-11-02 03:25:20 -04:00
mu := http . NewServeMux ( )
2014-08-22 07:59:59 -04:00
if u . Path == "" || u . Path == "/" {
2017-11-02 03:25:20 -04:00
mu . Handle ( "/" , fileserver )
2014-08-22 07:59:59 -04:00
} else {
2017-11-02 03:25:20 -04:00
mu . Handle ( u . Path , http . StripPrefix ( u . Path , fileserver ) )
2014-08-22 07:59:59 -04:00
}
2015-05-01 22:28:21 -04:00
endpoint := net . JoinHostPort ( serverInterface , strconv . Itoa ( port ) )
2017-11-02 03:25:20 -04:00
return mu , endpoint , nil
}
2017-11-12 04:03:56 -05:00
func ( c * commandeer ) serve ( ) {
2017-11-02 03:25:20 -04:00
isMultiHost := Hugo . IsMultihost ( )
var (
baseURLs [ ] string
roots [ ] string
2017-11-12 04:03:56 -05:00
ports [ ] int
2017-11-02 03:25:20 -04:00
)
if isMultiHost {
for _ , s := range Hugo . Sites {
baseURLs = append ( baseURLs , s . BaseURL . String ( ) )
roots = append ( roots , s . Language . Lang )
2017-11-12 04:03:56 -05:00
ports = append ( ports , s . Info . ServerPort ( ) )
2017-11-02 03:25:20 -04:00
}
} else {
2017-11-12 04:03:56 -05:00
s := Hugo . Sites [ 0 ]
baseURLs = [ ] string { s . BaseURL . String ( ) }
2017-11-02 03:25:20 -04:00
roots = [ ] string { "" }
2017-11-12 04:03:56 -05:00
ports = append ( ports , s . Info . ServerPort ( ) )
2014-01-26 04:48:00 -05:00
}
2017-11-02 03:25:20 -04:00
srv := & fileServer {
2017-11-12 04:03:56 -05:00
ports : ports ,
2017-11-02 03:25:20 -04:00
baseURLs : baseURLs ,
roots : roots ,
c : c ,
}
2017-11-12 04:03:56 -05:00
doLiveReload := ! c . Cfg . GetBool ( "disableLiveReload" )
if doLiveReload {
livereload . Initialize ( )
}
2017-11-02 03:25:20 -04:00
for i , _ := range baseURLs {
mu , endpoint , err := srv . createEndpoint ( i )
2017-11-12 04:03:56 -05:00
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 )
2017-11-02 03:25:20 -04:00
go func ( ) {
err = http . ListenAndServe ( endpoint , mu )
if err != nil {
jww . ERROR . Printf ( "Error: %s\n" , err . Error ( ) )
os . Exit ( 1 )
}
} ( )
}
jww . FEEDBACK . Println ( "Press Ctrl+C to stop" )
2013-09-29 02:09:03 -04:00
}
2014-08-22 07:59:59 -04:00
2016-10-24 14:56:00 -04:00
// fixURL massages the baseURL into a form needed for serving
2015-01-15 20:02:19 -05:00
// all pages correctly.
2017-11-02 03:25:20 -04:00
func fixURL ( cfg config . Provider , s string , port int ) ( string , error ) {
2014-08-22 07:59:59 -04:00
useLocalhost := false
if s == "" {
2017-02-04 22:20:06 -05:00
s = cfg . GetString ( "baseURL" )
2014-08-22 07:59:59 -04:00
useLocalhost = true
}
2016-05-12 19:06:56 -04:00
2015-01-22 19:46:29 -05:00
if ! strings . HasSuffix ( s , "/" ) {
2015-01-15 20:02:19 -05:00
s = s + "/"
}
2016-05-12 19:06:56 -04:00
// do an initial parse of the input string
2014-08-22 07:59:59 -04:00
u , err := url . Parse ( s )
if err != nil {
return "" , err
}
2016-05-12 19:06:56 -04:00
// if no Host is defined, then assume that no schema or double-slash were
// present in the url. Add a double-slash and make a best effort attempt.
if u . Host == "" && s != "/" {
s = "//" + s
u , err = url . Parse ( s )
if err != nil {
return "" , err
}
}
if useLocalhost {
if u . Scheme == "https" {
2015-01-01 10:32:56 -05:00
u . Scheme = "http"
2014-08-22 07:59:59 -04:00
}
2016-05-12 19:06:56 -04:00
u . Host = "localhost"
}
if serverAppend {
if strings . Contains ( u . Host , ":" ) {
u . Host , _ , err = net . SplitHostPort ( u . Host )
2014-08-22 07:59:59 -04:00
if err != nil {
2016-10-24 14:56:00 -04:00
return "" , fmt . Errorf ( "Failed to split baseURL hostpost: %s" , err )
2014-08-22 07:59:59 -04:00
}
}
2017-11-02 03:25:20 -04:00
u . Host += fmt . Sprintf ( ":%d" , port )
2014-08-22 07:59:59 -04:00
}
return u . String ( ) , nil
}
2014-09-22 09:45:05 -04:00
func memStats ( ) error {
memstats := serverCmd . Flags ( ) . Lookup ( "memstats" ) . Value . String ( )
if memstats != "" {
interval , err := time . ParseDuration ( serverCmd . Flags ( ) . Lookup ( "meminterval" ) . Value . String ( ) )
if err != nil {
interval , _ = time . ParseDuration ( "100ms" )
}
fileMemStats , err := os . Create ( memstats )
if err != nil {
return err
}
fileMemStats . WriteString ( "# Time\tHeapSys\tHeapAlloc\tHeapIdle\tHeapReleased\n" )
go func ( ) {
var stats runtime . MemStats
start := time . Now ( ) . UnixNano ( )
for {
runtime . ReadMemStats ( & stats )
if fileMemStats != nil {
fileMemStats . WriteString ( fmt . Sprintf ( "%d\t%d\t%d\t%d\t%d\n" ,
( time . Now ( ) . UnixNano ( ) - start ) / 1000000 , stats . HeapSys , stats . HeapAlloc , stats . HeapIdle , stats . HeapReleased ) )
time . Sleep ( interval )
} else {
break
}
}
} ( )
}
return nil
}