Basic website with home page, contact/feedback forms (project links do nothing)
[ndcode_site.git] / _config / site.jst
1 let EmailJSCache = require('@ndcode/emailjs_cache')
2 let ZettairCache = require('@ndcode/zettair_cache')
3 let assert = require('assert')
4
5 let CustomSite = function(resources, root, options, prev_site) {
6   if (!this instanceof CustomSite)
7     throw Error('CustomSite is a constructor')
8   _jst_server.Site.call(this, resources, root, options, prev_site)
9
10   this.emailjs_cache = undefined
11   this.zettair_cache = undefined
12 }
13
14 CustomSite.prototype = Object.create(_jst_server.Site.prototype)
15
16 CustomSite.prototype.start = async function() {
17   await _jst_server.Site.prototype.start.call(this)
18
19   assert(this.emailjs_cache === undefined)
20   this.emailjs_cache = await this.resources.ref(
21     'emailjs_cache',
22     async () => new EmailJSCache(true)
23   )
24
25   assert(this.zettair_cache === undefined)
26   this.zettair_cache = await this.resources.ref(
27     'zettair_cache',
28     async () => new ZettairCache(true)
29   )
30 }
31
32 CustomSite.prototype.stop = async function() {
33   await _jst_server.Site.prototype.stop.call(this)
34
35   assert(this.emailjs_cache !== undefined)
36   await this.resources.unref('emailjs_cache')
37
38   assert(this.zettair_cache !== undefined)
39   await this.resources.unref('zettair_cache')
40 }
41
42 CustomSite.prototype.kick = async function() {
43   await _jst_server.Site.prototype.kick.call(this)
44
45   assert(this.emailjs_cache !== undefined)
46   this.emailjs_cache.kick()
47
48   assert(this.zettair_cache !== undefined)
49   this.zettair_cache.kick()
50 }
51
52 CustomSite.prototype.get_emailjs = function(pathname) {
53   return /*await*/ this.emailjs_cache.get(this.root + pathname)
54 }
55
56 CustomSite.prototype.get_zettair = function(pathname) {
57   return /*await*/ this.zettair_cache.get(this.root + pathname)
58 }
59
60 CustomSite.prototype.respond = async function(env) {
61   if (
62     env.parsed_url.pathname === '/node_modules' ||
63     env.parsed_url.pathname.slice(0, 14) === '/node_modules/' ||
64     env.parsed_url.pathname === '/package.json'
65   ) {
66     this.die(env, `banned file ${env.parsed_url.pathname}`)
67     return
68   }
69   return /*await*/ _jst_server.Site.prototype.respond.call(this, env)
70 }
71
72 return async (resources, root, prev_site) => new CustomSite(
73   resources,
74   root,
75   {},
76   prev_site
77 )