mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-28 06:30:33 +03:00
Proper integration of live reload with automatic injection
This commit is contained in:
parent
60ed5bda2b
commit
be1ee22032
8 changed files with 224 additions and 121 deletions
|
@ -14,7 +14,6 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
|
@ -22,9 +21,6 @@ import (
|
|||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
//"code.google.com/p/go.net/websocket"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/hugo/helpers"
|
||||
jww "github.com/spf13/jwalterweatherman"
|
||||
|
@ -34,12 +30,9 @@ import (
|
|||
var serverPort int
|
||||
var serverWatch bool
|
||||
var serverAppend bool
|
||||
var disableLiveReload bool
|
||||
|
||||
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, "append-port", "", true, "append port to baseurl")
|
||||
}
|
||||
//var serverCmdV *cobra.Command
|
||||
|
||||
var serverCmd = &cobra.Command{
|
||||
Use: "server",
|
||||
|
@ -47,7 +40,15 @@ var serverCmd = &cobra.Command{
|
|||
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,
|
||||
//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")
|
||||
serverCmd.Run = server
|
||||
}
|
||||
|
||||
func server(cmd *cobra.Command, args []string) {
|
||||
|
@ -57,6 +58,14 @@ func server(cmd *cobra.Command, args []string) {
|
|||
BaseUrl = "http://localhost"
|
||||
}
|
||||
|
||||
if cmd.Flags().Lookup("disableLiveReload").Changed {
|
||||
viper.Set("DisableLiveReload", disableLiveReload)
|
||||
}
|
||||
|
||||
if serverWatch {
|
||||
viper.Set("Watch", true)
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(BaseUrl, "http://") {
|
||||
BaseUrl = "http://" + BaseUrl
|
||||
}
|
||||
|
@ -96,111 +105,13 @@ func server(cmd *cobra.Command, args []string) {
|
|||
|
||||
func serve(port int) {
|
||||
jww.FEEDBACK.Println("Serving pages from " + helpers.AbsPathify(viper.GetString("PublishDir")))
|
||||
|
||||
jww.FEEDBACK.Printf("Web Server is available at %s\n", viper.GetString("BaseUrl"))
|
||||
|
||||
fmt.Println("Press ctrl+c to stop")
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
jww.ERROR.Printf("Error: %s\n", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue