Skip to content

Commit

Permalink
Merge pull request #14 from spinup-host/vicky/gh-3
Browse files Browse the repository at this point in the history
gh-3: to support streaming logs using websocket
  • Loading branch information
viggy28 authored Jul 19, 2021
2 parents 78f7336 + ba56b92 commit ca09898
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 3 deletions.
174 changes: 174 additions & 0 deletions api/streamlogs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package api

import (
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"text/template"
"time"

"github.com/gorilla/websocket"
)

const (
// Time allowed to write the file to the client.
writeWait = 10 * time.Second

// Time allowed to read the next pong message from the client.
pongWait = 60 * time.Second

// Send pings to client with this period. Must be less than pongWait.
pingPeriod = (pongWait * 9) / 10

// Poll file for changes with this period.
filePeriod = 10 * time.Second
)

var (
homeTempl = template.Must(template.New("").Parse(homeHTML))
filename = "/var/lib/docker/containers/1967dededef67f90df186c7569ccfa5d7d6828447bd47c4c48657c837990943a/1967dededef67f90df186c7569ccfa5d7d6828447bd47c4c48657c837990943a-json.log"
upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
)

func readFileIfModified(lastMod time.Time) ([]byte, time.Time, error) {
fi, err := os.Stat(filename)
if err != nil {
return nil, lastMod, err
}
if !fi.ModTime().After(lastMod) {
return nil, lastMod, nil
}
p, err := ioutil.ReadFile(filename)
if err != nil {
return nil, fi.ModTime(), err
}
return p, fi.ModTime(), nil
}

func reader(ws *websocket.Conn) {
defer ws.Close()
ws.SetReadLimit(512)
ws.SetReadDeadline(time.Now().Add(pongWait))
ws.SetPongHandler(func(string) error { ws.SetReadDeadline(time.Now().Add(pongWait)); return nil })
for {
_, _, err := ws.ReadMessage()
if err != nil {
break
}
}
}

func writer(ws *websocket.Conn, lastMod time.Time) {
lastError := ""
pingTicker := time.NewTicker(pingPeriod)
fileTicker := time.NewTicker(filePeriod)
defer func() {
pingTicker.Stop()
fileTicker.Stop()
ws.Close()
}()
for {
select {
case <-fileTicker.C:
var p []byte
var err error

p, lastMod, err = readFileIfModified(lastMod)

if err != nil {
if s := err.Error(); s != lastError {
lastError = s
p = []byte(lastError)
}
} else {
lastError = ""
}

if p != nil {
ws.SetWriteDeadline(time.Now().Add(writeWait))
if err := ws.WriteMessage(websocket.TextMessage, p); err != nil {
return
}
}
case <-pingTicker.C:
ws.SetWriteDeadline(time.Now().Add(writeWait))
if err := ws.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
return
}
}
}
}

func StreamLogs(w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
if _, ok := err.(websocket.HandshakeError); !ok {
log.Println(err)
}
return
}

var lastMod time.Time
if n, err := strconv.ParseInt(r.FormValue("lastMod"), 16, 64); err == nil {
lastMod = time.Unix(0, n)
}

go writer(ws, lastMod)
reader(ws)
}

func Logs(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/logs" {
http.Error(w, "Not found", http.StatusNotFound)
return
}
if r.Method != "GET" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
p, lastMod, err := readFileIfModified(time.Time{})
if err != nil {
p = []byte(err.Error())
lastMod = time.Unix(0, 0)
}
var v = struct {
Host string
Data string
LastMod string
}{
r.Host,
string(p),
strconv.FormatInt(lastMod.UnixNano(), 16),
}
homeTempl.Execute(w, &v)
}

const homeHTML = `<!DOCTYPE html>
<html lang="en">
<head>
<title>WebSocket Example</title>
</head>
<body>
<pre id="fileData">{{.Data}}</pre>
<script type="text/javascript">
(function() {
var data = document.getElementById("fileData");
var conn = new WebSocket("wss://api.spinup.host/streamlogs?lastMod={{.LastMod}}");
conn.onclose = function(evt) {
data.textContent = 'Connection closed';
}
conn.onmessage = function(evt) {
console.log('file updated');
data.textContent = evt.data;
}
})();
</script>
</body>
</html>
`
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ require (
github.com/cloudflare/cloudflare-go v0.14.0
github.com/golang/gddo v0.0.0-20210115222349-20d68f94ee1f
github.com/google/go-cmp v0.5.1 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/rs/cors v1.8.0 // indirect
github.com/gorilla/websocket v1.4.2
github.com/rs/cors v1.8.0
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ func main() {
mux.HandleFunc("/hello", api.Hello)
mux.HandleFunc("/createservice", api.CreateService)
mux.HandleFunc("/githubAuth", api.GithubAuth)
mux.HandleFunc("/logs", api.Logs)
mux.HandleFunc("/streamlogs", api.StreamLogs)
// TODO: remove http version
c := cors.New(cors.Options{
AllowedOrigins: []string{"https://spinup.host", "http://spinup.host"},
Expand Down

0 comments on commit ca09898

Please sign in to comment.