Browse Source

scaffolding: add lambda backend

Herby Vojčík 3 months ago
parent
commit
dd21a69923

+ 1 - 0
grunt-init-project/rename.json

@@ -2,5 +2,6 @@
   "gitignore-file": ".gitignore",
   "localamdjson-file": "local.amd.json",
   "src/name.st": "src/{%= name %}.st",
+  "src/backend.st": "src/{%= name %}-Backend.st",
   "src/test.st": "src/{%= name %}-Tests.st"
 }

+ 55 - 1
grunt-init-project/root/Gruntfile.js

@@ -15,6 +15,7 @@ module.exports = function (grunt) {
     grunt.registerTask('test', ['amdconfig:app', 'requirejs:test_runner', 'exec:test_runner', 'clean:test_runner']);
     grunt.registerTask('devel', ['amdconfig:app', 'requirejs:devel']);
     grunt.registerTask('deploy', ['amdconfig:app', 'requirejs:deploy']);
+    grunt.registerTask('deploy:lambda', ['amdconfig:app', 'requirejs:deploy_lambda']);
 
     var polyfillThenPromiseApp = function () {
         define(["require", "amber/es6-promise"], function (require, promiseLib) {
@@ -25,6 +26,33 @@ module.exports = function (grunt) {
         });
     };
 
+    var polyfillThenLambdaApp = function () {
+        define(["require" /*, possible polyfill libs*/], function (require) {
+            /* possible polyfill calls*/
+
+            var amberPromised = new Promise(function (resolve, reject) {
+                require(["__app__"], resolve, reject);
+            });
+
+            return function (className) {
+                var worker, workerPromise = amberPromised.then(function (amber) {
+                    worker = amber.globals[className]._new();
+                });
+                return function (selector) {
+                    var jsSelector, jsSelectorPromise = amberPromised.then(function (amber) {
+                        jsSelector = amber.api.st2js(selector);
+                    });
+                    var readyPromise = Promise.all([workerPromise, jsSelectorPromise]);
+                    return function (event, context) {
+                        return readyPromise.then(function () {
+                            return worker[jsSelector](event, context);
+                        });
+                    };
+                };
+            };
+        });
+    };
+
     // Project configuration.
     grunt.initConfig({
         // Metadata.
@@ -42,7 +70,7 @@ module.exports = function (grunt) {
             },
             all: {
                 src: [
-                    'src/{%= name %}.st', // list all sources in dependency order
+                    'src/{%= name %}.st', 'src/{%= name %}-Backend.st', // list all sources in dependency order
                     'src/{%= name %}-Tests.st' // list all tests in dependency order
                 ],
                 amd_namespace: '{%= namespace %}',
@@ -87,6 +115,32 @@ module.exports = function (grunt) {
                     out: "the.js"
                 }
             },
+            deploy_lambda: {
+                options: {
+                    mainConfigFile: "config.js",
+                    rawText: {
+                        "helios/index": "",
+                        "app": '(' + polyfillThenLambdaApp + '());',
+                        "__app__": "(" + function () {
+                            define(["lambda", "amber/core/Platform-Node"], function (amber) {
+                                return amber.initialize().then(function () {
+                                    return amber;
+                                });
+                            });
+                        } + "());"
+                    },
+                    pragmas: {
+                        excludeIdeData: true,
+                        excludeDebugContexts: true
+                    },
+                    include: ['app'],
+                    findNestedDependencies: true,
+                    exclude: ['helios/index'],
+                    wrap: {start: helpers.nodeWrapper.start, end: "return require('app');" + helpers.nodeWrapper.end},
+                    optimize: "uglify2",
+                    out: "lambda/the.js"
+                }
+            },
             test_runner: {
                 options: {
                     mainConfigFile: "config.js",

+ 1 - 0
grunt-init-project/root/gitignore-file

@@ -4,3 +4,4 @@
 
 /config.js
 /the.js
+/lambda/the.js

+ 1 - 0
grunt-init-project/root/index.html

@@ -20,6 +20,7 @@
 <button id="jquery-append">Hello from jQuery append</button>
 <button id="console-log">Log into JS console</button>
 <button id="timeout-alert">Remind me in five seconds</button>
+<button id="lambda-status">Inspect status of backend</button>
 <ol id="output-list"></ol>
 <!-- EXAMPLE APP END -->
 <script type='text/javascript'>

+ 8 - 0
grunt-init-project/root/lambda.js

@@ -0,0 +1,8 @@
+define([
+    'amber/deploy',
+    // --- packages to be deployed begin here ---
+    '{%= namespace %}/{%= name %}-Backend'
+    // --- packages to be deployed end here ---
+], function (amber) {
+    return amber;
+});

+ 4 - 0
grunt-init-project/root/lambda/index.js

@@ -0,0 +1,4 @@
+var app = require('./the.js')('{%= name %}Backend');
+
+exports.status = app('status');
+exports.echo = app('echoEvent:andContext:');

+ 23 - 0
grunt-init-project/root/src/backend.st

@@ -0,0 +1,23 @@
+Smalltalk createPackage: '{%= name %}-Backend'!
+Object subclass: #{%= name %}Backend
+	slots: {}
+	package: '{%= name %}-Backend'!
+
+!{%= name %}Backend methodsFor: 'lambda api'!
+
+echoEvent: event andContext: context
+	^ #{
+		#statusCode -> 200.
+		#headers->#{'Access-Control-Allow-Origin' -> '*'}.
+		#body -> (JSON stringify: #{#event -> event. #context -> context})
+	}
+!
+
+status
+	^ #{
+		#statusCode -> 200.
+		#headers->#{'Access-Control-Allow-Origin' -> '*'}.
+		#body -> (JSON stringify: #{#ok -> true. #amber -> Smalltalk version. #node -> process version})
+	}
+! !
+

+ 18 - 0
grunt-init-project/root/src/name.st

@@ -23,6 +23,12 @@ doConsoleLog
 	console log: #{ #greeting -> greeting. #target -> target. #callback -> [ console log: greeting, ', ', target, '!!' ] }
 !
 
+doInspectStatus
+	(self getApi: '/status')
+		then: {#json. #inspect}
+		catch: [ :err | Terminal alert: err ]
+!
+
 doJQueryAppend
 	'#output-list' asJQuery append: '<li>jQuery append added me!!</li>'
 !
@@ -31,9 +37,21 @@ doSilkTAG
 	'#output-list' asSilk LI: 'Silk TAG: added me!!'
 ! !
 
+!{%= name %} methodsFor: 'backend'!
+
+endpoint
+	"Return something like 'https://<char mess>.execute-api.eu-central-1.amazonaws.com/default'"
+	^ self error: 'Not yet implemented'
+!
+
+getApi: path
+	^ fetch value: self endpoint, path
+! !
+
 !{%= name %} methodsFor: 'starting'!
 
 augmentPage
+    '#lambda-status' asSilk on: #click bind: [ self doInspectStatus ].
 	'#console-log' asSilk on: #click bind: [ self doConsoleLog ].
 	'#timeout-alert' asSilk on: #click bind: [ self alert: 'REMINDER!!' after: 5000 ].
 	'#amber-with' asJQuery click: [ self doAmberWith ].

+ 1 - 0
grunt-init-project/root/testing.js

@@ -1,5 +1,6 @@
 define([
     './deploy',
+    './lambda',
     'amber/core/SUnit',
     // --- packages used only during automated testing begin here ---
     '{%= namespace %}/{%= name %}-Tests'

+ 1 - 1
grunt-init-project/template.js

@@ -125,7 +125,7 @@ exports.template = function (grunt, init, done) {
         };
         props.devDependencies = {
             "@ambers/ide-starter-modal": "^0.2.0",
-            "@ambers/sdk": "^0.12.0",
+            "@ambers/sdk": "^0.12.1",
             "@ambers/contrib-legacy": "^0.8.0",
             "@ambers/helios": "^0.11.0",
             "grunt": "^1.0.3",