Implement a command-line interface to the running webserver, and a way to get/set...
[ndcode_site.git] / api / globals / get.json.jst
1 let logjson = (await import('@ndcode/logjson')).default
2 let XDate = require('xdate')
3
4 return async env => {
5   let post_request = await _require('/_lib/post_request.jst')
6   let session_cookie = await _require('/_lib/session_cookie.jst')
7   let Problem = await _require('/_lib/Problem.jst')
8
9   await post_request(
10     // env
11     env,
12     // handler
13     async () => {
14       let transaction = await env.site.database.Transaction()
15       try {
16         // initialize env.session_key, set cookie in env.response
17         await session_cookie(env, transaction)
18         if (env.signed_in_as === null)
19           throw new Problem('Unauthorized', 'Please sign in first.', 401)
20
21         let root = await transaction.get({})
22         let account = await (
23           await root.get('accounts', {})
24         ).get(env.signed_in_as)
25         if (
26           !await logjson.logjson_to_json(
27             await account.get('administrator')
28           )
29         )
30           throw new Problem('Unauthorized', 'Not administrator.', 401)
31
32         globals = await logjson.logjson_to_json(
33           await root.get('globals', {})
34         )
35         await transaction.commit()
36         return globals
37       }
38       catch (error) {
39         transaction.rollback()
40         throw error
41       }
42     }
43   )
44 }