2013-09-29 06:09:03 +00:00
|
|
|
// Copyright © 2013 Steve Francia <spf@spf13.com>.
|
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"net/http"
|
2014-01-26 09:48:00 +00:00
|
|
|
"os"
|
2013-09-29 06:09:03 +00:00
|
|
|
"strconv"
|
2013-11-22 05:28:05 +00:00
|
|
|
"strings"
|
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
|
2013-09-29 06:09:03 +00:00
|
|
|
|
|
|
|
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")
|
2013-12-28 19:37:44 +00:00
|
|
|
serverCmd.Flags().BoolVarP(&serverAppend, "append-port", "", true, "append port to baseurl")
|
2013-09-29 06:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var serverCmd = &cobra.Command{
|
|
|
|
Use: "server",
|
|
|
|
Short: "Hugo runs it's own a webserver to render the files",
|
|
|
|
Long: `Hugo is able to run it's own high performance web server.
|
|
|
|
Hugo will render all the files defined in the source directory and
|
|
|
|
Serve them up.`,
|
|
|
|
Run: server,
|
|
|
|
}
|
|
|
|
|
|
|
|
func server(cmd *cobra.Command, args []string) {
|
|
|
|
InitializeConfig()
|
|
|
|
|
2013-10-04 20:03:13 +00:00
|
|
|
if BaseUrl == "" {
|
2013-11-22 05:28:05 +00:00
|
|
|
BaseUrl = "http://localhost"
|
2013-09-29 06:09:03 +00:00
|
|
|
}
|
|
|
|
|
2013-11-22 05:28:05 +00:00
|
|
|
if !strings.HasPrefix(BaseUrl, "http://") {
|
|
|
|
BaseUrl = "http://" + BaseUrl
|
|
|
|
}
|
|
|
|
|
2014-01-29 22:50:31 +00:00
|
|
|
if serverAppend {
|
|
|
|
Config.BaseUrl = strings.TrimSuffix(BaseUrl, "/") + ":" + strconv.Itoa(serverPort)
|
|
|
|
} else {
|
|
|
|
Config.BaseUrl = strings.TrimSuffix(BaseUrl, "/")
|
|
|
|
}
|
2013-11-22 05:28:05 +00:00
|
|
|
|
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 {
|
|
|
|
fmt.Println("Watching for changes in", Config.GetAbsPath(Config.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) {
|
|
|
|
if Verbose {
|
|
|
|
fmt.Println("Serving pages from " + Config.GetAbsPath(Config.PublishDir))
|
|
|
|
}
|
|
|
|
|
2013-12-16 08:37:09 +00:00
|
|
|
if BaseUrl == "" {
|
|
|
|
fmt.Printf("Web Server is available at %s\n", Config.BaseUrl)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Web Server is available at http://localhost:%v\n", port)
|
|
|
|
}
|
|
|
|
|
2013-09-29 06:09:03 +00:00
|
|
|
fmt.Println("Press ctrl+c to stop")
|
2014-01-26 09:48:00 +00:00
|
|
|
|
|
|
|
err := http.ListenAndServe(":"+strconv.Itoa(port), http.FileServer(http.Dir(Config.GetAbsPath(Config.PublishDir))))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2013-09-29 06:09:03 +00:00
|
|
|
}
|