2014-09-22 13:45:05 +00:00
|
|
|
// Copyright © 2013-14 Steve Francia <spf@spf13.com>.
|
2013-09-29 06:09:03 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Simple Public License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://opensource.org/licenses/Simple-2.0
|
|
|
|
//
|
|
|
|
// 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 19:07:46 +00:00
|
|
|
"net"
|
2013-09-29 06:09:03 +00:00
|
|
|
"net/http"
|
2014-08-22 11:59:59 +00:00
|
|
|
"net/url"
|
2014-01-26 09:48:00 +00:00
|
|
|
"os"
|
2014-09-22 13:45:05 +00:00
|
|
|
"runtime"
|
2013-09-29 06:09:03 +00:00
|
|
|
"strconv"
|
2013-11-22 05:28:05 +00:00
|
|
|
"strings"
|
2014-09-22 13:45:05 +00:00
|
|
|
"time"
|
2014-03-31 17:23:34 +00:00
|
|
|
|
2014-11-01 15:57:29 +00:00
|
|
|
"github.com/spf13/afero"
|
2014-03-31 17:23:34 +00:00
|
|
|
"github.com/spf13/cobra"
|
2014-04-05 05:26:43 +00:00
|
|
|
"github.com/spf13/hugo/helpers"
|
2014-11-01 15:57:29 +00:00
|
|
|
"github.com/spf13/hugo/hugofs"
|
2014-03-31 17:23:34 +00:00
|
|
|
jww "github.com/spf13/jwalterweatherman"
|
2014-04-05 05:26:43 +00:00
|
|
|
"github.com/spf13/viper"
|
2013-09-29 06:09:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var serverPort int
|
|
|
|
var serverWatch bool
|
2013-12-28 19:37:44 +00:00
|
|
|
var serverAppend bool
|
2014-05-16 21:49:27 +00:00
|
|
|
var disableLiveReload bool
|
2013-09-29 06:09:03 +00:00
|
|
|
|
2014-05-16 21:49:27 +00:00
|
|
|
//var serverCmdV *cobra.Command
|
2013-09-29 06:09:03 +00:00
|
|
|
|
|
|
|
var serverCmd = &cobra.Command{
|
|
|
|
Use: "server",
|
2014-07-27 06:43:21 +00:00
|
|
|
Short: "Hugo runs its own webserver to render the files",
|
|
|
|
Long: `Hugo is able to run its own high performance web server.
|
2013-09-29 06:09:03 +00:00
|
|
|
Hugo will render all the files defined in the source directory and
|
2014-12-24 11:20:49 +00:00
|
|
|
serve them up.`,
|
2014-05-16 21:49:27 +00:00
|
|
|
//Run: server,
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
serverCmd.Flags().IntVarP(&serverPort, "port", "p", 1313, "port to run the server on")
|
|
|
|
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")
|
2014-09-22 13:45:05 +00:00
|
|
|
serverCmd.Flags().String("memstats", "", "log memory usage to this file")
|
|
|
|
serverCmd.Flags().Int("meminterval", 100, "interval to poll memory usage (requires --memstats)")
|
2014-05-16 21:49:27 +00:00
|
|
|
serverCmd.Run = server
|
2013-09-29 06:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func server(cmd *cobra.Command, args []string) {
|
|
|
|
InitializeConfig()
|
|
|
|
|
2014-05-16 21:49:27 +00:00
|
|
|
if cmd.Flags().Lookup("disableLiveReload").Changed {
|
|
|
|
viper.Set("DisableLiveReload", disableLiveReload)
|
|
|
|
}
|
|
|
|
|
|
|
|
if serverWatch {
|
|
|
|
viper.Set("Watch", true)
|
|
|
|
}
|
|
|
|
|
2014-05-15 19:07:46 +00:00
|
|
|
l, err := net.Listen("tcp", ":"+strconv.Itoa(serverPort))
|
|
|
|
if err == nil {
|
|
|
|
l.Close()
|
|
|
|
} else {
|
|
|
|
jww.ERROR.Println("port", serverPort, "already in use, attempting to use an available port")
|
|
|
|
sp, err := helpers.FindAvailablePort()
|
|
|
|
if err != nil {
|
|
|
|
jww.ERROR.Println("Unable to find alternative port to use")
|
|
|
|
jww.ERROR.Fatalln(err)
|
|
|
|
}
|
|
|
|
serverPort = sp.Port
|
|
|
|
}
|
|
|
|
|
2014-05-28 23:01:24 +00:00
|
|
|
viper.Set("port", serverPort)
|
|
|
|
|
2015-03-11 17:34:57 +00:00
|
|
|
BaseURL, err := fixURL(BaseURL)
|
2014-08-22 11:59:59 +00:00
|
|
|
if err != nil {
|
|
|
|
jww.ERROR.Fatal(err)
|
2014-01-29 22:50:31 +00:00
|
|
|
}
|
2015-03-11 17:34:57 +00:00
|
|
|
viper.Set("BaseURL", BaseURL)
|
2013-11-22 05:28:05 +00:00
|
|
|
|
2014-09-22 13:45:05 +00:00
|
|
|
if err := memStats(); err != nil {
|
|
|
|
jww.ERROR.Println("memstats error:", err)
|
|
|
|
}
|
|
|
|
|
2013-10-25 22:03:14 +00:00
|
|
|
build(serverWatch)
|
2013-10-09 22:24:40 +00:00
|
|
|
|
2013-09-29 06:09:03 +00:00
|
|
|
// Watch runs its own server as part of the routine
|
|
|
|
if serverWatch {
|
2014-04-05 05:26:43 +00:00
|
|
|
jww.FEEDBACK.Println("Watching for changes in", helpers.AbsPathify(viper.GetString("ContentDir")))
|
2013-10-01 02:38:32 +00:00
|
|
|
err := NewWatcher(serverPort)
|
2013-09-29 06:09:03 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
serve(serverPort)
|
|
|
|
}
|
|
|
|
|
|
|
|
func serve(port int) {
|
2014-04-05 05:26:43 +00:00
|
|
|
jww.FEEDBACK.Println("Serving pages from " + helpers.AbsPathify(viper.GetString("PublishDir")))
|
2014-01-26 09:48:00 +00:00
|
|
|
|
2014-11-01 15:57:29 +00:00
|
|
|
httpFs := &afero.HttpFs{SourceFs: hugofs.DestinationFS}
|
|
|
|
fileserver := http.FileServer(httpFs.Dir(helpers.AbsPathify(viper.GetString("PublishDir"))))
|
2014-08-22 11:59:59 +00:00
|
|
|
|
2015-03-11 17:34:57 +00:00
|
|
|
u, err := url.Parse(viper.GetString("BaseURL"))
|
2014-08-22 11:59:59 +00:00
|
|
|
if err != nil {
|
2015-03-11 17:34:57 +00:00
|
|
|
jww.ERROR.Fatalf("Invalid BaseURL: %s", err)
|
2014-08-22 11:59:59 +00:00
|
|
|
}
|
|
|
|
if u.Path == "" || u.Path == "/" {
|
|
|
|
http.Handle("/", fileserver)
|
|
|
|
} else {
|
2014-10-19 12:41:02 +00:00
|
|
|
http.Handle(u.Path, http.StripPrefix(u.Path, fileserver))
|
2014-08-22 11:59:59 +00:00
|
|
|
}
|
|
|
|
|
2014-10-17 16:32:16 +00:00
|
|
|
u.Scheme = "http"
|
|
|
|
jww.FEEDBACK.Printf("Web Server is available at %s\n", u.String())
|
2015-01-29 21:19:12 +00:00
|
|
|
fmt.Println("Press Ctrl+C to stop")
|
2014-10-17 16:32:16 +00:00
|
|
|
|
2014-08-22 11:59:59 +00:00
|
|
|
err = http.ListenAndServe(":"+strconv.Itoa(port), nil)
|
2014-01-26 09:48:00 +00:00
|
|
|
if err != nil {
|
2014-03-31 17:23:34 +00:00
|
|
|
jww.ERROR.Printf("Error: %s\n", err.Error())
|
2014-01-26 09:48:00 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2013-09-29 06:09:03 +00:00
|
|
|
}
|
2014-08-22 11:59:59 +00:00
|
|
|
|
2015-03-18 05:16:54 +00:00
|
|
|
// fixURL massages the BaseURL into a form needed for serving
|
2015-01-16 01:02:19 +00:00
|
|
|
// all pages correctly.
|
2015-03-11 17:34:57 +00:00
|
|
|
func fixURL(s string) (string, error) {
|
2014-08-22 11:59:59 +00:00
|
|
|
useLocalhost := false
|
|
|
|
if s == "" {
|
2015-03-11 17:34:57 +00:00
|
|
|
s = viper.GetString("BaseURL")
|
2014-08-22 11:59:59 +00:00
|
|
|
useLocalhost = true
|
|
|
|
}
|
2014-10-06 09:50:44 +00:00
|
|
|
if !strings.HasPrefix(s, "http://") && !strings.HasPrefix(s, "https://") {
|
2014-08-22 11:59:59 +00:00
|
|
|
s = "http://" + s
|
|
|
|
}
|
2015-01-23 00:46:29 +00:00
|
|
|
if !strings.HasSuffix(s, "/") {
|
2015-01-16 01:02:19 +00:00
|
|
|
s = s + "/"
|
|
|
|
}
|
2014-08-22 11:59:59 +00:00
|
|
|
u, err := url.Parse(s)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if serverAppend {
|
|
|
|
if useLocalhost {
|
|
|
|
u.Host = fmt.Sprintf("localhost:%d", serverPort)
|
2015-01-01 15:32:56 +00:00
|
|
|
u.Scheme = "http"
|
2014-08-22 11:59:59 +00:00
|
|
|
return u.String(), nil
|
|
|
|
}
|
|
|
|
host := u.Host
|
|
|
|
if strings.Contains(host, ":") {
|
|
|
|
host, _, err = net.SplitHostPort(u.Host)
|
|
|
|
if err != nil {
|
2015-03-18 05:16:54 +00:00
|
|
|
return "", fmt.Errorf("Failed to split BaseURL hostpost: %s", err)
|
2014-08-22 11:59:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
u.Host = fmt.Sprintf("%s:%d", host, serverPort)
|
|
|
|
return u.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if useLocalhost {
|
|
|
|
u.Host = "localhost"
|
|
|
|
}
|
|
|
|
return u.String(), nil
|
|
|
|
}
|
2014-09-22 13:45:05 +00: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
|
|
|
|
}
|