Skip to content

Commit bcfb86d

Browse files
authored
[webapp] Fixed errors when static files not found (#51)
* Fixing web server runtime location * updating static file locations, defaults * Merge branch 'master' into run_loc * improved health check page error reporting
1 parent f62c8b6 commit bcfb86d

File tree

3 files changed

+57
-35
lines changed

3 files changed

+57
-35
lines changed

webapp/static/js/webapp.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ function drawBwtestSingleDir(dir, yAxisLabel, legend, reqCol, achCol) {
202202
credits : {
203203
enabled : legend,
204204
text : legend ? 'Download Data' : null,
205-
href : legend ? './files/webapp/data/' : null,
205+
href : legend ? './data/' : null,
206206
},
207207
exporting : {
208208
enabled : false
@@ -466,6 +466,9 @@ function command(continuous) {
466466
} else {
467467
handleGeneralResponse();
468468
}
469+
}).fail(function(error) {
470+
showError(error.responseJSON);
471+
handleGeneralResponse();
469472
});
470473
// onsubmit should always return false to override native http call
471474
return false;

webapp/template/health.html

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
<h2>SCIONLab Health Check</h2>
1010
<p>
11-
If all automated tests have passed, please continue to <a
12-
href="/apps">Apps</a> in the menu to use SCIONLab.
11+
If all automated tests have passed, please continue to <a href="/apps">Apps</a>
12+
in the menu to use SCIONLab.
1313
</p>
1414
<ul id="health-list">
1515
</ul>
@@ -32,12 +32,14 @@ <h2>SCIONLab Health Check</h2>
3232
if (d.err) {
3333
showError(d.err);
3434
}
35+
}).fail(function(error) {
36+
showError(error.responseJSON);
3537
});
3638
var last = [];
3739
var healthInterval = setInterval(function() {
3840
// setup interval to check status file webserver will update
3941
$.ajax({
40-
url : "files/data/healthcheck-result.json",
42+
url : "data/healthcheck-result.json",
4143
method : 'GET',
4244
dataType : "json",
4345
success : function(data) {
@@ -71,9 +73,13 @@ <h2>SCIONLab Health Check</h2>
7173
"Check last completed " + now.toLocaleDateString()
7274
+ " " + now.toLocaleTimeString() + ".");
7375
}
76+
},
77+
error : function(jqXHR, textStatus, errorThrown) {
78+
showError(this.url + ' ' + textStatus + ': ' + errorThrown);
79+
clearInterval(healthInterval);
7480
}
7581
});
76-
}, 500);
82+
}, 1000);
7783

7884
function getListItemDiv(resp, test) {
7985
var a = $('<a>').attr('href', "#hc-" + test).attr('class',

webapp/webapp.go

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"os/exec"
1616
"path"
1717
"regexp"
18-
"runtime"
1918
"strconv"
2019
"strings"
2120
"time"
@@ -28,16 +27,24 @@ import (
2827
. "github.com/netsec-ethz/scion-apps/webapp/util"
2928
)
3029

31-
var addr = flag.String("a", "0.0.0.0", "server host address")
32-
var port = flag.Int("p", 8080, "server port number")
33-
var root = flag.String("r", ".", "file system path to browse from")
30+
// browseRoot is browse-only, consider security (def: cwd)
31+
var browseRoot = flag.String("r", ".",
32+
"Root path to browse from, CAUTION: read-access granted from -a and -p.")
33+
34+
// staticRoot for serving/writing static data (def: cwd)
35+
var staticRoot = flag.String("s", ".",
36+
"Static path of web server files (local repo scion-apps/webapp).")
37+
38+
// cwdPath - this is where images are going, this is runtime (record, no settings)
39+
var cwdPath = "."
40+
41+
var addr = flag.String("a", "127.0.0.1", "Address of server host.")
42+
var port = flag.Int("p", 8000, "Port of server host.")
3443
var cmdBufLen = 1024
3544
var browserAddr = "127.0.0.1"
3645
var rootmarker = ".webapp"
37-
var srcpath string
3846
var myIa string
3947
var id = "webapp"
40-
var logDir = "./logs"
4148

4249
// Ensures an inactive browser will end continuous testing
4350
var maxContTimeout = time.Duration(10) * time.Minute
@@ -57,16 +64,25 @@ type Page struct {
5764
MyIA string
5865
}
5966

67+
func ensurePath(srcpath, staticDir string) string {
68+
dir := path.Join(srcpath, staticDir)
69+
if _, err := os.Stat(dir); os.IsNotExist(err) {
70+
os.Mkdir(dir, os.ModePerm)
71+
}
72+
return dir
73+
}
74+
6075
func main() {
6176
flag.Parse()
62-
_, srcfile, _, _ := runtime.Caller(0)
63-
srcpath = path.Dir(srcfile)
77+
// correct static files are required for the app to serve them, else fail
78+
if _, err := os.Stat(path.Join(*staticRoot, "static")); os.IsNotExist(err) {
79+
log.Error("-s flag must be set with local repo: scion-apps/webapp")
80+
CheckFatal(err)
81+
return
82+
}
6483

6584
// logging
66-
logDirPath := path.Join(srcpath, "logs")
67-
if _, err := os.Stat(logDirPath); os.IsNotExist(err) {
68-
os.Mkdir(logDirPath, os.ModePerm)
69-
}
85+
logDirPath := ensurePath(*staticRoot, "logs")
7086
log.Root().SetHandler(log.MultiHandler(
7187
log.LvlFilterHandler(log.LvlDebug,
7288
log.StreamHandler(os.Stderr, fmt15.Fmt15Format(fmt15.ColorMap))),
@@ -76,20 +92,17 @@ func main() {
7692
log.Info("======================> Webapp started")
7793

7894
// prepare templates
79-
templates = prepareTemplates(srcpath)
95+
templates = prepareTemplates(*staticRoot)
8096
// open and manage database
81-
dbpath := path.Join(srcpath, "webapp.db")
97+
dbpath := path.Join(*staticRoot, "webapp.db")
8298
model.InitDB(dbpath)
8399
defer model.CloseDB()
84100
model.LoadDB()
85101
go model.MaintainDatabase()
86-
dataDirPath := path.Join(srcpath, "data")
87-
if _, err := os.Stat(dataDirPath); os.IsNotExist(err) {
88-
os.Mkdir(dataDirPath, os.ModePerm)
89-
}
102+
ensurePath(*staticRoot, "data")
90103
// generate client/server default
91-
lib.GenClientNodeDefaults(srcpath)
92-
lib.GenServerNodeDefaults(srcpath)
104+
lib.GenClientNodeDefaults(*staticRoot)
105+
lib.GenServerNodeDefaults(*staticRoot)
93106
myIa = lib.GetLocalIa()
94107
refreshRootDirectory()
95108
appsBuildCheck("bwtester")
@@ -103,11 +116,11 @@ func main() {
103116
http.HandleFunc("/astopo", astopoHandler)
104117
http.HandleFunc("/crt", crtHandler)
105118
http.HandleFunc("/trc", trcHandler)
106-
fsStatic := http.FileServer(http.Dir(path.Join(srcpath, "static")))
119+
fsStatic := http.FileServer(http.Dir(path.Join(*staticRoot, "static")))
107120
http.Handle("/static/", http.StripPrefix("/static/", fsStatic))
108-
fsImageFetcher := http.FileServer(http.Dir("."))
109-
http.Handle("/images/", http.StripPrefix("/images/", fsImageFetcher))
110-
fsFileBrowser := http.FileServer(http.Dir(*root))
121+
fsData := http.FileServer(http.Dir(path.Join(*staticRoot, "data")))
122+
http.Handle("/data/", http.StripPrefix("/data/", fsData))
123+
fsFileBrowser := http.FileServer(http.Dir(*browseRoot))
111124
http.Handle("/files/", http.StripPrefix("/files/", fsFileBrowser))
112125

113126
http.HandleFunc("/command", commandHandler)
@@ -129,7 +142,7 @@ func main() {
129142
http.HandleFunc("/gettrc", lib.TrcHandler)
130143

131144
log.Info(fmt.Sprintf("Browser access: at http://%s:%d.", browserAddr, *port))
132-
log.Info("File browser root:", "root", *root)
145+
log.Info("File browser root:", "root", *browseRoot)
133146
log.Info(fmt.Sprintf("Listening on %s:%d...", *addr, *port))
134147
err := http.ListenAndServe(fmt.Sprintf("%s:%d", *addr, *port), logRequestHandler(http.DefaultServeMux))
135148
CheckFatal(err)
@@ -481,16 +494,16 @@ func writeCmdOutput(w http.ResponseWriter, reader io.Reader, stdin io.WriteClose
481494
}
482495
// store in database
483496
model.StoreBwTestItem(d)
484-
lib.WriteBwtestCsv(d, srcpath)
497+
lib.WriteBwtestCsv(d, *staticRoot)
485498
}
486499
}
487500

488501
func healthCheckHandler(w http.ResponseWriter, r *http.Request) {
489-
lib.HealthCheckHandler(w, r, srcpath)
502+
lib.HealthCheckHandler(w, r, *staticRoot)
490503
}
491504

492505
func getBwByTimeHandler(w http.ResponseWriter, r *http.Request) {
493-
lib.GetBwByTimeHandler(w, r, bwActive, srcpath)
506+
lib.GetBwByTimeHandler(w, r, bwActive, *staticRoot)
494507
}
495508

496509
// Handles locating most recent image formatting it for graphic display in response.
@@ -499,13 +512,13 @@ func findImageHandler(w http.ResponseWriter, r *http.Request) {
499512
}
500513

501514
func getNodesHandler(w http.ResponseWriter, r *http.Request) {
502-
lib.GetNodesHandler(w, r, srcpath)
515+
lib.GetNodesHandler(w, r, *staticRoot)
503516
}
504517

505518
// Used to workaround cache-control issues by ensuring root specified by user
506519
// has updated last modified date by writing a .webapp file
507520
func refreshRootDirectory() {
508-
cliFp := path.Join(srcpath, *root, rootmarker)
521+
cliFp := path.Join(*staticRoot, *browseRoot, rootmarker)
509522
err := ioutil.WriteFile(cliFp, []byte(``), 0644)
510523
CheckError(err)
511524
}

0 commit comments

Comments
 (0)