mirror of
https://github.com/strongdm/comply
synced 2024-11-22 07:34:54 +00:00
Serve now provides an HTTP server, listening on port 4000 by default. Listen port can be set with the --port flag to the serve command. Websocket refresh remains intact.
This commit is contained in:
parent
0c9dbd736b
commit
ef32942dad
@ -7,8 +7,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var serveCommand = cli.Command{
|
var serveCommand = cli.Command{
|
||||||
Name: "serve",
|
Name: "serve",
|
||||||
Usage: "live updating version of the build command",
|
Usage: "live updating version of the build command",
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
cli.IntFlag{
|
||||||
|
Name: "port",
|
||||||
|
Value: 4000,
|
||||||
|
Destination: &render.ServePort,
|
||||||
|
},
|
||||||
|
},
|
||||||
Action: serveAction,
|
Action: serveAction,
|
||||||
Before: beforeAll(pandocMustExist, cleanContainers),
|
Before: beforeAll(pandocMustExist, cleanContainers),
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,10 @@ import (
|
|||||||
|
|
||||||
const websocketReloader = `<script>
|
const websocketReloader = `<script>
|
||||||
(function(){
|
(function(){
|
||||||
var ws = new WebSocket("ws://localhost:5122/ws")
|
var ws = new WebSocket("ws://localhost:%d/ws")
|
||||||
|
if (location.host != "") {
|
||||||
|
ws = new WebSocket("ws://"+location.host+"/ws")
|
||||||
|
}
|
||||||
var connected = false
|
var connected = false
|
||||||
ws.onopen = function(e) {
|
ws.onopen = function(e) {
|
||||||
connected = true
|
connected = true
|
||||||
@ -74,7 +77,7 @@ func html(output string, live bool, errCh chan error, wg *sync.WaitGroup) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if live {
|
if live {
|
||||||
w.Write([]byte(websocketReloader))
|
w.Write([]byte(fmt.Sprintf(websocketReloader, ServePort)))
|
||||||
}
|
}
|
||||||
w.Close()
|
w.Close()
|
||||||
}
|
}
|
||||||
@ -82,7 +85,7 @@ func html(output string, live bool, errCh chan error, wg *sync.WaitGroup) {
|
|||||||
if live {
|
if live {
|
||||||
if !opened {
|
if !opened {
|
||||||
opened = true
|
opened = true
|
||||||
open.Run(filepath.Join(".", "output", "index.html"))
|
open.Run(fmt.Sprintf("http://127.0.0.1:%d/", ServePort))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
wg.Done()
|
wg.Done()
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
package render
|
package render
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -11,6 +13,8 @@ import (
|
|||||||
"github.com/yosssi/ace"
|
"github.com/yosssi/ace"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ServePort int
|
||||||
|
|
||||||
var upgrader = websocket.Upgrader{
|
var upgrader = websocket.Upgrader{
|
||||||
ReadBufferSize: 1024,
|
ReadBufferSize: 1024,
|
||||||
WriteBufferSize: 1024,
|
WriteBufferSize: 1024,
|
||||||
@ -87,6 +91,16 @@ func Build(output string, live bool) error {
|
|||||||
|
|
||||||
if live {
|
if live {
|
||||||
watch(errCh)
|
watch(errCh)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
http.Handle("/", http.FileServer(http.Dir(filepath.Join(".", "output"))))
|
||||||
|
err := http.ListenAndServe(fmt.Sprintf("127.0.0.1:%d", ServePort), nil)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
fmt.Printf("Serving content of output/ at http://127.0.0.1:%d (ctrl-c to quit)\n", ServePort)
|
||||||
}
|
}
|
||||||
// PDF
|
// PDF
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
|
@ -45,7 +45,6 @@ func watch(errCh chan error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
http.HandleFunc("/ws", serveWs)
|
http.HandleFunc("/ws", serveWs)
|
||||||
go http.ListenAndServe("127.0.0.1:5122", nil)
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user