Change page.jst to use /database.logjson instead of /_analytics/*.json files
[ndcode_site.git] / _config / site.jst
index d11c5c1..0298224 100644 (file)
+let assert = require('assert')
 let EmailJSCache = require('@ndcode/emailjs_cache')
 let ZettairCache = require('@ndcode/zettair_cache')
-let assert = require('assert')
 
-let CustomSite = function(resources, root, options, prev_site) {
-  if (!this instanceof CustomSite)
-    throw Error('CustomSite is a constructor')
-  _jst_server.Site.call(this, resources, root, options, prev_site)
+return async (resources, root, prev_site) => {
+  let logjson = (await import('@ndcode/logjson')).default
 
-  this.emailjs_cache = undefined
-  this.zettair_cache = undefined
-}
+  let CustomSite = function(resources, root, options, prev_site) {
+    if (!this instanceof CustomSite)
+      throw Error('CustomSite is a constructor')
+    _jst_server.Site.call(this, resources, root, options, prev_site)
 
-CustomSite.prototype = Object.create(_jst_server.Site.prototype)
+    this.database = undefined
+    this.emailjs_cache = undefined
+    this.zettair_cache = undefined
+  }
 
-CustomSite.prototype.start = async function() {
-  await _jst_server.Site.prototype.start.call(this)
+  CustomSite.prototype = Object.create(_jst_server.Site.prototype)
+
+  // called when the server starts or the site.jst file is modified
+  // in latter case it will carry over the previously created resource objects
+  CustomSite.prototype.start = async function() {
+    await _jst_server.Site.prototype.start.call(this)
+
+    assert(this.database === undefined)
+    this.database = await this.resources.ref(
+      'database',
+      async () => {
+        let database = new logjson.Database()
+        await database.open(this.root + '/database.logjson')
+        return database
+      }
+    )
+
+    assert(this.emailjs_cache === undefined)
+    this.emailjs_cache = await this.resources.ref(
+      'emailjs_cache',
+      async () => new EmailJSCache(true)
+    )
+
+    assert(this.zettair_cache === undefined)
+    this.zettair_cache = await this.resources.ref(
+      'zettair_cache',
+      async () => new ZettairCache(true)
+    )
+  }
 
-  assert(this.emailjs_cache === undefined)
-  this.emailjs_cache = await this.resources.ref(
-    'emailjs_cache',
-    async () => new EmailJSCache(true)
-  )
+  // called when the server starts or the site.jst file is modified
+  // in latter case the start() method of the new CustomSite object is called
+  // first and then the stop() method of the old CustomSite object, so that the
+  // reference counting can keep the resource objects alive during changeover
+  CustomSite.prototype.stop = async function() {
+    await _jst_server.Site.prototype.stop.call(this)
 
-  assert(this.zettair_cache === undefined)
-  this.zettair_cache = await this.resources.ref(
-    'zettair_cache',
-    async () => new ZettairCache(true)
-  )
-}
+    assert(this.database !== undefined)
+    await this.resources.unref('database')
 
-CustomSite.prototype.stop = async function() {
-  await _jst_server.Site.prototype.stop.call(this)
+    assert(this.emailjs_cache !== undefined)
+    await this.resources.unref('emailjs_cache')
 
-  assert(this.emailjs_cache !== undefined)
-  await this.resources.unref('emailjs_cache')
+    assert(this.zettair_cache !== undefined)
+    await this.resources.unref('zettair_cache')
+  }
 
-  assert(this.zettair_cache !== undefined)
-  await this.resources.unref('zettair_cache')
-}
+  // called once per second, responsible for cache cleaning and flushing
+  CustomSite.prototype.kick = async function() {
+    await _jst_server.Site.prototype.kick.call(this)
 
-CustomSite.prototype.kick = async function() {
-  await _jst_server.Site.prototype.kick.call(this)
+    assert(this.database !== undefined)
+    this.database.kick()
 
-  assert(this.emailjs_cache !== undefined)
-  this.emailjs_cache.kick()
+    assert(this.emailjs_cache !== undefined)
+    this.emailjs_cache.kick()
 
-  assert(this.zettair_cache !== undefined)
-  this.zettair_cache.kick()
-}
+    assert(this.zettair_cache !== undefined)
+    this.zettair_cache.kick()
+  }
 
-CustomSite.prototype.get_emailjs = function(pathname) {
-  return /*await*/ this.emailjs_cache.get(this.root + pathname)
-}
+  // retrieves a particular email account (loaded into an emailjs object)
+  CustomSite.prototype.get_emailjs = function(pathname) {
+    return /*await*/ this.emailjs_cache.get(this.root + pathname)
+  }
 
-CustomSite.prototype.get_zettair = function(pathname) {
-  return /*await*/ this.zettair_cache.get(this.root + pathname)
-}
+  // retrieves a particular search index (node.js wrapper of a zettair object)
+  CustomSite.prototype.get_zettair = function(pathname) {
+    return /*await*/ this.zettair_cache.get(this.root + pathname)
+  }
 
-CustomSite.prototype.respond = async function(env) {
-  if (
-    env.parsed_url.pathname === '/node_modules' ||
-    env.parsed_url.pathname.slice(0, 14) === '/node_modules/' ||
-    env.parsed_url.pathname === '/package.json' ||
-    env.parsed_url.pathname === '/package-lock.json'
-  ) {
-    this.die(env, `banned file ${env.parsed_url.pathname}`)
-    return
+  // customize the file search/serving algorithm for this particular website
+  CustomSite.prototype.respond = async function(env) {
+    if (
+      env.parsed_url.pathname === '/node_modules' ||
+      env.parsed_url.pathname.slice(0, 14) === '/node_modules/' ||
+      env.parsed_url.pathname === '/package.json' ||
+      env.parsed_url.pathname === '/package-lock.json'
+    ) {
+      this.die(env, `banned file ${env.parsed_url.pathname}`)
+      return
+    }
+    return /*await*/ _jst_server.Site.prototype.respond.call(this, env)
   }
-  return /*await*/ _jst_server.Site.prototype.respond.call(this, env)
-}
 
-return async (resources, root, prev_site) => new CustomSite(
-  resources,
-  root,
-  {},
-  prev_site
-)
+  return new CustomSite(
+    resources,
+    root,
+    {},
+    prev_site
+  )
+}