@cloudnux/cli 0.1.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,120 +0,0 @@
1
- import { FastifyInstance } from "fastify";
2
- import chalk from "chalk";
3
- import consola from "consola";
4
- import logSymbols from "log-symbols";
5
- import { AddressInfo } from "net";
6
- const querystring = require('querystring');
7
-
8
- import {
9
- HTTPAuth,
10
- HttpMethod,
11
- createHttpContext,
12
- createScheduleContext,
13
- } from "@anubis/cloud-provider";
14
- import { env } from "@anubis/utils";
15
-
16
- import * as src from "../../packages/components/<%=component%>/src";
17
-
18
- <%_
19
- const convertRouteParamstoFastifyRouteTemplate = (route) => {
20
- return route.replace(/{(.*?)}/g, (sub) => ":" + sub.slice(1, -1))
21
- }
22
- _%>
23
- function logEntryURL(method: string, routePath: string){
24
- const bgcolors = {
25
- PUT : chalk.bold.bgYellow.white,
26
- POST : chalk.bold.bgGreen.white,
27
- DELETE : chalk.bold.bgRed.white,
28
- GET : chalk.bold.bgBlue.white,
29
- PATCH : chalk.bold.bgGreenBright.white
30
- };
31
- const colors = {
32
- PUT : chalk.bold.yellow,
33
- POST : chalk.bold.green,
34
- DELETE : chalk.bold.red,
35
- GET : chalk.bold.blue,
36
- PATCH : chalk.bold.greenBright
37
- };
38
-
39
- const prefix = new Array(Math.floor((6 - method.length) / 2)).fill(" ").join("");
40
- const postfix = new Array(6 - method.length - prefix.length).fill(" ").join("");
41
-
42
- consola.log(
43
- logSymbols.info + bgcolors[method](`[${prefix}${method}${postfix}]`) + colors[method](` http://localhost:3000/api${routePath}`)
44
- );
45
- }
46
-
47
- async function entries(app: FastifyInstance) {
48
- consola.log(chalk.underline(`\n\rUrls you have in`, chalk.bold(`<%=component%>`), `component:`));
49
- let routePath;
50
- <%_ for (const [name, entry] of Object.entries(entries)) { _%>
51
- <%_ if (entry.trigger.type === "http") { _%>
52
-
53
- routePath = `/http<%= convertRouteParamstoFastifyRouteTemplate(entry.trigger.options.route) %>`;
54
- logEntryURL('<%= entry.trigger.options.method %>',routePath);
55
- app.route({
56
- method: "<%= entry.trigger.options.method %>" as any,
57
- url: routePath,
58
- handler: async function (request, reply) {
59
-
60
- const rawBody = request.rawBody as string;
61
- const baseAddress = app.server.address() as AddressInfo;
62
- const isAuth = env("DEV_IDENTITY")
63
- const ctx = createHttpContext({
64
- body: rawBody,
65
- headers: request.headers,
66
- method: request.method as HttpMethod,
67
- url: `http://localhost:${baseAddress!.port}/api${routePath}`,
68
- matchingKey: "<%= entry.trigger.options.route %>",
69
- params: request.params as Record<string, string>,
70
- rawQueryString : querystring.stringify(request.query),
71
- host: request.hostname
72
- }, isAuth ? {
73
- appId: env("DEV_APP_ID"),
74
- memberId: env("DEV_MEMBER_ID"),
75
- customerId: env("DEV_CUSTOMER_ID"),
76
- identity: env("DEV_IDENTITY") as HTTPAuth["identity"],
77
- token: "",
78
- claims: {
79
- app_id: env("DEV_APP_ID"),
80
- member_id: env("DEV_MEMBER_ID"),
81
- customer_id: env("DEV_CUSTOMER_ID"),
82
- identity: env("DEV_IDENTITY")
83
- }
84
- } : undefined);
85
- try {
86
- await src["<%= entry.handler %>"](ctx);
87
- reply
88
- .headers(ctx.response.headers || {})
89
- .status(ctx.response.status)
90
- .send(ctx.response.body);
91
- }
92
- catch(e){
93
- reply.send(e);
94
- }
95
- }
96
- });
97
-
98
- <%_ } _%>
99
- <%_ if (entry.trigger.type === "schedule") { _%>
100
- logEntryURL('GET','/schedule/<%= name %>');
101
- app.route({
102
- method: "GET",
103
- url: `/schedule/<%= name %>`,
104
- handler: async function (request, reply) {
105
- const ctx = createScheduleContext("<%= name %>");
106
- try {
107
- await src["<%= entry.handler %>"](ctx);
108
- reply.status(200).send("success");
109
- }
110
- catch (e) {
111
- reply.send(e);
112
- }
113
- }
114
- });
115
-
116
- <%_ } _%>
117
- <%_ } _%>
118
- }
119
-
120
- export default entries;
@@ -1,67 +0,0 @@
1
- terraform {
2
- backend "s3" {
3
- key = "<%= component %>/terraform.state"
4
- workspace_key_prefix = "state/<%= component %>"
5
- }
6
-
7
- required_providers {
8
- aws = {
9
- source = "hashicorp/aws"
10
- version = ">= 4.0"
11
- }
12
- }
13
-
14
- required_version = ">= 0.13.1"
15
- }
16
-
17
- data "aws_lambda_function" "this" {
18
- function_name = "<%= component %>"
19
- }
20
-
21
- data "aws_apigatewayv2_apis" "this" {
22
- tags = {
23
- application = "anubis"
24
- }
25
- }
26
-
27
- <% const httpEntries = Object.entries(entries).filter(([k,e]) => e.trigger.type.toLowerCase() === "http") %>
28
- <% if(httpEntries.length > 0) { %>
29
- module "http_trigger_for_<%= component %>" {
30
- source = "../../.deploy/aws/http"
31
- api_gateway_id = sort(data.aws_apigatewayv2_apis.this.ids)[0]
32
- lambda_arn = data.aws_lambda_function.this.arn
33
- lambda_name = "<%= component %>"
34
- entrypoints = {
35
- <% for(const [key,entry] of httpEntries){ %>
36
- "<%= key %>" = {
37
- description = "<% entry.trigger.options.description %>"
38
- method = "<%= entry.trigger.options.method %>"
39
- path = "<%= entry.trigger.options.route %>"
40
- }
41
- <% } %>
42
- }
43
- }
44
-
45
- <% } %>
46
-
47
- <% const scheduleEntries = Object.entries(entries).filter(([k,e]) => e.trigger.type.toLowerCase() === "schedule") %>
48
- <% if(scheduleEntries.length > 0) { %>
49
- module "schedule_trigger_for_<%= component %>" {
50
- source = "../../.deploy/aws/scheduler"
51
- component = "<%= component %>"
52
- target_arn = data.aws_lambda_function.this.arn
53
- schedules = {
54
- <% for(const [key,entry] of scheduleEntries) { %>
55
- "<%= key %>" = {
56
- name = "<%= key %>"
57
- pattern = "<%= entry.trigger.options.pattern %>"
58
- timezone = "UTC"
59
- state = "DISABLED"
60
- retry_policy = {
61
- maximum_retry_attempts = 3
62
- }
63
- }
64
- <% } %>
65
- }
66
- }
67
- <% } %>