Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

UI feedback during Kelp GUI app startup (closes #356) #361

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
2 - watch/tail log file on load of app
nikhilsaraf committed Feb 7, 2020

Verified

This commit was signed with the committer’s verified signature.
commit 882c629be47f77b96747cce82cdfa8760c1fbe3f
113 changes: 105 additions & 8 deletions cmd/server_amd64.go
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ import (
"fmt"
"image"
"image/png"
"io/ioutil"
"log"
"net/http"
"os"
@@ -38,6 +39,8 @@ const kelpCcxtPath = "/ccxt"
const ccxtDownloadBaseURL = "https://github.com/stellar/kelp/releases/download/ccxt-rest_v0.0.4"
const ccxtBinaryName = "ccxt-rest"
const ccxtWaitSeconds = 60
const stringPlaceholder = "PLACEHOLDER_URL"
const readyStringIndicator = "Serving frontend and API server on HTTP port"

type serverInputs struct {
port *uint16
@@ -60,37 +63,48 @@ func init() {
options.noHeaders = serverCmd.Flags().Bool("no-headers", false, "do not set X-App-Name and X-App-Version headers on requests to horizon")

serverCmd.Run = func(ccmd *cobra.Command, args []string) {
binDirectory, e := getBinaryDirectory()
if e != nil {
panic(errors.Wrap(e, "could not get binary directory"))
}
log.Printf("binDirectory: %s", binDirectory)

isLocalMode := env == envDev
isLocalDevMode := isLocalMode && *options.dev
kos := kelpos.GetKelpOS()
logFilepath := ""
if !isLocalDevMode {
l := logger.MakeBasicLogger()
t := time.Now().Format("20060102T150405MST")
logDir := "/logs"
logFilename := fmt.Sprintf("kelp-ui_%s.log", t)

binDirectory, e := getBinaryDirectory()
if e != nil {
panic(errors.Wrap(e, "could not get binary directory"))
}
log.Printf("binDirectory: %s", binDirectory)

logDirPath := filepath.Join(binDirectory, kelpPrefsDirectory, logDir)
log.Printf("making logDirPath: %s ...", logDirPath)
e = kos.Mkdir(logDirPath)
if e != nil {
panic(errors.Wrap(e, "could not make directories for logDirPath: "+logDirPath))
}

logFilepath := filepath.Join(logDirPath, logFilename)
logFilepath = filepath.Join(logDirPath, logFilename)
setLogFile(l, logFilepath)
}

if !isLocalDevMode {
// write out tail.html after setting the file to be tailed
tailFileCompiled := strings.Replace(tailFileHTML, stringPlaceholder, logFilepath, -1)
tailFilepath := filepath.Join(binDirectory, kelpPrefsDirectory, "tail.html")
fileContents := []byte(tailFileCompiled)
e := ioutil.WriteFile(tailFilepath, fileContents, 0644)
if e != nil {
panic(fmt.Sprintf("could not write tailfile to path '%s': %s", tailFilepath, e))
}

// kick off the desktop window for UI feedback to the user
// local mode (non --dev) and release binary should open browser (since --dev already opens browser via yarn and returns)
go func() {
url := fmt.Sprintf("http://localhost:%d", *options.port)
// url := fmt.Sprintf("http://localhost:%d", *options.port)
url := tailFilepath
log.Printf("opening up the desktop window to URL '%s'\n", url)
openBrowser(kos, url)
}()
@@ -455,3 +469,86 @@ func quit() {
log.Printf("quitting...")
os.Exit(0)
}

const tailFileHTML = `<!-- taken from http://www.davejennifer.com/computerjunk/javascript/tail-dash-f.html with minor modifications -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<title>Javascript tail -f</title>

<script type="text/javascript">
var lastByte = 0;

if (typeof XMLHttpRequest == "undefined") {
// this is only for really ancient browsers
XMLHttpRequest = function () {
try { return new ActiveXObject("Msxml2.xmlHttp.6.0"); }
catch (e1) { }
try { return new ActiveXObject("Msxml2.xmlHttp.3.0"); }
catch (e2) { }
try { return new ActiveXObject("Msxml2.xmlHttp"); }
catch (e3) { }
throw new Error("This browser does not support xmlHttpRequest.");
};
}

// Substitute the URL for your server log file here...
//
var url = "PLACEHOLDER_URL";

function tailf() {
var ajax = new XMLHttpRequest();
ajax.open("POST", url, true);

if (lastByte == 0) {
// First request - get everything
} else {
//
// All subsequent requests - add the Range header
//
ajax.setRequestHeader("Range", "bytes=" + parseInt(lastByte) + "-");
}

ajax.onreadystatechange = function () {
if (ajax.readyState == 4) {

if (ajax.status == 200) {
// only the first request
lastByte = parseInt(ajax.getResponseHeader("Content-length"));
document.getElementById("thePlace").innerHTML = ajax.responseText;
document.getElementById("theEnd").scrollIntoView()

} else if (ajax.status == 206) {
lastByte += parseInt(ajax.getResponseHeader("Content-length"));
document.getElementById("thePlace").innerHTML += ajax.responseText;
document.getElementById("theEnd").scrollIntoView()

} else if (ajax.status == 416) {
// no new data, so do nothing

} else {
// Some error occurred - just display the status code and response
alert("Ajax status: " + ajax.status + "\n" + ajax.getAllResponseHeaders());
}
}// ready state 4
}//orsc function def

ajax.send(null);

}// function tailf

</script>

</head>


<body onLoad='tailf(); setInterval("tailf()", 250)'>
<div>
<pre id="thePlace"></pre>
<div id="theEnd"></div>
</div>
</body>

</html>
`