Skip to content

Commit

Permalink
Merge pull request #1 from AwalaNetwork/internet-endpoint-codelab
Browse files Browse the repository at this point in the history
Write Awala Internet Codelab
  • Loading branch information
gnarea authored Oct 31, 2023
2 parents 73a4018 + a5937ce commit 1d32156
Show file tree
Hide file tree
Showing 28 changed files with 2,717 additions and 3 deletions.
5 changes: 5 additions & 0 deletions codelabs/aie-gcp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
app-js/node_modules

infrastructure/terraform.tfvars
infrastructure/terraform.tfstate*
infrastructure/.terraform/
1 change: 1 addition & 0 deletions codelabs/aie-gcp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Codelab example for the Awala Internet Endpoint on GCP
7 changes: 7 additions & 0 deletions codelabs/aie-gcp/app-js/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM node:20.3.1
WORKDIR /tmp/app
COPY . ./
RUN npm install
USER node
CMD ["node", "--unhandled-rejections=strict", "./server.js"]
EXPOSE 8080
1,487 changes: 1,487 additions & 0 deletions codelabs/aie-gcp/app-js/package-lock.json

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions codelabs/aie-gcp/app-js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"dependencies": {
"@google-cloud/pubsub": "^4.0.6",
"env-var": "^7.4.1",
"fastify": "^4.24.3"
}
}
47 changes: 47 additions & 0 deletions codelabs/aie-gcp/app-js/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { PubSub } from '@google-cloud/pubsub';
import envVar from 'env-var';
import Fastify from 'fastify';

const OUTGOING_MESSAGES_TOPIC = envVar.get('OUTGOING_MESSAGES_TOPIC')
.required().asString();

const pubSubClient = new PubSub();

const fastify = Fastify({ logger: true });

fastify.get('/', async (_request, reply) => {
// Used by the health check
return reply.send('All good!');
});

fastify.post('/', async (request, reply) => {
// Extract the message and its metadata
const pingData = request.body.message.data;
const pingMessageAttributes = request.body.message.attributes;
const pingSenderId = pingMessageAttributes.source;
const pingRecipientId = pingMessageAttributes.subject;

// Send the pong message
const pongEvent = {
data: pingData,
attributes: {
source: pingRecipientId,
subject: pingSenderId,
},
};
const topic = pubSubClient.topic(OUTGOING_MESSAGES_TOPIC);
await topic.publishMessage(pongEvent);

return reply.send('Message processed');
});

const start = async () => {
try {
await fastify.listen({ port: 8080, host: '0.0.0.0' });
} catch (err) {
fastify.log.error(err);
process.exit(1);
}
}
start();

102 changes: 102 additions & 0 deletions codelabs/aie-gcp/infrastructure/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions codelabs/aie-gcp/infrastructure/endpoint.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
variable "internet_address" {}
variable "pohttp_server_domain" {}

module "awala-endpoint" {
source = "relaycorp/awala-endpoint/google"
version = "1.8.20"

backend_name = "pong"
internet_address = var.internet_address

project_id = var.google_project_id
region = var.google_region

pohttp_server_domain = var.pohttp_server_domain

mongodb_uri = local.mongodb_uri
mongodb_db = local.mongodb_db_name
mongodb_user = mongodbatlas_database_user.main.username
mongodb_password = random_password.mongodb_user_password.result
}

output "pohttp_server_ip_address" {
value = module.awala-endpoint.pohttp_server_ip_address
}
output "bootstrap_job_name" {
value = module.awala-endpoint.bootstrap_job_name
}
33 changes: 33 additions & 0 deletions codelabs/aie-gcp/infrastructure/gcp.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
variable "google_project_id" {}
variable "google_credentials_path" {}
variable "google_region" {
default = "europe-west1"
}

locals {
gcp_services = [
"run.googleapis.com",
"compute.googleapis.com",
"cloudkms.googleapis.com",
"pubsub.googleapis.com",
"secretmanager.googleapis.com",
"iam.googleapis.com",
]
}

provider "google" {
project = var.google_project_id
credentials = file(var.google_credentials_path)
}

provider "google-beta" {
project = var.google_project_id
credentials = file(var.google_credentials_path)
}

resource "google_project_service" "services" {
for_each = toset(local.gcp_services)

service = each.value
disable_dependent_services = true
}
54 changes: 54 additions & 0 deletions codelabs/aie-gcp/infrastructure/mongodbatlas.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
variable "mongodbatlas_public_key" {}
variable "mongodbatlas_private_key" {}
variable "mongodbatlas_project_id" {}

terraform {
required_providers {
mongodbatlas = {
source = "mongodb/mongodbatlas"
version = "~> 1.10.2"
}
}
}

provider "mongodbatlas" {
public_key = var.mongodbatlas_public_key
private_key = var.mongodbatlas_private_key
}

locals {
mongodb_db_name = "main"
mongodb_uri = "${mongodbatlas_serverless_instance.main.connection_strings_standard_srv}/?retryWrites=true&w=majority"
}

resource "mongodbatlas_serverless_instance" "main" {
project_id = var.mongodbatlas_project_id
name = "awala-endpoint"

provider_settings_backing_provider_name = "GCP"
provider_settings_provider_name = "SERVERLESS"
provider_settings_region_name = "WESTERN_EUROPE"
}

resource "mongodbatlas_project_ip_access_list" "main" {
project_id = var.mongodbatlas_project_id
comment = "See https://github.com/relaycorp/terraform-google-awala-endpoint/issues/2"
cidr_block = "0.0.0.0/0"
}

resource "mongodbatlas_database_user" "main" {
project_id = var.mongodbatlas_project_id

username = "awala-endpoint"
password = random_password.mongodb_user_password.result
auth_database_name = "admin"

roles {
role_name = "readWrite"
database_name = local.mongodb_db_name
}
}

resource "random_password" "mongodb_user_password" {
length = 32
}
5 changes: 4 additions & 1 deletion docs/Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
source "https://rubygems.org"

gem "github-pages", "= 228", :group => :jekyll_plugins
group :jekyll_plugins do
gem "github-pages", "= 228"
gem "jekyll-tabs", '~> 1.1'
end

# Work around https://github.com/jekyll/jekyll/issues/8523
gem "webrick", "~> 1.7"
3 changes: 3 additions & 0 deletions docs/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ GEM
jekyll-sitemap (1.4.0)
jekyll (>= 3.7, < 5.0)
jekyll-swiss (1.0.0)
jekyll-tabs (1.1.1)
jekyll (>= 3.0, < 5.0)
jekyll-theme-architect (0.2.0)
jekyll (> 3.5, < 5.0)
jekyll-seo-tag (~> 2.0)
Expand Down Expand Up @@ -266,6 +268,7 @@ PLATFORMS

DEPENDENCIES
github-pages (= 228)
jekyll-tabs (~> 1.1)
webrick (~> 1.7)

BUNDLED WITH
Expand Down
5 changes: 4 additions & 1 deletion docs/_config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
title: "Awala Developers"
description: "Documentation for software developers building Awala-compatible apps."

plugins:
- jekyll-tabs

liquid:
error_mode: strict
strict_filters: true
Expand All @@ -23,6 +26,6 @@ remote_theme: "pmarsceill/just-the-docs@v0.7.0"
color_scheme: dark
search_enabled: false
aux_links:
"GitHub repo": "https://github.com/relaycorp/letro-website"
"GitHub repo": "https://github.com/AwalaNetwork/website-dev"
"Relaycorp": "https://relaycorp.tech"
heading_anchors: true
3 changes: 3 additions & 0 deletions docs/_includes/footer_custom.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p class="fs-3">
Copyright 2023 by <a href="https://relaycorp.tech">Relaycorp</a>.
</p>
2 changes: 2 additions & 0 deletions docs/_includes/head_custom.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<link rel="stylesheet" href="{{ site.baseurl }}/assets/css/jekyll-tabs.css"/>
<script src="{{ site.baseurl }}/assets/js/jekyll-tabs.js"></script>
3 changes: 3 additions & 0 deletions docs/_includes/nav_footer_custom.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p class="fs-3 text-center mx-5">
<a href="https://awala.network">Go to awala.network.</a>
</p>
Loading

0 comments on commit 1d32156

Please sign in to comment.