bug fixes
[henge/kiak.git] / strappServer.js
index 2f8d383..5cced85 100644 (file)
@@ -15,15 +15,17 @@ const dlog = (msg) => console.log(msg)
 class Server {
   constructor(...props) {
     Object.assign(this, new.target.defaults, ...props)
+    this.pendingResponses = new Array(this.responseCacheSize)
     this.init()
+    return this
   }
   init() {
     if (this.cacheDir)
       this.loadCache(this.cacheDir, '')
-    require('fs').readFile('./strapp.js', (err, data) => {
+    require('fs').readFile('./www/strapp.min.js', (err, data) => {
       if (err)
-        throw new Error (err)
-      this.cache['strapp.js'] = ['application/javascript',data]
+        throw new Error(err)
+      this.cache['strapp.min.js'] = ['application/javascript',data]
     })
     this.startHttpd()
   }
@@ -64,7 +66,7 @@ class Server {
     const htArgv = request.url.slice(1).split('?')
     dlog(`request for ${request.url} received`)
     if (request.method === 'GET' && htArgv[0] in this.cache) {
-      dlog(`found in cache`)
+      dlog(`found ${htArgv[0]} in cache`)
       response.writeHead(200, { 'Content-Type': this.cache[htArgv[0]][0] })
       response.write(this.cache[htArgv[0]][1])
       response.end()
@@ -146,9 +148,17 @@ class Server {
       this.app.send(`${args[0]} ${request.method} ${pubKey} ${msgID} ${data}`)
     }
     else {
-      response.writeHead(200, { 'Content-Type': 'text/html' })
-      response.write(Server.bootstrapp)
-      response.end()
+      require('fs').readFile('./bootstrapp.html', (err, data) => {
+        if (err) {
+          response.writeHead(500)
+          response.end()
+        }
+        else {
+          response.writeHead(200, { 'Content-Type': 'text/html' })
+          response.write(data)
+          response.end()
+        }
+      })
     }
   }
 }
@@ -158,7 +168,7 @@ Server.defaults = {
   host: undefined,
   cache: {},
   cacheDir: './www',
-  pendingResponses: new Array(20),
+  responseCacheSize: 40,
   msgID: 0,
   answers: {},
   remoteAdminKey: undefined,
@@ -167,17 +177,6 @@ Server.defaults = {
   httpd: undefined,
   wsd: undefined
 }
-Server.bootstrapp = `
-<!DOCTYPE html>
-<html>
-  <title>bootstrapp</title>
-  <head>
-    <script src='/strapp.js'></script>
-  </head>
-  <body>
-  </body>
-</html>
-`
 
 //TODO: module.exports = Server