Implement a command-line interface to the running webserver, and a way to get/set...
[ndcode_site.git] / api / globals / set.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 globals => {
14       // coerce and/or validate
15       globals = {
16         site_url: globals.site_url.slice(0, 1024),
17         site_title: globals.site_title.slice(0, 1024),
18         contact_from: globals.contact_from.slice(0, 1024),
19         contact_to: globals.contact_to.slice(0, 1024),
20         feedback_from: globals.feedback_from.slice(0, 1024),
21         feedback_to: globals.feedback_to.slice(0, 1024),
22         noreply_from: globals.noreply_from.slice(0, 1024),
23         noreply_signature: globals.noreply_signature.slice(0, 1024),
24         copyright: globals.copyright.slice(0, 1024)
25       }
26
27       let transaction = await env.site.database.Transaction()
28       try {
29         // initialize env.session_key, set cookie in env.response
30         await session_cookie(env, transaction)
31         if (env.signed_in_as === null)
32           throw new Problem('Unauthorized', 'Please sign in first.', 401)
33
34         let root = await transaction.get({})
35         let account = await (
36           await root.get('accounts', {})
37         ).get(env.signed_in_as)
38         if (
39           !await logjson.logjson_to_json(
40             await account.get('administrator')
41           )
42         )
43           throw new Problem('Unauthorized', 'Not administrator.', 401)
44
45         root.set('globals', transaction.json_to_logjson(globals))
46         await transaction.commit()
47       }
48       catch (error) {
49         transaction.rollback()
50         throw error
51       }
52     }
53   )
54 }