2013-09-29 02:09:03 -04: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 (
|
2014-05-16 11:48:59 -04:00
|
|
|
"bytes"
|
2013-09-29 02:09:03 -04:00
|
|
|
"fmt"
|
2014-05-15 15:07:46 -04:00
|
|
|
"net"
|
2013-09-29 02:09:03 -04:00
|
|
|
"net/http"
|
2014-01-26 04:48:00 -05:00
|
|
|
"os"
|
2013-09-29 02:09:03 -04:00
|
|
|
"strconv"
|
2013-11-22 00:28:05 -05:00
|
|
|
"strings"
|
2014-03-31 13:23:34 -04:00
|
|
|
|
2014-05-16 11:48:59 -04:00
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
//"code.google.com/p/go.net/websocket"
|
|
|
|
|
2014-03-31 13:23:34 -04:00
|
|
|
"github.com/spf13/cobra"
|
2014-04-05 01:26:43 -04:00
|
|
|
"github.com/spf13/hugo/helpers"
|
2014-03-31 13:23:34 -04:00
|
|
|
jww "github.com/spf13/jwalterweatherman"
|
2014-04-05 01:26:43 -04:00
|
|
|
"github.com/spf13/viper"
|
2013-09-29 02:09:03 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var serverPort int
|
|
|
|
var serverWatch bool
|
2013-12-28 14:37:44 -05:00
|
|
|
var serverAppend bool
|
2013-09-29 02:09:03 -04: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 14:37:44 -05:00
|
|
|
serverCmd.Flags().BoolVarP(&serverAppend, "append-port", "", true, "append port to baseurl")
|
2013-09-29 02:09:03 -04: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 16:03:13 -04:00
|
|
|
if BaseUrl == "" {
|
2013-11-22 00:28:05 -05:00
|
|
|
BaseUrl = "http://localhost"
|
2013-09-29 02:09:03 -04:00
|
|
|
}
|
|
|
|
|
2013-11-22 00:28:05 -05:00
|
|
|
if !strings.HasPrefix(BaseUrl, "http://") {
|
|
|
|
BaseUrl = "http://" + BaseUrl
|
|
|
|
}
|
|
|
|
|
2014-05-15 15:07:46 -04: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-01-29 17:50:31 -05:00
|
|
|
if serverAppend {
|
2014-04-05 01:26:43 -04:00
|
|
|
viper.Set("BaseUrl", strings.TrimSuffix(BaseUrl, "/")+":"+strconv.Itoa(serverPort))
|
2014-01-29 17:50:31 -05:00
|
|
|
} else {
|
2014-04-05 01:26:43 -04:00
|
|
|
viper.Set("BaseUrl", strings.TrimSuffix(BaseUrl, "/"))
|
2014-01-29 17:50:31 -05:00
|
|
|
}
|
2013-11-22 00:28:05 -05:00
|
|
|
|
2013-10-25 18:03:14 -04:00
|
|
|
build(serverWatch)
|
2013-10-09 18:24:40 -04:00
|
|
|
|
2013-09-29 02:09:03 -04:00
|
|
|
// Watch runs its own server as part of the routine
|
|
|
|
if serverWatch {
|
2014-04-05 01:26:43 -04:00
|
|
|
jww.FEEDBACK.Println("Watching for changes in", helpers.AbsPathify(viper.GetString("ContentDir")))
|
2013-09-30 22:38:32 -04:00
|
|
|
err := NewWatcher(serverPort)
|
2013-09-29 02:09:03 -04:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
serve(serverPort)
|
|
|
|
}
|
|
|
|
|
|
|
|
func serve(port int) {
|
2014-04-05 01:26:43 -04:00
|
|
|
jww.FEEDBACK.Println("Serving pages from " + helpers.AbsPathify(viper.GetString("PublishDir")))
|
2013-09-29 02:09:03 -04:00
|
|
|
|
2014-05-08 21:23:26 -04:00
|
|
|
jww.FEEDBACK.Printf("Web Server is available at %s\n", viper.GetString("BaseUrl"))
|
2013-12-16 03:37:09 -05:00
|
|
|
|
2013-09-29 02:09:03 -04:00
|
|
|
fmt.Println("Press ctrl+c to stop")
|
2014-01-26 04:48:00 -05:00
|
|
|
|
2014-05-16 11:48:59 -04:00
|
|
|
http.Handle("/", http.FileServer(http.Dir(helpers.AbsPathify(viper.GetString("PublishDir")))))
|
|
|
|
go wsHub.run()
|
|
|
|
http.HandleFunc("/livereload", wsHandler)
|
|
|
|
|
|
|
|
err := http.ListenAndServe(":"+strconv.Itoa(port), nil)
|
2014-01-26 04:48:00 -05:00
|
|
|
if err != nil {
|
2014-03-31 13:23:34 -04:00
|
|
|
jww.ERROR.Printf("Error: %s\n", err.Error())
|
2014-01-26 04:48:00 -05:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2013-09-29 02:09:03 -04:00
|
|
|
}
|
2014-05-16 11:48:59 -04:00
|
|
|
|
|
|
|
type hub struct {
|
|
|
|
// Registered connections.
|
|
|
|
connections map[*connection]bool
|
|
|
|
|
|
|
|
// Inbound messages from the connections.
|
|
|
|
broadcast chan []byte
|
|
|
|
|
|
|
|
// Register requests from the connections.
|
|
|
|
register chan *connection
|
|
|
|
|
|
|
|
// Unregister requests from connections.
|
|
|
|
unregister chan *connection
|
|
|
|
}
|
|
|
|
|
|
|
|
var wsHub = hub{
|
|
|
|
broadcast: make(chan []byte),
|
|
|
|
register: make(chan *connection),
|
|
|
|
unregister: make(chan *connection),
|
|
|
|
connections: make(map[*connection]bool),
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *hub) run() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case c := <-h.register:
|
|
|
|
h.connections[c] = true
|
|
|
|
case c := <-h.unregister:
|
|
|
|
delete(h.connections, c)
|
|
|
|
close(c.send)
|
|
|
|
case m := <-h.broadcast:
|
|
|
|
for c := range h.connections {
|
|
|
|
select {
|
|
|
|
case c.send <- m:
|
|
|
|
default:
|
|
|
|
delete(h.connections, c)
|
|
|
|
close(c.send)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type connection struct {
|
|
|
|
// The websocket connection.
|
|
|
|
ws *websocket.Conn
|
|
|
|
|
|
|
|
// Buffered channel of outbound messages.
|
|
|
|
send chan []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *connection) reader() {
|
|
|
|
for {
|
|
|
|
_, message, err := c.ws.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
fmt.Println(string(message))
|
|
|
|
switch true {
|
|
|
|
case bytes.Contains(message, []byte(`"command":"hello"`)):
|
|
|
|
wsHub.broadcast <- []byte(`{
|
|
|
|
"command": "hello",
|
|
|
|
"protocols": [ "http://livereload.com/protocols/official-7" ],
|
|
|
|
"serverName": "Hugo"
|
|
|
|
}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.ws.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *connection) writer() {
|
|
|
|
for message := range c.send {
|
|
|
|
err := c.ws.WriteMessage(websocket.TextMessage, message)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.ws.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
var upgrader = &websocket.Upgrader{ReadBufferSize: 1024, WriteBufferSize: 1024}
|
|
|
|
|
|
|
|
func wsHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ws, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c := &connection{send: make(chan []byte, 256), ws: ws}
|
|
|
|
wsHub.register <- c
|
|
|
|
defer func() { wsHub.unregister <- c }()
|
|
|
|
go c.writer()
|
|
|
|
c.reader()
|
|
|
|
}
|