From 2e77cf83c97d7332738a2bddceec3c8c40bb0ce0 Mon Sep 17 00:00:00 2001
From: Xiao Gui <xgui3783@gmail.com>
Date: Sun, 17 May 2020 17:49:03 +0200
Subject: [PATCH] update netlify functions

---
 functions/nehubaConfig.js              | 38 ++++++++++++++++++++++++++
 functions/{templates => }/templates.js |  1 -
 netlify.toml                           |  2 +-
 3 files changed, 39 insertions(+), 2 deletions(-)
 create mode 100644 functions/nehubaConfig.js
 rename functions/{templates => }/templates.js (96%)

diff --git a/functions/nehubaConfig.js b/functions/nehubaConfig.js
new file mode 100644
index 000000000..52f1f91a7
--- /dev/null
+++ b/functions/nehubaConfig.js
@@ -0,0 +1,38 @@
+const fs = require('fs')
+
+exports.handler = (ev, ctx, cb) => {
+  const {
+    path,
+    httpMethod,
+    headers,
+    queryStringParameters,
+    body,
+    isBase64Encoded,
+  } = ev
+
+
+  const re = /nehubaConfig\/(.+)$/.exec(path)
+  if (!re) {
+    return cb(null, {
+      status: 401,
+      body: `config name is required`
+    })
+  }
+
+  const configName = re[1]
+  fs.readFile(`./json/${configName}.json`, 'utf-8', (err, data) => {
+    if (err) {
+      return cb(null, {
+        status: 404,
+        body: `config ${configName} does not exist, ${err.toString()}`
+      })
+    }
+    return cb(null, {
+      status: 200,
+      body: data,
+      headers: {
+        'Content-type': 'application/json'
+      }
+    })
+  })
+}
\ No newline at end of file
diff --git a/functions/templates/templates.js b/functions/templates.js
similarity index 96%
rename from functions/templates/templates.js
rename to functions/templates.js
index fb7d8cd88..753ff3e53 100644
--- a/functions/templates/templates.js
+++ b/functions/templates.js
@@ -1,5 +1,4 @@
 const fs = require('fs')
-const path = require('path')
 
 exports.handler = (ev, ctx, cb) => {
   
diff --git a/netlify.toml b/netlify.toml
index dfcc620f6..100526f9f 100644
--- a/netlify.toml
+++ b/netlify.toml
@@ -2,6 +2,6 @@
 
 [build]
   publish="dist/aot/"
-  command="cp -R src/res/ext functions/templates/json && npm run build-aot"
+  command="cp -R src/res/ext functions/json && npm run build-aot"
   functions="functions"
   environment={ BACKEND_URL=".netlify/functions/" }
\ No newline at end of file
-- 
GitLab