add: stand configs
This commit is contained in:
@@ -0,0 +1,25 @@
|
|||||||
|
terraform {
|
||||||
|
required_providers {
|
||||||
|
nubes = {
|
||||||
|
source = "terra.k8c.ru/nubes/nubes"
|
||||||
|
version = "2.1.26"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "api_token" {
|
||||||
|
type = string
|
||||||
|
sensitive = true
|
||||||
|
description = "Nubes API token"
|
||||||
|
}
|
||||||
|
# variable "s3_uid" {
|
||||||
|
# type = string
|
||||||
|
# sensitive = true
|
||||||
|
# description = "S3 Storage UUID"
|
||||||
|
# }
|
||||||
|
|
||||||
|
provider "nubes" {
|
||||||
|
api_token = var.api_token
|
||||||
|
api_endpoint = "https://deck-api.ngcloud.ru/api/v1/index.cfm"
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,141 @@
|
|||||||
|
resource "nubes_postgres" "db2" {
|
||||||
|
# Основной Postgres-кластер для демо.
|
||||||
|
resource_name = "pg-testing"
|
||||||
|
s3_uid = "6d6061cb-b0c1-44b9-8969-a70f08fe673c"
|
||||||
|
# s3_uid = "6d6061cb-b0c1-44b9-8969-a70f08fe673c"
|
||||||
|
resource_realm = "k8s-3.ext.nubes.ru"
|
||||||
|
resource_instances = 1
|
||||||
|
resource_memory = 1024
|
||||||
|
resource_c_p_u = 1000
|
||||||
|
resource_disk = "1"
|
||||||
|
app_version = "17"
|
||||||
|
json_parameters = jsonencode({
|
||||||
|
# Выключаем подробные логи подключений в демо.
|
||||||
|
log_connections = "off"
|
||||||
|
log_disconnections = "off"
|
||||||
|
})
|
||||||
|
enable_pg_pooler_master = false
|
||||||
|
enable_pg_pooler_slave = false
|
||||||
|
allow_no_s_s_l = false
|
||||||
|
auto_scale = false
|
||||||
|
auto_scale_percentage = 10
|
||||||
|
auto_scale_tech_window = 0
|
||||||
|
auto_scale_quota_gb = "1"
|
||||||
|
need_external_address_master = false
|
||||||
|
|
||||||
|
adopt_existing_on_create = true
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
# output "pg_vault_secrets" {
|
||||||
|
# value = nubes_postgres.db2.vault_secrets
|
||||||
|
# sensitive = true
|
||||||
|
# }
|
||||||
|
resource "nubes_lucee" "app1" {
|
||||||
|
# Lucee UI, который читает/пишет в Postgres.
|
||||||
|
resource_name = "lucy11"
|
||||||
|
resource_realm = nubes_postgres.db2.resource_realm
|
||||||
|
domain = "web07"
|
||||||
|
git_path = "https://gitea-naeel.giteak8s.services.ngcloud.ru/naeel/testlucee.git"
|
||||||
|
|
||||||
|
json_env = jsonencode({
|
||||||
|
# JDBC datasource для Lucee.
|
||||||
|
testds_class = "org.postgresql.Driver"
|
||||||
|
testds_bundleName = "org.postgresql.jdbc"
|
||||||
|
testds_bundleVersion = "42.6.0"
|
||||||
|
testds_connectionString = "jdbc:postgresql://${nubes_postgres.db2.state_out_flat["internalConnect.master"]}:5432/postgres?sslmode=require"
|
||||||
|
testds_username = nubes_postgres.db2.vault_secrets["adminUser"]
|
||||||
|
testds_password = nubes_postgres.db2.vault_secrets["adminPass"]
|
||||||
|
testds_connectionLimit = "7"
|
||||||
|
testds_liveTimeout = "15"
|
||||||
|
testds_validate = "false"
|
||||||
|
})
|
||||||
|
|
||||||
|
resource_c_p_u = 300
|
||||||
|
resource_memory = 512
|
||||||
|
resource_instances = 1
|
||||||
|
app_version = "5.4"
|
||||||
|
|
||||||
|
depends_on = [nubes_postgres.db2]
|
||||||
|
|
||||||
|
adopt_existing_on_create = true
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "nubes_s3bucket" "baba_bucket" {
|
||||||
|
resource_name = "baba-buck-test2"
|
||||||
|
s3_user_uid = "6d6061cb-b0c1-44b9-8969-a70f08fe673c"
|
||||||
|
bucket_name = "baba0"
|
||||||
|
# adopt_existing_on_create=true
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
resource "nubes_nodejs" "app3" {
|
||||||
|
# NodeJS демо, работающий с тем же Postgres.
|
||||||
|
resource_name = "node_0111"
|
||||||
|
resource_realm = nubes_postgres.db2.resource_realm
|
||||||
|
domain = "node07"
|
||||||
|
git_path = "https://gitea-naeel.giteak8s.services.ngcloud.ru/naeel/testnode.git"
|
||||||
|
health_path = "/healthz"
|
||||||
|
app_version = "23"
|
||||||
|
|
||||||
|
json_env = jsonencode({
|
||||||
|
# Переменные подключения к Postgres.
|
||||||
|
PGHOST = nubes_postgres.db2.state_out_flat["internalConnect.master"]
|
||||||
|
PGPORT = "5432"
|
||||||
|
PGUSER = nubes_postgres.db2.vault_secrets["adminUser"]
|
||||||
|
PGPASSWORD = nubes_postgres.db2.vault_secrets["adminPass"]
|
||||||
|
PGSSLMODE = "require"
|
||||||
|
DATABASE_URL = format(
|
||||||
|
"postgresql://%s:%s@%s:5432/postgres",
|
||||||
|
nubes_postgres.db2.vault_secrets["adminUser"],
|
||||||
|
nubes_postgres.db2.vault_secrets["adminPass"],
|
||||||
|
nubes_postgres.db2.state_out_flat["internalConnect.master"]
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
resource_c_p_u = 300
|
||||||
|
resource_memory = 256
|
||||||
|
resource_instances = 1
|
||||||
|
|
||||||
|
adopt_existing_on_create = true
|
||||||
|
# suspend_on_destroy = true
|
||||||
|
|
||||||
|
depends_on = [nubes_postgres.db2]
|
||||||
|
}
|
||||||
|
|
||||||
|
# resource "nubes_nodejs" "app4" {
|
||||||
|
# # NodeJS демо, работающий с тем же Postgres.
|
||||||
|
# resource_name = "node_0113"
|
||||||
|
# resource_realm = nubes_postgres.db2.resource_realm
|
||||||
|
# domain = "node07"
|
||||||
|
# git_path = "https://gitea-naeel.giteak8s.services.ngcloud.ru/naeel/testnode.git"
|
||||||
|
# health_path = "/healthz"
|
||||||
|
# app_version = "23"
|
||||||
|
|
||||||
|
# json_env = jsonencode({
|
||||||
|
# # Переменные подключения к Postgres.
|
||||||
|
# PGHOST = nubes_postgres.db2.state_out_flat["internalConnect.master"]
|
||||||
|
# PGPORT = "5432"
|
||||||
|
# PGUSER = nubes_postgres.db2.vault_secrets["adminUser"]
|
||||||
|
# PGPASSWORD = nubes_postgres.db2.vault_secrets["adminPass"]
|
||||||
|
# PGSSLMODE = "require"
|
||||||
|
# DATABASE_URL = format(
|
||||||
|
# "postgresql://%s:%s@%s:5432/postgres",
|
||||||
|
# nubes_postgres.db2.vault_secrets["adminUser"],
|
||||||
|
# nubes_postgres.db2.vault_secrets["adminPass"],
|
||||||
|
# nubes_postgres.db2.state_out_flat["internalConnect.master"]
|
||||||
|
# )
|
||||||
|
# })
|
||||||
|
|
||||||
|
# resource_c_p_u = 300
|
||||||
|
# resource_memory = 256
|
||||||
|
# resource_instances = 1
|
||||||
|
|
||||||
|
# adopt_existing_on_create = true
|
||||||
|
# # suspend_on_destroy = true
|
||||||
|
|
||||||
|
# depends_on = [nubes_postgres.db2]
|
||||||
|
# }
|
||||||
|
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
# resource "nubes_vc_vm_v3" "vmnginx01" {
|
||||||
|
# resource_name = "vm-nginx-auto"
|
||||||
|
# vapp_uid = "464eb4a0-62dc-42cd-af2b-4305db3c8d1b"
|
||||||
|
# vm_name = "vmnginx01"
|
||||||
|
# vm_cpu = 1
|
||||||
|
# vm_ram = 1
|
||||||
|
# vm_disk = 10
|
||||||
|
# image_vm = "Ubuntu_22-20G"
|
||||||
|
# user_login = "root"
|
||||||
|
# user_public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDUd0DCo52wbKDRR6skYmwx6ffLkFewpXu/YSwtalNlVIhQaOCaEJmY5SerGVqgvXQakckptt3bTRGFPKThxr3LsOk9iftvNKQysr7HMSHWLk3nQvix8DaBIla2Ej+zGV/BfIkFA/85R/wwOC6wTLxOcByXKeJCuIx05NUXh0/5nRc3nowpwyKusUm9VBvMyLdZ4MER8bFq+QhdTaEf1sR3PWNQeDA6Oo4qn/HEU2AzqDI1zEPY8ZT9O6Uc30KdbIaEzAmKpw7F0ai0pmlqbzeDVGx4M4FewQei354d9zvu3CI2KryWQ88jm4feuMcl7wGrhD+kS2F+AUiHCJaPczeh"
|
||||||
|
# ip_space_name = "no-needed"
|
||||||
|
# access_ip_list = jsonencode([])
|
||||||
|
# access_port_list = jsonencode(["22", "80", "10050"])
|
||||||
|
# adopt_existing_on_create = true
|
||||||
|
# }
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
/*
|
||||||
|
resource "nubes_flask" "app4" {
|
||||||
|
resource_name = "flask_0"
|
||||||
|
resource_realm = nubes_postgres.db2.resource_realm
|
||||||
|
domain = "flask"
|
||||||
|
git_path = "https://gitea-naeel.giteak8s.services.ngcloud.ru/naeel/testflask.git"
|
||||||
|
|
||||||
|
json_env = jsonencode({
|
||||||
|
PGHOST = nubes_postgres.db2.state_out_flat["internalConnect.master"]
|
||||||
|
PGPORT = "5432"
|
||||||
|
PGUSER = nubes_postgres.db2.vault_secrets["adminUser"]
|
||||||
|
PGPASSWORD = nubes_postgres.db2.vault_secrets["adminPass"]
|
||||||
|
PGSSLMODE = "require"
|
||||||
|
DATABASE_URL = format(
|
||||||
|
"postgresql://%s:%s@%s:5432/postgres",
|
||||||
|
nubes_postgres.db2.vault_secrets["adminUser"],
|
||||||
|
nubes_postgres.db2.vault_secrets["adminPass"],
|
||||||
|
nubes_postgres.db2.state_out_flat["internalConnect.master"]
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
resource_c_p_u = 300
|
||||||
|
resource_memory = 256
|
||||||
|
resource_instances = 1
|
||||||
|
|
||||||
|
depends_on = [nubes_postgres.db2]
|
||||||
|
}
|
||||||
|
*/
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
terraform {
|
||||||
|
required_providers {
|
||||||
|
nubes = {
|
||||||
|
source = "terra.k8c.ru/nubes/nubes"
|
||||||
|
version = "2.1.12"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "api_token" {
|
||||||
|
type = string
|
||||||
|
sensitive = true
|
||||||
|
description = "Nubes API token"
|
||||||
|
}
|
||||||
|
|
||||||
|
provider "nubes" {
|
||||||
|
api_token = var.api_token
|
||||||
|
api_endpoint = "https://deck-api.ngcloud.ru/api/v1/index.cfm"
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
resource "nubes_postgres" "db2" {
|
||||||
|
# Основной Postgres-кластер для демо.
|
||||||
|
resource_name = "pg-tst0"
|
||||||
|
s3_uid = "6d6061cb-b0c1-44b9-8969-a70f08fe673c"
|
||||||
|
# s3_uid = "s3-111805"
|
||||||
|
resource_realm = "k8s-3.ext.nubes.ru"
|
||||||
|
resource_instances = 1
|
||||||
|
resource_memory = 512
|
||||||
|
resource_c_p_u = 500
|
||||||
|
resource_disk = "1"
|
||||||
|
app_version = "17"
|
||||||
|
json_parameters = jsonencode({
|
||||||
|
# Выключаем подробные логи подключений в демо.
|
||||||
|
log_connections = "off"
|
||||||
|
log_disconnections = "off"
|
||||||
|
})
|
||||||
|
enable_pg_pooler_master = false
|
||||||
|
enable_pg_pooler_slave = false
|
||||||
|
allow_no_s_s_l = false
|
||||||
|
auto_scale = false
|
||||||
|
auto_scale_percentage = 10
|
||||||
|
auto_scale_tech_window = 0
|
||||||
|
auto_scale_quota_gb = "1"
|
||||||
|
need_external_address_master = false
|
||||||
|
}
|
||||||
|
|
||||||
|
output "pg_vault_secrets" {
|
||||||
|
value = nubes_postgres.db2.vault_secrets
|
||||||
|
sensitive = true
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "nubes_lucee" "app1" {
|
||||||
|
# Lucee UI, который читает/пишет в Postgres.
|
||||||
|
resource_name = "lucy1"
|
||||||
|
resource_realm = nubes_postgres.db2.resource_realm
|
||||||
|
domain = "web03"
|
||||||
|
git_path = "https://gitea-naeel.giteak8s.services.ngcloud.ru/naeel/testlucee.git"
|
||||||
|
|
||||||
|
json_env = jsonencode({
|
||||||
|
# JDBC datasource для Lucee.
|
||||||
|
testds_class = "org.postgresql.Driver"
|
||||||
|
testds_bundleName = "org.postgresql.jdbc"
|
||||||
|
testds_bundleVersion = "42.6.0"
|
||||||
|
testds_connectionString = "jdbc:postgresql://${nubes_postgres.db2.state_out_flat["internalConnect.master"]}:5432/postgres"
|
||||||
|
testds_username = nubes_postgres.db2.vault_secrets["adminUser"]
|
||||||
|
testds_password = nubes_postgres.db2.vault_secrets["adminPass"]
|
||||||
|
testds_connectionLimit = "5"
|
||||||
|
testds_liveTimeout = "15"
|
||||||
|
testds_validate = "false"
|
||||||
|
})
|
||||||
|
|
||||||
|
resource_c_p_u = 300
|
||||||
|
resource_memory = 512
|
||||||
|
resource_instances = 1
|
||||||
|
app_version = "5.4"
|
||||||
|
|
||||||
|
depends_on = [nubes_postgres.db2]
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "nubes_nodejs" "app3" {
|
||||||
|
# NodeJS демо, работающий с тем же Postgres.
|
||||||
|
resource_name = "node_0"
|
||||||
|
resource_realm = nubes_postgres.db2.resource_realm
|
||||||
|
domain = "node"
|
||||||
|
git_path = "https://gitea-naeel.giteak8s.services.ngcloud.ru/naeel/testnode.git"
|
||||||
|
health_path = "/healthz"
|
||||||
|
app_version = "23"
|
||||||
|
|
||||||
|
json_env = jsonencode({
|
||||||
|
# Переменные подключения к Postgres.
|
||||||
|
PGHOST = nubes_postgres.db2.state_out_flat["internalConnect.master"]
|
||||||
|
PGPORT = "5432"
|
||||||
|
PGUSER = nubes_postgres.db2.vault_secrets["adminUser"]
|
||||||
|
PGPASSWORD = nubes_postgres.db2.vault_secrets["adminPass"]
|
||||||
|
PGSSLMODE = "require"
|
||||||
|
DATABASE_URL = format(
|
||||||
|
"postgresql://%s:%s@%s:5432/postgres",
|
||||||
|
nubes_postgres.db2.vault_secrets["adminUser"],
|
||||||
|
nubes_postgres.db2.vault_secrets["adminPass"],
|
||||||
|
nubes_postgres.db2.state_out_flat["internalConnect.master"]
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
resource_c_p_u = 300
|
||||||
|
resource_memory = 256
|
||||||
|
resource_instances = 1
|
||||||
|
|
||||||
|
depends_on = [nubes_postgres.db2]
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
site/templates/
|
||||||
|
site/static/
|
||||||
|
venv/
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
disable=missing-function-docstring
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
# Базовый образ Python для Flask-приложения.
|
||||||
|
FROM python:3.9-slim
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY requirements.txt .
|
||||||
|
|
||||||
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
|
COPY site /app/site
|
||||||
|
|
||||||
|
EXPOSE 5000
|
||||||
|
|
||||||
|
CMD ["python", "site/app.py"]
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
# Flask + Postgres Demo
|
||||||
|
|
||||||
|
Простое CRUD-приложение на Flask, которое пишет и читает из таблицы `nubes_test_table`.
|
||||||
|
Если поле ввода пустое, вставляется запись "Это Flask сделал".
|
||||||
|
|
||||||
|
Примечание: таблица создается автоматически при первом запросе.
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
Flask==2.0.1
|
||||||
|
Werkzeug==2.3.7
|
||||||
|
psycopg2-binary==2.9.9
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
<cfcomponent displayname="Application" output="true">
|
||||||
|
<!--- Базовая конфигурация Lucee и datasource. --->
|
||||||
|
<cfset this.Name = "nubes-app-v8" />
|
||||||
|
<cfset this.sessionmanagement = "Yes" />
|
||||||
|
<cfset this.datasource = "testds" />
|
||||||
|
<!--- Инициализируем datasource из переменных окружения. --->
|
||||||
|
<cfset getDS(this.datasource) />
|
||||||
|
|
||||||
|
<!--- Собираем datasource из *_field переменных окружения. --->
|
||||||
|
<cffunction name="getDS" access="private" returntype="void">
|
||||||
|
<cfargument name="dsname" type="string" required="true"/>
|
||||||
|
<cfset var system = createObject("java", "java.lang.System")/>
|
||||||
|
<cfset var ds = {} />
|
||||||
|
<cfloop list="class,connectionString,database,driver,host,port,type,url,username,password,bundleName,bundleVersion,connectionLimit,liveTimeout,validate" item="field">
|
||||||
|
<cfset var envVal = system.getEnv("#arguments.dsname#_#field#") />
|
||||||
|
<cfif isDefined("envVal") AND len(envVal)><cfset ds[field] = envVal /></cfif>
|
||||||
|
</cfloop>
|
||||||
|
<cfset this.datasources[arguments.dsname] = ds />
|
||||||
|
</cffunction>
|
||||||
|
|
||||||
|
<!--- CRUD над таблицей nubes_test_table по POST запросам. --->
|
||||||
|
<cffunction name="OnRequest" access="public" returntype="void" output="true">
|
||||||
|
<cfargument name="template" type="string" required="true" />
|
||||||
|
<cfset request.DS = this.datasource />
|
||||||
|
|
||||||
|
<!--- Обработка insert/update/delete через form.crud_action. --->
|
||||||
|
<cfif CGI.REQUEST_METHOD EQ "POST" AND structKeyExists(form, "crud_action")>
|
||||||
|
<cftry>
|
||||||
|
<cfswitch expression="#form.crud_action#">
|
||||||
|
<cfcase value="insert">
|
||||||
|
<cfquery datasource="#request.DS#">
|
||||||
|
INSERT INTO nubes_test_table (test_data) VALUES (<cfqueryparam value="#form.txt_content#" cfsqltype="cf_sql_varchar">)
|
||||||
|
</cfquery>
|
||||||
|
</cfcase>
|
||||||
|
<cfcase value="update">
|
||||||
|
<cfquery datasource="#request.DS#">
|
||||||
|
UPDATE nubes_test_table SET test_data = <cfqueryparam value="#form.txt_content#" cfsqltype="cf_sql_varchar"> WHERE id = <cfqueryparam value="#form.id#" cfsqltype="cf_sql_integer">
|
||||||
|
</cfquery>
|
||||||
|
</cfcase>
|
||||||
|
<cfcase value="delete">
|
||||||
|
<cfquery datasource="#request.DS#">
|
||||||
|
DELETE FROM nubes_test_table WHERE id = <cfqueryparam value="#form.id#" cfsqltype="cf_sql_integer">
|
||||||
|
</cfquery>
|
||||||
|
</cfcase>
|
||||||
|
</cfswitch>
|
||||||
|
<cflocation url="#CGI.SCRIPT_NAME#" addtoken="false">
|
||||||
|
<cfcatch><cfset request.db_error = cfcatch.message /></cfcatch>
|
||||||
|
</cftry>
|
||||||
|
</cfif>
|
||||||
|
|
||||||
|
<!--- Гарантируем наличие таблицы при первом заходе. --->
|
||||||
|
<cftry>
|
||||||
|
<cfquery datasource="#request.DS#">CREATE TABLE IF NOT EXISTS nubes_test_table (id SERIAL PRIMARY KEY, test_data TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP);</cfquery>
|
||||||
|
<cfcatch><cfset request.db_error = cfcatch.message /></cfcatch>
|
||||||
|
</cftry>
|
||||||
|
<cfinclude template="#arguments.template#" />
|
||||||
|
</cffunction>
|
||||||
|
</cfcomponent>
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
# Тестовое приложение by Terraform - Lucee & Postgres
|
||||||
|
|
||||||
|
### Что здесь можно делать:
|
||||||
|
|
||||||
|
* **Просмотр:** Список последних записей загружается автоматически при открытии страницы.
|
||||||
|
* **Добавление:** Введите текст в верхнее поле и нажмите кнопку «Добавить», чтобы сохранить данные в базу.
|
||||||
|
* **Редактирование:** Вы можете изменить текст любой записи прямо в таблице. Не забудьте нажать на кнопку с дискетой (💾), чтобы сохранить изменения.
|
||||||
|
* **Удаление:** Нажмите на иконку корзины (🗑), чтобы навсегда удалить запись из базы данных.
|
||||||
|
|
||||||
|
Примечание: таблица создается автоматически при первом открытии страницы.
|
||||||
|
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
<!--- Редирект на основной UI экран. --->
|
||||||
|
<cflocation addtoken="No" url="query.cfm##q"/>
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="ru">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Nubes | Управление данными</title>
|
||||||
|
<link rel="icon" href="https://nubes.ru/themes/custom/nubes/images/nubes-ico.svg" type="image/svg+xml">
|
||||||
|
<style>
|
||||||
|
:root { --nubes-blue: #005BFF; --nubes-dark: #1A1A1A; --nubes-grey: #F8F9FA; --nubes-border: #E5E7EB; }
|
||||||
|
body { font-family: 'Segoe UI', Tahoma, sans-serif; margin: 0; padding: 0; background: var(--nubes-grey); color: var(--nubes-dark); }
|
||||||
|
.header-bg { position: sticky; top: 0; z-index: 1000; background: #fff; border-bottom: 1px solid var(--nubes-border); padding: 15px 0; box-shadow: 0 2px 10px rgba(0,0,0,0.05); }
|
||||||
|
.container { max-width: 1000px; margin: auto; padding: 0 20px; }
|
||||||
|
.header-content { display: flex; align-items: center; justify-content: space-between; }
|
||||||
|
.logo { height: 40px; }
|
||||||
|
.main-content { padding: 40px 0; }
|
||||||
|
.card { background: #fff; padding: 32px; border-radius: 16px; box-shadow: 0 4px 20px rgba(0,0,0,0.04); }
|
||||||
|
.btn { display: inline-flex; align-items: center; justify-content: center; cursor: pointer; padding: 12px 24px; border: none; border-radius: 8px; font-weight: 600; font-size: 14px; }
|
||||||
|
.btn-primary { background: var(--nubes-blue); color: #fff; }
|
||||||
|
/* Увеличенные кнопки действий */
|
||||||
|
.btn-action { padding: 12px; background: #fff; border: 1px solid var(--nubes-border); border-radius: 8px; font-size: 24px; line-height: 1; cursor: pointer; min-width: 50px; }
|
||||||
|
.btn-action:hover { background: var(--nubes-grey); border-color: var(--nubes-blue); }
|
||||||
|
.input-group { display: flex; gap: 12px; margin-bottom: 32px; }
|
||||||
|
input[type="text"] { flex-grow: 1; padding: 12px 16px; border: 1px solid var(--nubes-border); border-radius: 8px; font-size: 14px; }
|
||||||
|
/* Полосатая таблица */
|
||||||
|
table { width: 100%; border-collapse: collapse; }
|
||||||
|
th { text-align: left; padding: 16px; font-size: 12px; text-transform: uppercase; color: #6B7280; border-bottom: 1px solid var(--nubes-border); }
|
||||||
|
td { padding: 16px; border-bottom: 1px solid var(--nubes-border); }
|
||||||
|
tbody tr:nth-child(even) { background-color: #FAFBFC; }
|
||||||
|
tbody tr:hover { background-color: #F3F4F6; }
|
||||||
|
.id-cell { font-family: monospace; color: #9CA3AF; width: 60px; }
|
||||||
|
.actions-cell { display: flex; gap: 12px; width: 130px; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<!--- Верхняя панель с брендингом. --->
|
||||||
|
<div class="header-bg">
|
||||||
|
<div class="container header-content">
|
||||||
|
<img src="https://nubes.ru/themes/custom/nubes_2025/logo.svg" alt="Nubes" class="logo">
|
||||||
|
<div style="font-size: 14px; color: var(--nubes-blue); font-weight: 600;">Lucee + Postgres Demo</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="container main-content">
|
||||||
|
<!--- Форма добавления записи. --->
|
||||||
|
<div class="card">
|
||||||
|
<form method="post" class="input-group">
|
||||||
|
<input type="hidden" name="crud_action" value="insert">
|
||||||
|
<input type="text" name="txt_content" placeholder="Новое сообщение..." required>
|
||||||
|
<button type="submit" class="btn btn-primary">Добавить</button>
|
||||||
|
</form>
|
||||||
|
<!--- Читаем последние записи для отображения. --->
|
||||||
|
<cfquery name="qGet" datasource="#request.DS#">SELECT * FROM nubes_test_table ORDER BY id DESC LIMIT 20</cfquery>
|
||||||
|
<table>
|
||||||
|
<thead><tr><th>ID</th><th>Содержимое</th><th>Действия</th></tr></thead>
|
||||||
|
<tbody>
|
||||||
|
<cfoutput query="qGet">
|
||||||
|
<tr>
|
||||||
|
<td class="id-cell">#id#</td>
|
||||||
|
<td>
|
||||||
|
<form method="post" id="upd_#id#" style="margin:0">
|
||||||
|
<input type="hidden" name="crud_action" value="update"><input type="hidden" name="id" value="#id#">
|
||||||
|
<input type="text" name="txt_content" value="#HTMLEditFormat(test_data)#" style="width:100%; border:none; background:transparent;">
|
||||||
|
</form>
|
||||||
|
</td>
|
||||||
|
<td class="actions-cell">
|
||||||
|
<button type="submit" form="upd_#id#" class="btn-action">💾</button>
|
||||||
|
<form method="post" style="margin:0" onsubmit="return confirm('Удалить?')">
|
||||||
|
<input type="hidden" name="crud_action" value="delete"><input type="hidden" name="id" value="#id#">
|
||||||
|
<button type="submit" class="btn-action">🗑</button>
|
||||||
|
</form>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</cfoutput>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
again test hello
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
node_modules/
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
# NodeJS + Postgres Demo
|
||||||
|
|
||||||
|
Simple Node.js CRUD app that writes to `nubes_test_table` and renders a small HTML UI.
|
||||||
|
If the input is empty, it inserts "Node did it".
|
||||||
|
|
||||||
|
Примечание: таблица создается автоматически, а дубликаты подряд отсекаются по таймауту.
|
||||||
+6808
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,69 @@
|
|||||||
|
{
|
||||||
|
"name": "loopback4-example-github",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"description": "loopback4-example-github",
|
||||||
|
"keywords": [
|
||||||
|
"loopback-application",
|
||||||
|
"loopback"
|
||||||
|
],
|
||||||
|
"main": "server.js",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=10.16"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"build": "lb-tsc",
|
||||||
|
"build:watch": "lb-tsc --watch",
|
||||||
|
"lint": "npm run eslint && npm run prettier:check",
|
||||||
|
"lint:fix": "npm run eslint:fix && npm run prettier:fix",
|
||||||
|
"prettier:cli": "lb-prettier \"**/*.ts\" \"**/*.js\"",
|
||||||
|
"prettier:check": "npm run prettier:cli -- -l",
|
||||||
|
"prettier:fix": "npm run prettier:cli -- --write",
|
||||||
|
"eslint": "lb-eslint --report-unused-disable-directives .",
|
||||||
|
"eslint:fix": "npm run eslint -- --fix",
|
||||||
|
"pretest": "npm run rebuild",
|
||||||
|
"test": "lb-mocha --allow-console-logs \"dist/__tests__\"",
|
||||||
|
"posttest": "npm run lint",
|
||||||
|
"test:dev": "lb-mocha --allow-console-logs dist/__tests__/**/*.js && npm run posttest",
|
||||||
|
"docker:build": "docker build -t loopback4-example-github .",
|
||||||
|
"docker:run": "docker run -p 3000:3000 -d loopback4-example-github",
|
||||||
|
"premigrate": "npm run build",
|
||||||
|
"migrate": "node ./dist/migrate",
|
||||||
|
"preopenapi-spec": "npm run build",
|
||||||
|
"openapi-spec": "node ./dist/openapi-spec",
|
||||||
|
"start": "node server.js",
|
||||||
|
"clean": "lb-clean dist *.tsbuildinfo .eslintcache",
|
||||||
|
"rebuild": "npm run clean && npm run build"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": ""
|
||||||
|
},
|
||||||
|
"author": "Diana Lau <dhmlau@ca.ibm.com>",
|
||||||
|
"license": "",
|
||||||
|
"files": [
|
||||||
|
"README.md",
|
||||||
|
"dist",
|
||||||
|
"src",
|
||||||
|
"!*/__tests__"
|
||||||
|
],
|
||||||
|
"dependencies": {
|
||||||
|
"@loopback/boot": "^3.4.1",
|
||||||
|
"@loopback/core": "^2.16.1",
|
||||||
|
"@loopback/repository": "^3.7.0",
|
||||||
|
"@loopback/rest": "^9.3.1",
|
||||||
|
"@loopback/rest-explorer": "^3.3.1",
|
||||||
|
"@loopback/service-proxy": "^3.2.1",
|
||||||
|
"loopback-connector-rest": "^3.7.0",
|
||||||
|
"pg": "^8.18.0",
|
||||||
|
"tslib": "^2.0.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@loopback/build": "^6.4.1",
|
||||||
|
"@loopback/eslint-config": "^10.2.1",
|
||||||
|
"@loopback/testlab": "^3.4.1",
|
||||||
|
"@types/node": "^10.17.60",
|
||||||
|
"eslint": "^7.28.0",
|
||||||
|
"source-map-support": "^0.5.19",
|
||||||
|
"typescript": "~4.3.2"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<title>loopback4-example-github</title>
|
||||||
|
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<link rel="shortcut icon" type="image/x-icon" href="https://loopback.io/favicon.ico">
|
||||||
|
|
||||||
|
<style>
|
||||||
|
h3 {
|
||||||
|
margin-left: 25px;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
a, a:visited {
|
||||||
|
color: #3f5dff;
|
||||||
|
}
|
||||||
|
|
||||||
|
h3 a {
|
||||||
|
margin-left: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
a:hover, a:focus, a:active {
|
||||||
|
color: #001956;
|
||||||
|
}
|
||||||
|
|
||||||
|
.power {
|
||||||
|
position: absolute;
|
||||||
|
bottom: 25px;
|
||||||
|
left: 50%;
|
||||||
|
transform: translateX(-50%);
|
||||||
|
}
|
||||||
|
|
||||||
|
.info {
|
||||||
|
position: absolute;
|
||||||
|
top: 50%;
|
||||||
|
left: 50%;
|
||||||
|
transform: translate(-50%, -50%)
|
||||||
|
}
|
||||||
|
|
||||||
|
.info h1 {
|
||||||
|
text-align: center;
|
||||||
|
margin-bottom: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.info p {
|
||||||
|
text-align: center;
|
||||||
|
margin-bottom: 3em;
|
||||||
|
margin-top: 1em;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (prefers-color-scheme: dark) {
|
||||||
|
body {
|
||||||
|
background-color: rgb(29, 30, 32);
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
|
||||||
|
a, a:visited {
|
||||||
|
color: #4990e2;
|
||||||
|
}
|
||||||
|
|
||||||
|
a:hover, a:focus, a:active {
|
||||||
|
color: #2b78ff;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<div class="info">
|
||||||
|
<h1>loopback4-example-github</h1>
|
||||||
|
<p>Version 1.0.0</p>
|
||||||
|
|
||||||
|
<h3>OpenAPI spec: <a href="/openapi.json">/openapi.json</a></h3>
|
||||||
|
<h3>API Explorer: <a href="/explorer">/explorer</a></h3>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<footer class="power">
|
||||||
|
<a href="https://loopback.io" target="_blank">
|
||||||
|
<img src="https://loopback.io/images/branding/powered-by-loopback/blue/powered-by-loopback-sm.png" />
|
||||||
|
</a>
|
||||||
|
</footer>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
||||||
@@ -0,0 +1,204 @@
|
|||||||
|
const http = require("http");
|
||||||
|
const { URL } = require("url");
|
||||||
|
const { Pool } = require("pg");
|
||||||
|
|
||||||
|
// Конфигурация Postgres, DATABASE_URL имеет приоритет.
|
||||||
|
function buildPgConfig() {
|
||||||
|
if (process.env.DATABASE_URL) {
|
||||||
|
return {
|
||||||
|
connectionString: process.env.DATABASE_URL,
|
||||||
|
ssl: process.env.PGSSLMODE === "require" ? { rejectUnauthorized: false } : undefined,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
host: process.env.PGHOST,
|
||||||
|
port: Number(process.env.PGPORT || "5432"),
|
||||||
|
user: process.env.PGUSER,
|
||||||
|
password: process.env.PGPASSWORD,
|
||||||
|
database: process.env.PGDATABASE || "postgres",
|
||||||
|
ssl: process.env.PGSSLMODE === "require" ? { rejectUnauthorized: false } : undefined,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Общий пул подключений.
|
||||||
|
const pool = new Pool(buildPgConfig());
|
||||||
|
|
||||||
|
// Таблица для демо создается автоматически.
|
||||||
|
async function ensureTable() {
|
||||||
|
await pool.query(
|
||||||
|
"CREATE TABLE IF NOT EXISTS nubes_test_table (id SERIAL PRIMARY KEY, test_data TEXT, created_at TIMESTAMP DEFAULT NOW())"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Простой парсер application/x-www-form-urlencoded.
|
||||||
|
function parseForm(body) {
|
||||||
|
return body
|
||||||
|
.split("&")
|
||||||
|
.map((pair) => pair.split("="))
|
||||||
|
.reduce((acc, [key, value]) => {
|
||||||
|
acc[decodeURIComponent(key)] = decodeURIComponent((value || "").replace(/\+/g, " "));
|
||||||
|
return acc;
|
||||||
|
}, {});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Рендер HTML-страницы с CRUD-формами.
|
||||||
|
function renderPage(rows, error) {
|
||||||
|
const errorHtml = error ? `<p style="color:red">${error}</p>` : "";
|
||||||
|
const rowsHtml = rows
|
||||||
|
.map(
|
||||||
|
(row) => `
|
||||||
|
<tr>
|
||||||
|
<td>${row.id}</td>
|
||||||
|
<td>
|
||||||
|
<form method="POST" action="/update">
|
||||||
|
<input type="hidden" name="id" value="${row.id}">
|
||||||
|
<input type="text" name="txt_content" value="${row.test_data}">
|
||||||
|
<button type="submit">save</button>
|
||||||
|
</form>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<form method="POST" action="/delete" onsubmit="return confirm('Delete?')">
|
||||||
|
<input type="hidden" name="id" value="${row.id}">
|
||||||
|
<button type="submit">delete</button>
|
||||||
|
</form>
|
||||||
|
</td>
|
||||||
|
</tr>`
|
||||||
|
)
|
||||||
|
.join("");
|
||||||
|
|
||||||
|
return `<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>NodeJS + Postgres Demo</title>
|
||||||
|
<link rel="icon" href="https://nubes.ru/themes/custom/nubes/images/nubes-ico.svg" type="image/svg+xml">
|
||||||
|
<style>
|
||||||
|
:root { --nubes-blue: #005BFF; --nubes-dark: #1A1A1A; --nubes-grey: #F8F9FA; --nubes-border: #E5E7EB; }
|
||||||
|
body { font-family: 'Segoe UI', Tahoma, sans-serif; margin: 0; padding: 0; background: var(--nubes-grey); color: var(--nubes-dark); }
|
||||||
|
.header-bg { position: sticky; top: 0; z-index: 1000; background: #fff; border-bottom: 1px solid var(--nubes-border); padding: 15px 0; box-shadow: 0 2px 10px rgba(0,0,0,0.05); }
|
||||||
|
.container { max-width: 1000px; margin: auto; padding: 0 20px; }
|
||||||
|
.header-content { display: flex; align-items: center; justify-content: space-between; }
|
||||||
|
.logo { height: 40px; }
|
||||||
|
.main-content { padding: 40px 0; }
|
||||||
|
.card { background: #fff; padding: 32px; border-radius: 16px; box-shadow: 0 4px 20px rgba(0,0,0,0.04); }
|
||||||
|
.btn { display: inline-flex; align-items: center; justify-content: center; cursor: pointer; padding: 12px 24px; border: none; border-radius: 8px; font-weight: 600; font-size: 14px; }
|
||||||
|
.btn-primary { background: var(--nubes-blue); color: #fff; }
|
||||||
|
.btn-action { padding: 12px; background: #fff; border: 1px solid var(--nubes-border); border-radius: 8px; font-size: 18px; line-height: 1; cursor: pointer; min-width: 44px; }
|
||||||
|
.btn-action:hover { background: var(--nubes-grey); border-color: var(--nubes-blue); }
|
||||||
|
.input-group { display: flex; gap: 12px; margin-bottom: 32px; }
|
||||||
|
input[type="text"] { flex-grow: 1; width: 100%; padding: 12px 16px; border: 1px solid var(--nubes-border); border-radius: 8px; font-size: 14px; }
|
||||||
|
table { width: 100%; border-collapse: collapse; }
|
||||||
|
th { text-align: left; padding: 16px; font-size: 12px; text-transform: uppercase; color: #6B7280; border-bottom: 1px solid var(--nubes-border); }
|
||||||
|
td { padding: 16px; border-bottom: 1px solid var(--nubes-border); }
|
||||||
|
tbody tr:nth-child(even) { background-color: #FAFBFC; }
|
||||||
|
tbody tr:hover { background-color: #F3F4F6; }
|
||||||
|
.id-cell { font-family: monospace; color: #9CA3AF; width: 60px; }
|
||||||
|
.actions-cell { display: flex; gap: 12px; width: 130px; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="header-bg">
|
||||||
|
<div class="container header-content">
|
||||||
|
<img src="https://nubes.ru/themes/custom/nubes_2025/logo.svg" alt="Nubes" class="logo">
|
||||||
|
<div style="font-size: 14px; color: var(--nubes-blue); font-weight: 600;">NodeJS + Postgres Demo</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="container main-content">
|
||||||
|
<div class="card">
|
||||||
|
${errorHtml}
|
||||||
|
<form method="POST" action="/add" class="input-group" onsubmit="const btn=this.querySelector('button'); if(btn){btn.disabled=true; btn.textContent='Adding...';}">
|
||||||
|
<input type="text" name="txt_content" placeholder="New message" required>
|
||||||
|
<button type="submit" class="btn btn-primary">Add</button>
|
||||||
|
</form>
|
||||||
|
<table>
|
||||||
|
<thead><tr><th>ID</th><th>Content</th><th>Actions</th></tr></thead>
|
||||||
|
<tbody>
|
||||||
|
${rowsHtml}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Маршруты: /, /add, /update, /delete, /healthz.
|
||||||
|
async function handleRequest(req, res) {
|
||||||
|
const url = new URL(req.url, `http://${req.headers.host}`);
|
||||||
|
|
||||||
|
if (req.method === "GET" && url.pathname === "/healthz") {
|
||||||
|
res.writeHead(200, { "Content-Type": "text/plain" });
|
||||||
|
res.end("ok");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.method === "GET" && url.pathname === "/") {
|
||||||
|
try {
|
||||||
|
await ensureTable();
|
||||||
|
const { rows } = await pool.query(
|
||||||
|
"SELECT id, test_data FROM nubes_test_table ORDER BY id DESC LIMIT 20"
|
||||||
|
);
|
||||||
|
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
|
||||||
|
res.end(renderPage(rows));
|
||||||
|
} catch (err) {
|
||||||
|
res.writeHead(500, { "Content-Type": "text/html; charset=utf-8" });
|
||||||
|
res.end(renderPage([], err.message));
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.method === "POST" && ["/add", "/update", "/delete"].includes(url.pathname)) {
|
||||||
|
let body = "";
|
||||||
|
req.on("data", (chunk) => {
|
||||||
|
body += chunk;
|
||||||
|
});
|
||||||
|
req.on("end", async () => {
|
||||||
|
const form = parseForm(body);
|
||||||
|
try {
|
||||||
|
await ensureTable();
|
||||||
|
if (url.pathname === "/add") {
|
||||||
|
const content = form.txt_content || "Node did it";
|
||||||
|
// Защита от быстрых дублей подряд.
|
||||||
|
const { rows: lastRows } = await pool.query(
|
||||||
|
"SELECT test_data, created_at FROM nubes_test_table ORDER BY id DESC LIMIT 1"
|
||||||
|
);
|
||||||
|
const last = lastRows[0];
|
||||||
|
const isDuplicate =
|
||||||
|
last &&
|
||||||
|
last.test_data === content &&
|
||||||
|
Date.now() - new Date(last.created_at).getTime() < 3000;
|
||||||
|
|
||||||
|
if (!isDuplicate) {
|
||||||
|
await pool.query("INSERT INTO nubes_test_table (test_data) VALUES ($1)", [content]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (url.pathname === "/update") {
|
||||||
|
await pool.query("UPDATE nubes_test_table SET test_data=$1 WHERE id=$2", [
|
||||||
|
form.txt_content || "",
|
||||||
|
form.id,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
if (url.pathname === "/delete") {
|
||||||
|
await pool.query("DELETE FROM nubes_test_table WHERE id=$1", [form.id]);
|
||||||
|
}
|
||||||
|
res.writeHead(303, { Location: "/" });
|
||||||
|
res.end();
|
||||||
|
} catch (err) {
|
||||||
|
res.writeHead(500, { "Content-Type": "text/html; charset=utf-8" });
|
||||||
|
res.end(renderPage([], err.message));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
res.writeHead(404, { "Content-Type": "text/plain" });
|
||||||
|
res.end("Not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
const port = process.env.PORT || 3000;
|
||||||
|
const server = http.createServer(handleRequest);
|
||||||
|
|
||||||
|
server.listen(port, () => {
|
||||||
|
console.log(`Server running on port ${port}`);
|
||||||
|
});
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
# Tests
|
||||||
|
|
||||||
|
Please place your tests in this folder.
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
import {Client} from '@loopback/testlab';
|
||||||
|
import {Loopback4ExampleGithubApplication} from '../..';
|
||||||
|
import {setupApplication} from './test-helper';
|
||||||
|
|
||||||
|
describe('HomePage', () => {
|
||||||
|
let app: Loopback4ExampleGithubApplication;
|
||||||
|
let client: Client;
|
||||||
|
|
||||||
|
before('setupApplication', async () => {
|
||||||
|
({app, client} = await setupApplication());
|
||||||
|
});
|
||||||
|
|
||||||
|
after(async () => {
|
||||||
|
await app.stop();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('exposes a default home page', async () => {
|
||||||
|
await client
|
||||||
|
.get('/')
|
||||||
|
.expect(200)
|
||||||
|
.expect('Content-Type', /text\/html/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('exposes self-hosted explorer', async () => {
|
||||||
|
await client
|
||||||
|
.get('/explorer/')
|
||||||
|
.expect(200)
|
||||||
|
.expect('Content-Type', /text\/html/)
|
||||||
|
.expect(/<title>LoopBack API Explorer/);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
import {Client, expect} from '@loopback/testlab';
|
||||||
|
import {Loopback4ExampleGithubApplication} from '../..';
|
||||||
|
import {setupApplication} from './test-helper';
|
||||||
|
|
||||||
|
describe('PingController', () => {
|
||||||
|
let app: Loopback4ExampleGithubApplication;
|
||||||
|
let client: Client;
|
||||||
|
|
||||||
|
before('setupApplication', async () => {
|
||||||
|
({app, client} = await setupApplication());
|
||||||
|
});
|
||||||
|
|
||||||
|
after(async () => {
|
||||||
|
await app.stop();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('invokes GET /ping', async () => {
|
||||||
|
const res = await client.get('/ping?msg=world').expect(200);
|
||||||
|
expect(res.body).to.containEql({greeting: 'Hello from LoopBack'});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
import {Loopback4ExampleGithubApplication} from '../..';
|
||||||
|
import {
|
||||||
|
createRestAppClient,
|
||||||
|
givenHttpServerConfig,
|
||||||
|
Client,
|
||||||
|
} from '@loopback/testlab';
|
||||||
|
|
||||||
|
export async function setupApplication(): Promise<AppWithClient> {
|
||||||
|
const restConfig = givenHttpServerConfig({
|
||||||
|
// Customize the server configuration here.
|
||||||
|
// Empty values (undefined, '') will be ignored by the helper.
|
||||||
|
//
|
||||||
|
// host: process.env.HOST,
|
||||||
|
// port: +process.env.PORT,
|
||||||
|
});
|
||||||
|
|
||||||
|
const app = new Loopback4ExampleGithubApplication({
|
||||||
|
rest: restConfig,
|
||||||
|
});
|
||||||
|
|
||||||
|
await app.boot();
|
||||||
|
await app.start();
|
||||||
|
|
||||||
|
const client = createRestAppClient(app);
|
||||||
|
|
||||||
|
return {app, client};
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface AppWithClient {
|
||||||
|
app: Loopback4ExampleGithubApplication;
|
||||||
|
client: Client;
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
import {BootMixin} from '@loopback/boot';
|
||||||
|
import {ApplicationConfig} from '@loopback/core';
|
||||||
|
import {
|
||||||
|
RestExplorerBindings,
|
||||||
|
RestExplorerComponent,
|
||||||
|
} from '@loopback/rest-explorer';
|
||||||
|
import {RepositoryMixin} from '@loopback/repository';
|
||||||
|
import {RestApplication} from '@loopback/rest';
|
||||||
|
import {ServiceMixin} from '@loopback/service-proxy';
|
||||||
|
import path from 'path';
|
||||||
|
import {MySequence} from './sequence';
|
||||||
|
|
||||||
|
export {ApplicationConfig};
|
||||||
|
|
||||||
|
export class Loopback4ExampleGithubApplication extends BootMixin(
|
||||||
|
ServiceMixin(RepositoryMixin(RestApplication)),
|
||||||
|
) {
|
||||||
|
constructor(options: ApplicationConfig = {}) {
|
||||||
|
super(options);
|
||||||
|
|
||||||
|
// Подключаем кастомную sequence для обработки запросов.
|
||||||
|
this.sequence(MySequence);
|
||||||
|
|
||||||
|
// Главная страница из папки public.
|
||||||
|
this.static('/', path.join(__dirname, '../public'));
|
||||||
|
|
||||||
|
// Включаем REST Explorer.
|
||||||
|
this.configure(RestExplorerBindings.COMPONENT).to({
|
||||||
|
path: '/explorer',
|
||||||
|
});
|
||||||
|
this.component(RestExplorerComponent);
|
||||||
|
|
||||||
|
this.projectRoot = __dirname;
|
||||||
|
// Настройки автозагрузки контроллеров.
|
||||||
|
this.bootOptions = {
|
||||||
|
controllers: {
|
||||||
|
// Ищем контроллеры в папке controllers.
|
||||||
|
dirs: ['controllers'],
|
||||||
|
extensions: ['.controller.js'],
|
||||||
|
nested: true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
# Controllers
|
||||||
|
|
||||||
|
This directory contains source files for the controllers exported by this app.
|
||||||
|
|
||||||
|
To add a new empty controller, type in `lb4 controller [<name>]` from the
|
||||||
|
command-line of your application's root directory.
|
||||||
|
|
||||||
|
For more information, please visit
|
||||||
|
[Controller generator](http://loopback.io/doc/en/lb4/Controller-generator.html).
|
||||||
@@ -0,0 +1,134 @@
|
|||||||
|
// Uncomment these imports to begin using these cool features!
|
||||||
|
|
||||||
|
import {inject} from '@loopback/context';
|
||||||
|
import {get, getModelSchemaRef, param} from '@loopback/openapi-v3';
|
||||||
|
import {QueryResult, ResultIssueInfo} from '../models';
|
||||||
|
import {GhQueryService, IssueInfo, QueryResponse} from '../services';
|
||||||
|
|
||||||
|
// import {inject} from '@loopback/core';
|
||||||
|
|
||||||
|
|
||||||
|
export class GhQueryController {
|
||||||
|
// inject the GhQueryService service proxy
|
||||||
|
constructor(@inject('services.GhQueryService') protected queryService:GhQueryService) {}
|
||||||
|
|
||||||
|
// create the API that get the issues by providing:
|
||||||
|
// repo: <GitHub org>/<GitHub repo>. For example, `strongloop/loopback-next`
|
||||||
|
// label: If it has special characters, you need to escape it.
|
||||||
|
// For example, if the label is "help wanted", it will be "help+wanted".
|
||||||
|
@get('/issues/repo/{repo}/label/{label}', {
|
||||||
|
responses: {
|
||||||
|
'200': {
|
||||||
|
description: 'Array of GitHub issues info',
|
||||||
|
content: {
|
||||||
|
'application/json': {
|
||||||
|
schema: getModelSchemaRef(QueryResult)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
async getIssuesByLabel(
|
||||||
|
@param.path.string('repo') repo: string,
|
||||||
|
@param.path.string('label') label:string): Promise<QueryResult> {
|
||||||
|
let result:QueryResponse = await this.queryService.getIssuesByLabel(repo, label);
|
||||||
|
let queryResult = new QueryResult();
|
||||||
|
queryResult.items = [];
|
||||||
|
queryResult.total_count = result.body.total_count;
|
||||||
|
result.body.items.forEach(issue => {
|
||||||
|
this.addToResult(issue, queryResult);
|
||||||
|
});
|
||||||
|
|
||||||
|
// check if there is next page of the results
|
||||||
|
const nextLink = this.getNextLink(result.headers.link);
|
||||||
|
if (nextLink == null) return queryResult;
|
||||||
|
await this.getIssueByURL(nextLink, this.queryService, queryResult);
|
||||||
|
return queryResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get issues from URL
|
||||||
|
* @param nextLinkURL
|
||||||
|
* @param queryService
|
||||||
|
* @param queryResult
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
async getIssueByURL(nextLinkURL: string, queryService: GhQueryService, queryResult:QueryResult) {
|
||||||
|
let result = await queryService.getIssuesByURL(nextLinkURL);
|
||||||
|
result.body.items.forEach(issue => {
|
||||||
|
this.addToResult(issue, queryResult);
|
||||||
|
});
|
||||||
|
|
||||||
|
const nextLink2 = this.getNextLink(result.headers.link);
|
||||||
|
if (nextLink2 == null) return;
|
||||||
|
await this.getIssueByURL(nextLink2, queryService, queryResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the URL for the "next" page.
|
||||||
|
* The Link header is in the format of:
|
||||||
|
* Link: <https://api.github.com/search/code?q=addClass+user%3Amozilla&page=2>; rel="next",
|
||||||
|
<https://api.github.com/search/code?q=addClass+user%3Amozilla&page=34>; rel="last"
|
||||||
|
* @param link
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
getNextLink(link: string): string|null {
|
||||||
|
if (link == undefined) return null;
|
||||||
|
|
||||||
|
let tokens: string[] = link.split(',');
|
||||||
|
let url: string|null = null;
|
||||||
|
|
||||||
|
tokens.forEach(token => {
|
||||||
|
if (token.indexOf('rel="next"')!=-1) {
|
||||||
|
url = token.substring(token.indexOf('<')+1, token.indexOf(';')-1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Add the issue to the QueryResult object
|
||||||
|
* @param issue
|
||||||
|
* @param queryResult
|
||||||
|
*/
|
||||||
|
addToResult(issue: IssueInfo, queryResult: QueryResult) {
|
||||||
|
let issueInfo:ResultIssueInfo = new ResultIssueInfo();
|
||||||
|
issueInfo.html_url = issue.html_url;
|
||||||
|
issueInfo.title = issue.title;
|
||||||
|
issueInfo.state = issue.state;
|
||||||
|
issueInfo.age = this.getIssueAge(issue.created_at);
|
||||||
|
queryResult.items?.push(issueInfo);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Calculate the age of the issue
|
||||||
|
* i.e. take today's date and find the number of days difference from
|
||||||
|
* the issue creation date
|
||||||
|
* @param created_at
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
getIssueAge(created_at: string): number {
|
||||||
|
let todayDate: Date = new Date();
|
||||||
|
let createDate: Date = new Date(created_at);
|
||||||
|
let differenceInTime = todayDate.getTime() - createDate.getTime();
|
||||||
|
|
||||||
|
//get the difference in day
|
||||||
|
return Math.floor(differenceInTime / (1000 * 3600 * 24));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// /**
|
||||||
|
// * QueryResult
|
||||||
|
// */
|
||||||
|
// class QueryResult {
|
||||||
|
// total_count: number;
|
||||||
|
// items: ResultIssueInfo[];
|
||||||
|
// }
|
||||||
|
|
||||||
|
// class ResultIssueInfo {
|
||||||
|
// title: string;
|
||||||
|
// html_url: string;
|
||||||
|
// state: string;
|
||||||
|
// age: number;
|
||||||
|
// }
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
export * from './ping.controller';
|
||||||
|
export * from './gh-query.controller';
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
import {inject} from '@loopback/core';
|
||||||
|
import {
|
||||||
|
Request,
|
||||||
|
RestBindings,
|
||||||
|
get,
|
||||||
|
response,
|
||||||
|
ResponseObject,
|
||||||
|
} from '@loopback/rest';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* OpenAPI response for ping()
|
||||||
|
*/
|
||||||
|
const PING_RESPONSE: ResponseObject = {
|
||||||
|
description: 'Ping Response',
|
||||||
|
content: {
|
||||||
|
'application/json': {
|
||||||
|
schema: {
|
||||||
|
type: 'object',
|
||||||
|
title: 'PingResponse',
|
||||||
|
properties: {
|
||||||
|
greeting: {type: 'string'},
|
||||||
|
date: {type: 'string'},
|
||||||
|
url: {type: 'string'},
|
||||||
|
headers: {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
'Content-Type': {type: 'string'},
|
||||||
|
},
|
||||||
|
additionalProperties: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A simple controller to bounce back http requests
|
||||||
|
*/
|
||||||
|
export class PingController {
|
||||||
|
constructor(@inject(RestBindings.Http.REQUEST) private req: Request) {}
|
||||||
|
|
||||||
|
// Map to `GET /ping`
|
||||||
|
@get('/ping')
|
||||||
|
@response(200, PING_RESPONSE)
|
||||||
|
ping(): object {
|
||||||
|
// Reply with a greeting, the current time, the url, and request headers
|
||||||
|
return {
|
||||||
|
greeting: 'Hello from LoopBack',
|
||||||
|
date: new Date(),
|
||||||
|
url: this.req.url,
|
||||||
|
headers: Object.assign({}, this.req.headers),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
# Datasources
|
||||||
|
|
||||||
|
This directory contains config for datasources used by this app.
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
import {inject, lifeCycleObserver, LifeCycleObserver} from '@loopback/core';
|
||||||
|
import {juggler} from '@loopback/repository';
|
||||||
|
|
||||||
|
const config = {
|
||||||
|
name: 'githubds',
|
||||||
|
connector: 'rest',
|
||||||
|
baseURL: 'https://api.github.ibm.com',
|
||||||
|
crud: false,
|
||||||
|
options: {
|
||||||
|
headers: {
|
||||||
|
accept: 'application/json',
|
||||||
|
Authorization: process.env.TOKEN,
|
||||||
|
'User-Agent': 'loopback4-example-github',
|
||||||
|
'X-RateLimit-Limit': 5000,
|
||||||
|
'content-type': 'application/json'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
operations: [
|
||||||
|
{
|
||||||
|
template: {
|
||||||
|
method: 'GET',
|
||||||
|
fullResponse: true,
|
||||||
|
url: 'https://api.github.com/search/issues?q=repo:{repo}+label:"{label}"'
|
||||||
|
},
|
||||||
|
functions: {
|
||||||
|
getIssuesByLabel: ['repo','label']
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
template: {
|
||||||
|
method: 'GET',
|
||||||
|
fullResponse: true,
|
||||||
|
url: '{url}'
|
||||||
|
},
|
||||||
|
functions: {
|
||||||
|
getIssuesByURL: ['url']
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
template: {
|
||||||
|
method: 'GET',
|
||||||
|
fullResponse: true,
|
||||||
|
url: 'https://api.github.com/search/issues?q=repo:{repo}+{querystring}'
|
||||||
|
},
|
||||||
|
functions: {
|
||||||
|
getIssuesWithQueryString: ['repo','querystring']
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
// Observe application's life cycle to disconnect the datasource when
|
||||||
|
// application is stopped. This allows the application to be shut down
|
||||||
|
// gracefully. The `stop()` method is inherited from `juggler.DataSource`.
|
||||||
|
// Learn more at https://loopback.io/doc/en/lb4/Life-cycle.html
|
||||||
|
@lifeCycleObserver('datasource')
|
||||||
|
export class GithubdsDataSource extends juggler.DataSource
|
||||||
|
implements LifeCycleObserver {
|
||||||
|
static dataSourceName = 'githubds';
|
||||||
|
static readonly defaultConfig = config;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
@inject('datasources.config.githubds', {optional: true})
|
||||||
|
dsConfig: object = config,
|
||||||
|
) {
|
||||||
|
super(dsConfig);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
export * from './githubds.datasource';
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
import {ApplicationConfig, Loopback4ExampleGithubApplication} from './application';
|
||||||
|
|
||||||
|
export * from './application';
|
||||||
|
|
||||||
|
export async function main(options: ApplicationConfig = {}) {
|
||||||
|
// Создаем и запускаем LoopBack приложение.
|
||||||
|
const app = new Loopback4ExampleGithubApplication(options);
|
||||||
|
await app.boot();
|
||||||
|
await app.start();
|
||||||
|
|
||||||
|
const url = app.restServer.url;
|
||||||
|
console.log(`Server is running at ${url}`);
|
||||||
|
console.log(`Try ${url}/ping`);
|
||||||
|
|
||||||
|
return app;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (require.main === module) {
|
||||||
|
// Локальный запуск с базовой конфигурацией REST.
|
||||||
|
const config = {
|
||||||
|
rest: {
|
||||||
|
port: +(process.env.PORT ?? 3000),
|
||||||
|
host: process.env.HOST,
|
||||||
|
// The `gracePeriodForClose` provides a graceful close for http/https
|
||||||
|
// servers with keep-alive clients. The default value is `Infinity`
|
||||||
|
// (don't force-close). If you want to immediately destroy all sockets
|
||||||
|
// upon stop, set its value to `0`.
|
||||||
|
// See https://www.npmjs.com/package/stoppable
|
||||||
|
gracePeriodForClose: 5000, // 5 seconds
|
||||||
|
openApiSpec: {
|
||||||
|
// useful when used with OpenAPI-to-GraphQL to locate your application
|
||||||
|
setServersFromRequest: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
main(config).catch(err => {
|
||||||
|
console.error('Cannot start the application.', err);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
import {Loopback4ExampleGithubApplication} from './application';
|
||||||
|
|
||||||
|
export async function migrate(args: string[]) {
|
||||||
|
const existingSchema = args.includes('--rebuild') ? 'drop' : 'alter';
|
||||||
|
console.log('Migrating schemas (%s existing schema)', existingSchema);
|
||||||
|
|
||||||
|
const app = new Loopback4ExampleGithubApplication();
|
||||||
|
await app.boot();
|
||||||
|
await app.migrateSchema({existingSchema});
|
||||||
|
|
||||||
|
// Connectors usually keep a pool of opened connections,
|
||||||
|
// this keeps the process running even after all work is done.
|
||||||
|
// We need to exit explicitly.
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
migrate(process.argv).catch(err => {
|
||||||
|
console.error('Cannot migrate database schema', err);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
# Models
|
||||||
|
|
||||||
|
This directory contains code for models provided by this app.
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
export * from './query-result.model';
|
||||||
|
export * from './result-issue-info.model';
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
import {Model, model, property} from '@loopback/repository';
|
||||||
|
import {ResultIssueInfo} from './result-issue-info.model';
|
||||||
|
|
||||||
|
@model()
|
||||||
|
export class QueryResult extends Model {
|
||||||
|
@property({
|
||||||
|
type: 'number',
|
||||||
|
})
|
||||||
|
total_count?: number;
|
||||||
|
|
||||||
|
@property.array(ResultIssueInfo)
|
||||||
|
items?: ResultIssueInfo[];
|
||||||
|
|
||||||
|
|
||||||
|
constructor(data?: Partial<QueryResult>) {
|
||||||
|
super(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface QueryResultRelations {
|
||||||
|
// describe navigational properties here
|
||||||
|
}
|
||||||
|
|
||||||
|
export type QueryResultWithRelations = QueryResult & QueryResultRelations;
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
import {Model, model, property} from '@loopback/repository';
|
||||||
|
|
||||||
|
@model()
|
||||||
|
export class ResultIssueInfo extends Model {
|
||||||
|
@property({
|
||||||
|
type: 'string',
|
||||||
|
})
|
||||||
|
title?: string;
|
||||||
|
|
||||||
|
@property({
|
||||||
|
type: 'string',
|
||||||
|
})
|
||||||
|
html_url?: string;
|
||||||
|
|
||||||
|
@property({
|
||||||
|
type: 'string',
|
||||||
|
})
|
||||||
|
state?: string;
|
||||||
|
|
||||||
|
@property({
|
||||||
|
type: 'number',
|
||||||
|
})
|
||||||
|
age?: number;
|
||||||
|
|
||||||
|
|
||||||
|
constructor(data?: Partial<ResultIssueInfo>) {
|
||||||
|
super(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ResultIssueInfoRelations {
|
||||||
|
// describe navigational properties here
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ResultIssueInfoWithRelations = ResultIssueInfo & ResultIssueInfoRelations;
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import {ApplicationConfig} from '@loopback/core';
|
||||||
|
import {Loopback4ExampleGithubApplication} from './application';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Export the OpenAPI spec from the application
|
||||||
|
*/
|
||||||
|
async function exportOpenApiSpec(): Promise<void> {
|
||||||
|
const config: ApplicationConfig = {
|
||||||
|
rest: {
|
||||||
|
port: +(process.env.PORT ?? 3000),
|
||||||
|
host: process.env.HOST ?? 'localhost',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const outFile = process.argv[2] ?? '';
|
||||||
|
const app = new Loopback4ExampleGithubApplication(config);
|
||||||
|
await app.boot();
|
||||||
|
await app.exportOpenApiSpec(outFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
exportOpenApiSpec().catch(err => {
|
||||||
|
console.error('Fail to export OpenAPI spec from the application.', err);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
# Repositories
|
||||||
|
|
||||||
|
This directory contains code for repositories provided by this app.
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
import {MiddlewareSequence} from '@loopback/rest';
|
||||||
|
|
||||||
|
// Стандартная sequence без кастомной логики.
|
||||||
|
export class MySequence extends MiddlewareSequence {}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
import {inject, Provider} from '@loopback/core';
|
||||||
|
import {getService} from '@loopback/service-proxy';
|
||||||
|
import {GithubdsDataSource} from '../datasources';
|
||||||
|
|
||||||
|
export interface GhQueryService {
|
||||||
|
// this is where you define the Node.js methods that will be
|
||||||
|
// mapped to REST/SOAP/gRPC operations as stated in the datasource
|
||||||
|
// json file.
|
||||||
|
|
||||||
|
// Add the three methods here.
|
||||||
|
// Make sure the function names and the parameter names matches
|
||||||
|
// the ones you defined in the datasource
|
||||||
|
getIssuesByLabel(repo: string, label: string): Promise<QueryResponse>;
|
||||||
|
getIssuesByURL(url: string): Promise<QueryResponse>;
|
||||||
|
getIssuesWithQueryString(repo:string, querystring: string): Promise<QueryResponse>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface QueryResponse {
|
||||||
|
headers: any;
|
||||||
|
body: QueryResponseBody;
|
||||||
|
}
|
||||||
|
export interface QueryResponseBody {
|
||||||
|
total_count: number;
|
||||||
|
items: IssueInfo[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export class IssueInfo {
|
||||||
|
title: string;
|
||||||
|
html_url: string;
|
||||||
|
state: string;
|
||||||
|
created_at: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export class GhQueryServiceProvider implements Provider<GhQueryService> {
|
||||||
|
constructor(
|
||||||
|
// githubds must match the name property in the datasource json file
|
||||||
|
@inject('datasources.githubds')
|
||||||
|
protected dataSource: GithubdsDataSource = new GithubdsDataSource(),
|
||||||
|
) {}
|
||||||
|
|
||||||
|
value(): Promise<GhQueryService> {
|
||||||
|
return getService(this.dataSource);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
export * from './gh-query-service.service';
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"$schema": "http://json.schemastore.org/tsconfig",
|
||||||
|
"extends": "@loopback/build/config/tsconfig.common.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"outDir": "dist",
|
||||||
|
"rootDir": "src"
|
||||||
|
},
|
||||||
|
"include": ["src"]
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
// Фиксируем провайдер и версию, чтобы поведение было стабильным.
|
||||||
|
terraform {
|
||||||
|
required_providers {
|
||||||
|
nubes = {
|
||||||
|
source = "terra.k8c.ru/nubes/nubes"
|
||||||
|
version = "2.1.10"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Провайдер Nubes для создания ресурсов.
|
||||||
|
provider "nubes" {
|
||||||
|
api_token = var.api_token
|
||||||
|
api_endpoint = "https://deck-api.ngcloud.ru/api/v1/index.cfm"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Токен доступа к Nubes API.
|
||||||
|
variable "api_token" {
|
||||||
|
type = string
|
||||||
|
sensitive = true
|
||||||
|
description = "Nubes API token"
|
||||||
|
}
|
||||||
@@ -0,0 +1,209 @@
|
|||||||
|
<cfcomponent displayname="Application" output="true">
|
||||||
|
<!--- Базовая конфигурация приложения и подключение JAR-клиента RabbitMQ. --->
|
||||||
|
<cfset this.Name = "rabbit-lsd-ui" />
|
||||||
|
<cfset this.sessionmanagement = "Yes" />
|
||||||
|
<cfset this.datasource = "testds" />
|
||||||
|
<cfset this.javaSettings = { loadPaths = [ expandPath("./lib") ] } />
|
||||||
|
<!--- Инициализируем datasource из переменных окружения. --->
|
||||||
|
<cfset getDS(this.datasource) />
|
||||||
|
|
||||||
|
<!--- Безопасное чтение переменной окружения с дефолтом. --->
|
||||||
|
<cffunction name="getEnv" access="private" returntype="string">
|
||||||
|
<cfargument name="key" type="string" required="true" />
|
||||||
|
<cfargument name="def" type="string" required="true" />
|
||||||
|
<cfset var system = createObject("java", "java.lang.System") />
|
||||||
|
<cfset var val = system.getEnv(arguments.key) />
|
||||||
|
<cfif isDefined("val") AND len(val)><cfreturn val /></cfif>
|
||||||
|
<cfreturn arguments.def />
|
||||||
|
</cffunction>
|
||||||
|
|
||||||
|
<!--- Собираем datasource из набора *_field переменных окружения. --->
|
||||||
|
<cffunction name="getDS" access="private" returntype="void">
|
||||||
|
<cfargument name="dsname" type="string" required="true" />
|
||||||
|
<cfset var system = createObject("java", "java.lang.System") />
|
||||||
|
<cfset var ds = {} />
|
||||||
|
<cfloop list="class,connectionString,database,driver,host,port,type,url,username,password,bundleName,bundleVersion,connectionLimit,liveTimeout,validate" item="field">
|
||||||
|
<cfset var envVal = system.getEnv("#arguments.dsname#_#field#") />
|
||||||
|
<cfif isDefined("envVal") AND len(envVal)><cfset ds[field] = envVal /></cfif>
|
||||||
|
</cfloop>
|
||||||
|
<cfset this.datasources[arguments.dsname] = ds />
|
||||||
|
</cffunction>
|
||||||
|
|
||||||
|
<!--- Публикация CRUD-сообщения в RabbitMQ. --->
|
||||||
|
<cffunction name="publishToRabbit" access="private" returntype="void">
|
||||||
|
<cfargument name="payload" type="struct" required="true" />
|
||||||
|
<cfset var host = getEnv("RABBIT_HOST", "") />
|
||||||
|
<cfset var user = getEnv("RABBIT_USER", "") />
|
||||||
|
<cfset var pass = getEnv("RABBIT_PASSWORD", "") />
|
||||||
|
<cfset var vhost = getEnv("RABBIT_VHOST", "/") />
|
||||||
|
<cfset var port = getEnv("RABBIT_PORT", "5672") />
|
||||||
|
<cfset var queueList = listToArray(getEnv("RABBIT_QUEUES", "crud_queue")) />
|
||||||
|
<cfset var queueName = trim(queueList[1]) />
|
||||||
|
<cfset var durable = lcase(getEnv("RABBIT_DURABLE", "true")) EQ "true" />
|
||||||
|
<cfset var factory = createObject("java", "com.rabbitmq.client.ConnectionFactory") />
|
||||||
|
<cfset var connection = "" />
|
||||||
|
<cfset var channel = "" />
|
||||||
|
|
||||||
|
<!--- Не стартуем публикацию без базовых кредов. --->
|
||||||
|
<cfif host EQ "" OR user EQ "" OR pass EQ "">
|
||||||
|
<cfthrow message="Rabbit credentials are missing" />
|
||||||
|
</cfif>
|
||||||
|
|
||||||
|
<cfset factory.setHost(host) />
|
||||||
|
<cfset factory.setPort(JavaCast("int", port)) />
|
||||||
|
<cfset factory.setUsername(user) />
|
||||||
|
<cfset factory.setPassword(pass) />
|
||||||
|
<cfset factory.setVirtualHost(vhost) />
|
||||||
|
|
||||||
|
<!--- Подключаемся, объявляем очередь и отправляем JSON. --->
|
||||||
|
<cftry>
|
||||||
|
<cfset connection = factory.newConnection() />
|
||||||
|
<cfset channel = connection.createChannel() />
|
||||||
|
<cfset channel.queueDeclare(queueName, durable, false, false, JavaCast("null", "")) />
|
||||||
|
<cfset var payloadJson = serializeJSON(arguments.payload) />
|
||||||
|
<cfset var body = createObject("java", "java.lang.String").init(payloadJson).getBytes("UTF-8") />
|
||||||
|
<cfset channel.basicPublish("", queueName, JavaCast("null", ""), body) />
|
||||||
|
<cfif isDefined("channel")><cfset channel.close() /></cfif>
|
||||||
|
<cfif isDefined("connection")><cfset connection.close() /></cfif>
|
||||||
|
<cfcatch>
|
||||||
|
<cfif isDefined("channel")><cfset channel.close() /></cfif>
|
||||||
|
<cfif isDefined("connection")><cfset connection.close() /></cfif>
|
||||||
|
<cfthrow message="#cfcatch.message#" />
|
||||||
|
</cfcatch>
|
||||||
|
</cftry>
|
||||||
|
</cffunction>
|
||||||
|
|
||||||
|
<!--- Основной обработчик запроса: подготовка БД, логов и POST-операций. --->
|
||||||
|
<cffunction name="OnRequest" access="public" returntype="void" output="true">
|
||||||
|
<cfargument name="template" type="string" required="true" />
|
||||||
|
<cfset request.DS = this.datasource />
|
||||||
|
<cfset request.tableName = getEnv("PG_TABLE", "rabbit_messages") />
|
||||||
|
<cfset request.logTableName = getEnv("UI_LOG_TABLE", "rabbit_ui_log") />
|
||||||
|
<cfset request.rabbitAdminUrl = getEnv("RABBIT_ADMIN_URL", "") />
|
||||||
|
<cfset request.rabbitUser = getEnv("RABBIT_USER", "") />
|
||||||
|
<cfset request.rabbitPassword = getEnv("RABBIT_PASSWORD", "") />
|
||||||
|
<cfset request.nodeworkerUrl = getEnv("NODEWORKER_URL", "") />
|
||||||
|
|
||||||
|
<!--- Гарантируем наличие основной таблицы сообщений. --->
|
||||||
|
<cftry>
|
||||||
|
<cfquery datasource="#request.DS#">
|
||||||
|
CREATE TABLE IF NOT EXISTS #request.tableName# (id SERIAL PRIMARY KEY, test_data TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)
|
||||||
|
</cfquery>
|
||||||
|
<cfcatch><cfset request.db_error = cfcatch.message /></cfcatch>
|
||||||
|
</cftry>
|
||||||
|
|
||||||
|
<!--- Гарантируем наличие таблицы лога UI и обновляем статусы. --->
|
||||||
|
<cftry>
|
||||||
|
<cfquery datasource="#request.DS#">
|
||||||
|
CREATE TABLE IF NOT EXISTS #request.logTableName# (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
action TEXT NOT NULL,
|
||||||
|
text TEXT,
|
||||||
|
request_id TEXT,
|
||||||
|
target_id INTEGER,
|
||||||
|
queued_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
processed_at TIMESTAMP,
|
||||||
|
status TEXT DEFAULT 'queued',
|
||||||
|
error_reason TEXT
|
||||||
|
)
|
||||||
|
</cfquery>
|
||||||
|
<cfquery datasource="#request.DS#">
|
||||||
|
ALTER TABLE #request.logTableName# ADD COLUMN IF NOT EXISTS error_reason TEXT
|
||||||
|
</cfquery>
|
||||||
|
<cfquery datasource="#request.DS#">
|
||||||
|
UPDATE #request.logTableName#
|
||||||
|
SET processed_at = NOW(), status = 'done'
|
||||||
|
WHERE status = 'queued'
|
||||||
|
AND action IN ('create','update')
|
||||||
|
AND request_id IS NOT NULL
|
||||||
|
AND EXISTS (
|
||||||
|
SELECT 1 FROM #request.tableName#
|
||||||
|
WHERE test_data LIKE '%' || '[req:' || request_id || ']%'
|
||||||
|
)
|
||||||
|
</cfquery>
|
||||||
|
<cfquery datasource="#request.DS#">
|
||||||
|
UPDATE #request.logTableName#
|
||||||
|
SET processed_at = NOW(), status = 'done'
|
||||||
|
WHERE status = 'queued'
|
||||||
|
AND action = 'delete'
|
||||||
|
AND target_id IS NOT NULL
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT 1 FROM #request.tableName# WHERE id = target_id
|
||||||
|
)
|
||||||
|
</cfquery>
|
||||||
|
<cfquery datasource="#request.DS#">
|
||||||
|
UPDATE #request.logTableName#
|
||||||
|
SET status = 'failed', error_reason = 'target not found'
|
||||||
|
WHERE status = 'queued'
|
||||||
|
AND action = 'update'
|
||||||
|
AND target_id IS NOT NULL
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT 1 FROM #request.tableName# WHERE id = target_id
|
||||||
|
)
|
||||||
|
</cfquery>
|
||||||
|
<cfcatch><cfset request.db_error = cfcatch.message /></cfcatch>
|
||||||
|
</cftry>
|
||||||
|
|
||||||
|
<!--- Обрабатываем POST CRUD и отправляем событие в очередь. --->
|
||||||
|
<cfif CGI.REQUEST_METHOD EQ "POST" AND structKeyExists(form, "crud_action")>
|
||||||
|
<cfset var action = lcase(form.crud_action) />
|
||||||
|
<cfset var requestId = createUUID() />
|
||||||
|
<cfset var payload = { "request_id" = requestId } />
|
||||||
|
<cfset var logText = "" />
|
||||||
|
<cfset var logTargetId = "" />
|
||||||
|
|
||||||
|
<!--- Формируем payload и лог для очереди. --->
|
||||||
|
<cftry>
|
||||||
|
<cfswitch expression="#action#">
|
||||||
|
<cfcase value="insert">
|
||||||
|
<cfset payload.action = "create" />
|
||||||
|
<cfset logText = trim(form.txt_content) />
|
||||||
|
<cfset payload.text = logText & " [req:" & requestId & "]" />
|
||||||
|
</cfcase>
|
||||||
|
<cfcase value="update">
|
||||||
|
<cfif NOT structKeyExists(form, "id")>
|
||||||
|
<cfthrow message="Missing id for update" />
|
||||||
|
</cfif>
|
||||||
|
<cfset payload.action = "update" />
|
||||||
|
<cfset payload.id = val(form.id) />
|
||||||
|
<cfset logText = trim(form.txt_content) />
|
||||||
|
<cfset logTargetId = toString(val(form.id)) />
|
||||||
|
<cfset payload.text = logText & " [req:" & requestId & "]" />
|
||||||
|
</cfcase>
|
||||||
|
<cfcase value="delete">
|
||||||
|
<cfif NOT structKeyExists(form, "id")>
|
||||||
|
<cfthrow message="Missing id for delete" />
|
||||||
|
</cfif>
|
||||||
|
<cfset payload.action = "delete" />
|
||||||
|
<cfset payload.id = val(form.id) />
|
||||||
|
<cfset logTargetId = toString(val(form.id)) />
|
||||||
|
<cfset logText = "delete id " & logTargetId />
|
||||||
|
</cfcase>
|
||||||
|
<cfdefaultcase>
|
||||||
|
<cfthrow message="Unknown action" />
|
||||||
|
</cfdefaultcase>
|
||||||
|
</cfswitch>
|
||||||
|
|
||||||
|
<!--- Публикуем сообщение и пишем запись в лог UI. --->
|
||||||
|
<cfset publishToRabbit(payload) />
|
||||||
|
<cfquery datasource="#request.DS#">
|
||||||
|
INSERT INTO #request.logTableName# (action, text, request_id, target_id, queued_at, status)
|
||||||
|
VALUES (
|
||||||
|
<cfqueryparam value="#payload.action#" cfsqltype="cf_sql_varchar">,
|
||||||
|
<cfqueryparam value="#logText#" cfsqltype="cf_sql_varchar">,
|
||||||
|
<cfqueryparam value="#requestId#" cfsqltype="cf_sql_varchar">,
|
||||||
|
<cfqueryparam value="#logTargetId#" cfsqltype="cf_sql_integer" null="#NOT len(logTargetId)#">,
|
||||||
|
NOW(),
|
||||||
|
'queued'
|
||||||
|
)
|
||||||
|
</cfquery>
|
||||||
|
<cflocation url="#CGI.SCRIPT_NAME#" addtoken="false">
|
||||||
|
<cfcatch>
|
||||||
|
<cfset request.queue_error = cfcatch.message />
|
||||||
|
</cfcatch>
|
||||||
|
</cftry>
|
||||||
|
</cfif>
|
||||||
|
|
||||||
|
<cfinclude template="#arguments.template#" />
|
||||||
|
</cffunction>
|
||||||
|
</cfcomponent>
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
# Lucee UI for Rabbit CRUD
|
||||||
|
|
||||||
|
UI publishes CRUD actions to RabbitMQ and reads results from Postgres.
|
||||||
|
|
||||||
|
Коротко: интерфейс пишет в RabbitMQ, а фактическое состояние берет из Postgres, чтобы показать разницу между "поставлено в очередь" и "исполнено".
|
||||||
|
|
||||||
|
## Behavior
|
||||||
|
|
||||||
|
- Insert/Update/Delete actions are queued via RabbitMQ.
|
||||||
|
- A status banner shows when the action was queued and when it appears in Postgres.
|
||||||
|
- The UI reads the latest rows from the table defined by `PG_TABLE`.
|
||||||
|
|
||||||
|
## Environment variables
|
||||||
|
|
||||||
|
Lucee datasource (required):
|
||||||
|
- `testds_connectionString`
|
||||||
|
- `testds_username`
|
||||||
|
- `testds_password`
|
||||||
|
- `testds_class` (org.postgresql.Driver)
|
||||||
|
- `testds_bundleName` (org.postgresql.jdbc)
|
||||||
|
- `testds_bundleVersion` (42.6.0)
|
||||||
|
- `testds_connectionLimit` (5)
|
||||||
|
- `testds_liveTimeout` (15)
|
||||||
|
- `testds_validate` (false)
|
||||||
|
|
||||||
|
RabbitMQ (required):
|
||||||
|
- `RABBIT_HOST`
|
||||||
|
- `RABBIT_PORT` (default 5672)
|
||||||
|
- `RABBIT_USER`
|
||||||
|
- `RABBIT_PASSWORD`
|
||||||
|
- `RABBIT_VHOST` (default `/`)
|
||||||
|
- `RABBIT_QUEUES` (default `crud_queue`)
|
||||||
|
- `RABBIT_DURABLE` (default `true`)
|
||||||
|
|
||||||
|
Optional:
|
||||||
|
- `PG_TABLE` (default `rabbit_messages`)
|
||||||
|
|
||||||
|
Optional:
|
||||||
|
- `UI_LOG_TABLE` (default `rabbit_ui_log`) — таблица лога для статуса запросов UI.
|
||||||
|
|
||||||
|
## Notes
|
||||||
|
|
||||||
|
This app uses RabbitMQ Java client JARs located in `lib/`.
|
||||||
|
|
||||||
|
Комментарий: JAR-ы подключены через `this.javaSettings.loadPaths`, поэтому важно держать `lib/` рядом с приложением.
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
<!--- Простой редирект на основной экран UI. --->
|
||||||
|
<cflocation addtoken="No" url="query.cfm##q"/>
|
||||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,219 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="ru">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Nubes | Rabbit CRUD UI</title>
|
||||||
|
<link rel="icon" href="https://nubes.ru/themes/custom/nubes/images/nubes-ico.svg" type="image/svg+xml">
|
||||||
|
<style>
|
||||||
|
:root { --nubes-blue: #005BFF; --nubes-dark: #1A1A1A; --nubes-grey: #F8F9FA; --nubes-border: #E5E7EB; --nubes-green: #0f9d58; --nubes-red: #d93025; }
|
||||||
|
body { font-family: 'Segoe UI', Tahoma, sans-serif; margin: 0; padding: 0; background: var(--nubes-grey); color: var(--nubes-dark); }
|
||||||
|
.header-bg { position: sticky; top: 0; z-index: 1000; background: #fff; border-bottom: 1px solid var(--nubes-border); padding: 15px 0; box-shadow: 0 2px 10px rgba(0,0,0,0.05); }
|
||||||
|
.container { max-width: 1100px; margin: auto; padding: 0 20px; }
|
||||||
|
.header-content { display: flex; align-items: center; justify-content: space-between; }
|
||||||
|
.logo { height: 40px; }
|
||||||
|
.main-content { padding: 32px 0; }
|
||||||
|
.card { background: #fff; padding: 24px; border-radius: 16px; box-shadow: 0 4px 20px rgba(0,0,0,0.04); margin-bottom: 20px; }
|
||||||
|
h3 { margin: 0 0 12px 0; }
|
||||||
|
.btn { display: inline-flex; align-items: center; justify-content: center; cursor: pointer; padding: 12px 24px; border: none; border-radius: 8px; font-weight: 600; font-size: 14px; }
|
||||||
|
.btn-primary { background: var(--nubes-blue); color: #fff; }
|
||||||
|
.btn-action { padding: 6px; background: #fff; border: 1px solid var(--nubes-border); border-radius: 8px; font-size: 16px; line-height: 1; cursor: pointer; min-width: 32px; }
|
||||||
|
.btn-action:hover { background: var(--nubes-grey); border-color: var(--nubes-blue); }
|
||||||
|
.input-group { display: flex; gap: 12px; margin-bottom: 12px; }
|
||||||
|
input[type="text"] { flex-grow: 1; padding: 12px 16px; border: 1px solid var(--nubes-border); border-radius: 8px; font-size: 14px; }
|
||||||
|
table { width: 100%; border-collapse: collapse; }
|
||||||
|
th { text-align: left; padding: 8px 10px; font-size: 11px; text-transform: uppercase; color: #6B7280; border-bottom: 1px solid var(--nubes-border); }
|
||||||
|
td { padding: 8px 10px; font-size: 12px; border-bottom: 1px solid var(--nubes-border); vertical-align: top; }
|
||||||
|
tbody tr:nth-child(even) { background-color: #FAFBFC; }
|
||||||
|
tbody tr:hover { background-color: #F3F4F6; }
|
||||||
|
.id-cell { font-family: monospace; color: #9CA3AF; width: 60px; }
|
||||||
|
.actions-cell { display: flex; gap: 10px; width: 120px; }
|
||||||
|
.alert { padding: 12px 16px; border-radius: 10px; margin-bottom: 16px; font-size: 14px; }
|
||||||
|
.alert-info { background: #eef4ff; color: #234; border: 1px solid #d6e3ff; }
|
||||||
|
.alert-success { background: #eef8f1; color: #1e4620; border: 1px solid #cbe6d3; }
|
||||||
|
.alert-error { background: #fdeeee; color: var(--nubes-red); border: 1px solid #f4c7c3; }
|
||||||
|
.badge { display: inline-block; padding: 4px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; }
|
||||||
|
.badge-queued { background: #eef4ff; color: #1d3b8b; }
|
||||||
|
.badge-done { background: #eef8f1; color: #1e4620; }
|
||||||
|
.small { font-size: 12px; color: #6B7280; }
|
||||||
|
.top-info { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
|
||||||
|
.link-row { display: flex; gap: 10px; align-items: center; }
|
||||||
|
.link-row a { color: var(--nubes-blue); text-decoration: none; font-weight: 600; }
|
||||||
|
.split { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
|
||||||
|
.tight { font-size: 12px; }
|
||||||
|
.table-scroll { overflow-y: auto; display: block; }
|
||||||
|
.table-scroll-queue { max-height: 220px; }
|
||||||
|
.table-scroll-db { max-height: 580px; }
|
||||||
|
.table-scroll table { margin: 0; }
|
||||||
|
.col-queued { width: 120px; }
|
||||||
|
.col-processed { width: 120px; }
|
||||||
|
.col-status { width: 90px; }
|
||||||
|
.col-id { width: 60px; }
|
||||||
|
.col-content { width: 100%; }
|
||||||
|
.col-req { width: 220px; }
|
||||||
|
.wide { grid-column: 1 / -1; }
|
||||||
|
@media (max-width: 980px) {
|
||||||
|
.split { grid-template-columns: 1fr; }
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<!--- Сводный статус последней операции и базовые ссылки. --->
|
||||||
|
<cfset status = {}>
|
||||||
|
<cfset rabbitAdminUrl = request.rabbitAdminUrl>
|
||||||
|
<cfset rabbitUser = request.rabbitUser>
|
||||||
|
<cfset rabbitPassword = request.rabbitPassword>
|
||||||
|
<cfset nodeworkerUrl = request.nodeworkerUrl>
|
||||||
|
|
||||||
|
<!--- Лог последних событий из UI-очереди. --->
|
||||||
|
<cfquery name="qLog" datasource="#request.DS#">
|
||||||
|
SELECT id, action, text, request_id, target_id, queued_at, processed_at, status, error_reason
|
||||||
|
FROM #request.logTableName#
|
||||||
|
ORDER BY id DESC
|
||||||
|
</cfquery>
|
||||||
|
|
||||||
|
<!--- Формируем сообщение статуса из лога/ошибок. --->
|
||||||
|
<cfif structKeyExists(request, "queue_error")>
|
||||||
|
<cfset status.type = "error">
|
||||||
|
<cfset status.message = "Ошибка постановки в очередь: " & request.queue_error>
|
||||||
|
<cfelseif qLog.recordCount GT 0>
|
||||||
|
<cfset status.type = qLog.status[1] EQ "done" ? "success" : "info">
|
||||||
|
<cfif qLog.status[1] EQ "done">
|
||||||
|
<cfset status.message = "Исполнено в " & dateTimeFormat(qLog.processed_at[1], "yyyy-mm-dd HH:nn:ss")>
|
||||||
|
<cfelse>
|
||||||
|
<cfset status.message = "Поставлено в очередь: " & uCase(qLog.action[1]) & " в " & dateTimeFormat(qLog.queued_at[1], "yyyy-mm-dd HH:nn:ss")>
|
||||||
|
</cfif>
|
||||||
|
</cfif>
|
||||||
|
|
||||||
|
<!--- Фиксированный заголовок Nubes. --->
|
||||||
|
<div class="header-bg">
|
||||||
|
<div class="container header-content">
|
||||||
|
<img src="https://nubes.ru/themes/custom/nubes_2025/logo.svg" alt="Nubes" class="logo">
|
||||||
|
<div style="font-size: 14px; color: var(--nubes-blue); font-weight: 600;">Lucee + Rabbit + Postgres Demo</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="container main-content">
|
||||||
|
<!--- Карточка с ссылками и кредами Rabbit/worker. --->
|
||||||
|
<div class="card">
|
||||||
|
<div class="top-info">
|
||||||
|
<div>
|
||||||
|
<div class="small">RabbitMQ UI</div>
|
||||||
|
<div class="link-row">
|
||||||
|
<cfif len(rabbitAdminUrl)>
|
||||||
|
<a href="<cfoutput>#rabbitAdminUrl#</cfoutput>" target="_blank">Открыть Rabbit UI</a>
|
||||||
|
<cfelse>
|
||||||
|
<span class="small">URL не задан</span>
|
||||||
|
</cfif>
|
||||||
|
</div>
|
||||||
|
<div class="small">Логин / Пароль</div>
|
||||||
|
<div><cfoutput>#rabbitUser#</cfoutput> / <cfoutput>#rabbitPassword#</cfoutput></div>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<div class="small">NodeJS Worker</div>
|
||||||
|
<div class="link-row">
|
||||||
|
<cfif len(nodeworkerUrl)>
|
||||||
|
<a href="<cfoutput>#nodeworkerUrl#</cfoutput>" target="_blank">Открыть NodeJS Worker</a>
|
||||||
|
<cfelse>
|
||||||
|
<span class="small">URL не задан</span>
|
||||||
|
</cfif>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!--- Карточка действий CRUD. --->
|
||||||
|
<div class="card">
|
||||||
|
<cfif structKeyExists(request, "db_error")>
|
||||||
|
<div class="alert alert-error">Ошибка базы данных: <cfoutput>#request.db_error#</cfoutput></div>
|
||||||
|
</cfif>
|
||||||
|
<cfif structKeyExists(status, "message")>
|
||||||
|
<div class="alert <cfoutput>#status.type EQ 'success' ? 'alert-success' : (status.type EQ 'error' ? 'alert-error' : 'alert-info')#</cfoutput>">
|
||||||
|
<cfoutput>#status.message#</cfoutput>
|
||||||
|
</div>
|
||||||
|
</cfif>
|
||||||
|
|
||||||
|
<form method="post" class="input-group">
|
||||||
|
<input type="hidden" name="crud_action" value="insert">
|
||||||
|
<input type="text" name="txt_content" placeholder="Новое сообщение..." required>
|
||||||
|
<button type="submit" class="btn btn-primary">Поставить в очередь</button>
|
||||||
|
</form>
|
||||||
|
<div class="small">💾 — сохранить (update), 🗑 — удалить</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!--- Две таблицы: очередь/лог и фактическая БД. --->
|
||||||
|
<div class="split">
|
||||||
|
<div class="card">
|
||||||
|
<h3>Очередь (лог отправки)</h3>
|
||||||
|
<div class="table-scroll table-scroll-queue">
|
||||||
|
<table class="tight">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>ID</th>
|
||||||
|
<th>Действие</th>
|
||||||
|
<th>Текст</th>
|
||||||
|
<th>Target</th>
|
||||||
|
<th class="col-queued">Queued</th>
|
||||||
|
<th class="col-processed">Processed</th>
|
||||||
|
<th class="col-status">Статус</th>
|
||||||
|
<th>Причина</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<cfoutput query="qLog">
|
||||||
|
<tr>
|
||||||
|
<td class="id-cell">#id#</td>
|
||||||
|
<td>#uCase(action)#</td>
|
||||||
|
<td>#HTMLEditFormat(text)#</td>
|
||||||
|
<td><cfif len(target_id)>#target_id#<cfelse>-</cfif></td>
|
||||||
|
<td>#dateTimeFormat(queued_at, "yyyy-mm-dd HH:nn:ss")#</td>
|
||||||
|
<td><cfif len(processed_at)>#dateTimeFormat(processed_at, "yyyy-mm-dd HH:nn:ss")#<cfelse>-</cfif></td>
|
||||||
|
<td>
|
||||||
|
<span class="badge <cfif status EQ 'done'>badge-done<cfelse>badge-queued</cfif>">#status#</span>
|
||||||
|
</td>
|
||||||
|
<td><cfif len(error_reason)>#HTMLEditFormat(error_reason)#<cfelse>-</cfif></td>
|
||||||
|
</tr>
|
||||||
|
</cfoutput>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="card wide">
|
||||||
|
<h3>База (фактические записи)</h3>
|
||||||
|
<!--- Читаем последние фактические записи для сравнения с логом. --->
|
||||||
|
<cfquery name="qGet" datasource="#request.DS#">SELECT * FROM #request.tableName# ORDER BY id DESC</cfquery>
|
||||||
|
<div class="table-scroll table-scroll-db">
|
||||||
|
<table class="tight">
|
||||||
|
<thead><tr><th class="col-id">ID</th><th class="col-content">Содержимое</th><th class="col-req">Request ID</th><th>Действия</th></tr></thead>
|
||||||
|
<tbody>
|
||||||
|
<cfoutput query="qGet">
|
||||||
|
<!--- Отделяем request_id из хвоста строки. --->
|
||||||
|
<cfset reqMatch = reFindNoCase("\[req:[0-9a-f-]+\]$", test_data, 1, true)>
|
||||||
|
<cfset reqTag = reqMatch.pos[1] GT 0 ? mid(test_data, reqMatch.pos[1], reqMatch.len[1]) : "">
|
||||||
|
<cfset cleanText = reReplace(test_data, "(?i)\s*\[req:[0-9a-f-]+\]$", "", "all")>
|
||||||
|
<cfset reqId = reReplace(reqTag, "(?i)\[req:([0-9a-f-]+)\]", "\1", "all")>
|
||||||
|
<tr>
|
||||||
|
<td class="id-cell col-id">#id#</td>
|
||||||
|
<td>
|
||||||
|
<form method="post" id="upd_#id#" style="margin:0">
|
||||||
|
<input type="hidden" name="crud_action" value="update"><input type="hidden" name="id" value="#id#">
|
||||||
|
<input type="text" name="txt_content" value="#HTMLEditFormat(cleanText)#" style="width:100%; border:none; background:transparent;">
|
||||||
|
</form>
|
||||||
|
</td>
|
||||||
|
<td class="col-req"><cfif len(reqId)>#reqId#<cfelse>-</cfif></td>
|
||||||
|
<td class="actions-cell">
|
||||||
|
<button type="submit" form="upd_#id#" class="btn-action">💾</button>
|
||||||
|
<form method="post" style="margin:0" onsubmit="return confirm('Удалить?')">
|
||||||
|
<input type="hidden" name="crud_action" value="delete"><input type="hidden" name="id" value="#id#">
|
||||||
|
<button type="submit" class="btn-action">🗑</button>
|
||||||
|
</form>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</cfoutput>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
node_modules/
|
||||||
|
.npmrc
|
||||||
|
.env
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
# Rabbit NodeJS Worker
|
||||||
|
|
||||||
|
Consumes CRUD messages from RabbitMQ and writes to Postgres.
|
||||||
|
|
||||||
|
Комментарий: воркер специально пишет в таблицу демо и не хранит собственное состояние, кроме метрик в памяти.
|
||||||
|
|
||||||
|
## Environment variables
|
||||||
|
|
||||||
|
RabbitMQ:
|
||||||
|
- `AMQP_URL` (preferred), example: `amqp://user:pass@host:5672/vhost`
|
||||||
|
- or `RABBIT_HOST`, `RABBIT_PORT` (default 5672), `RABBIT_USER`, `RABBIT_PASSWORD`, `RABBIT_VHOST` (default `/`)
|
||||||
|
- `RABBIT_QUEUES` (default `crud_queue`)
|
||||||
|
- `RABBIT_DURABLE` (default `true`)
|
||||||
|
- `RABBIT_PREFETCH` (default `1`)
|
||||||
|
- `REQUEUE_ON_ERROR` (default `true`)
|
||||||
|
|
||||||
|
Postgres:
|
||||||
|
- `DATABASE_URL` (optional)
|
||||||
|
- or `PGHOST`, `PGPORT` (default 5432), `PGUSER`, `PGPASSWORD`, `PGDATABASE` (default `postgres`), `PGSSLMODE` (default `require`)
|
||||||
|
- `PG_TABLE` (default `rabbit_messages`)
|
||||||
|
|
||||||
|
## Notes
|
||||||
|
|
||||||
|
Health endpoint: `/healthz`.
|
||||||
|
|
||||||
|
HTML-страница со статусом доступна на `/`.
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"name": "rabbit-nodeworker",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"description": "RabbitMQ worker for CRUD demo",
|
||||||
|
"main": "server.js",
|
||||||
|
"scripts": {
|
||||||
|
"start": "node server.js"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"amqplib": "^0.10.4",
|
||||||
|
"pg": "^8.12.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,264 @@
|
|||||||
|
const http = require("http");
|
||||||
|
const amqp = require("amqplib");
|
||||||
|
const { Pool } = require("pg");
|
||||||
|
|
||||||
|
// Состояние процесса для /healthz и статуса в UI.
|
||||||
|
const status = {
|
||||||
|
state: "starting",
|
||||||
|
error: "",
|
||||||
|
lastSuccess: "",
|
||||||
|
};
|
||||||
|
|
||||||
|
// Метрики обработки сообщений (показываются на HTML-странице).
|
||||||
|
const metrics = {
|
||||||
|
processed: 0,
|
||||||
|
failed: 0,
|
||||||
|
lastError: "",
|
||||||
|
lastMessageAt: "",
|
||||||
|
lastAction: "",
|
||||||
|
};
|
||||||
|
|
||||||
|
// Унифицированное обновление состояния.
|
||||||
|
function setStatus(state, error) {
|
||||||
|
status.state = state;
|
||||||
|
status.error = error || "";
|
||||||
|
if (!error) {
|
||||||
|
status.lastSuccess = new Date().toISOString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// HTML-страница со статусом и счетчиками.
|
||||||
|
function renderStatusPage() {
|
||||||
|
const queueName = (process.env.RABBIT_QUEUES || "crud_queue").split(",")[0] || "crud_queue";
|
||||||
|
const tableName = process.env.PG_TABLE || "rabbit_messages";
|
||||||
|
return `<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>Rabbit NodeJS Worker</title>
|
||||||
|
<style>
|
||||||
|
body { font-family: Arial, sans-serif; background: #f6f7fb; color: #1c1c1c; margin: 0; }
|
||||||
|
.wrap { max-width: 900px; margin: 40px auto; padding: 0 20px; }
|
||||||
|
.card { background: #fff; border-radius: 14px; box-shadow: 0 8px 24px rgba(0,0,0,0.06); padding: 24px; }
|
||||||
|
.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
|
||||||
|
.stat { padding: 16px; border: 1px solid #e5e7eb; border-radius: 12px; background: #fafbff; }
|
||||||
|
.label { color: #6b7280; font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; }
|
||||||
|
.value { font-size: 20px; font-weight: 700; margin-top: 6px; }
|
||||||
|
.small { font-size: 12px; color: #6b7280; margin-top: 6px; }
|
||||||
|
.status { padding: 6px 12px; border-radius: 999px; display: inline-block; font-weight: 700; }
|
||||||
|
.ok { background: #e7f6ee; color: #1e4620; }
|
||||||
|
.bad { background: #fdeeee; color: #b42318; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="wrap">
|
||||||
|
<div class="card">
|
||||||
|
<h2>NodeJS Worker</h2>
|
||||||
|
<div class="small">Queue: ${queueName} | Table: ${tableName}</div>
|
||||||
|
<div style="margin: 12px 0;">
|
||||||
|
<span class="status ${status.state === "running" ? "ok" : "bad"}">${status.state}</span>
|
||||||
|
</div>
|
||||||
|
<div class="grid">
|
||||||
|
<div class="stat">
|
||||||
|
<div class="label">Processed</div>
|
||||||
|
<div class="value">${metrics.processed}</div>
|
||||||
|
</div>
|
||||||
|
<div class="stat">
|
||||||
|
<div class="label">Failed</div>
|
||||||
|
<div class="value">${metrics.failed}</div>
|
||||||
|
</div>
|
||||||
|
<div class="stat">
|
||||||
|
<div class="label">Last Action</div>
|
||||||
|
<div class="value">${metrics.lastAction || "-"}</div>
|
||||||
|
<div class="small">${metrics.lastMessageAt || ""}</div>
|
||||||
|
</div>
|
||||||
|
<div class="stat">
|
||||||
|
<div class="label">Last Error</div>
|
||||||
|
<div class="value">${metrics.lastError || "-"}</div>
|
||||||
|
<div class="small">${status.lastSuccess || ""}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Подготовка конфигурации PG (DATABASE_URL имеет приоритет).
|
||||||
|
function buildPgConfig() {
|
||||||
|
if (process.env.DATABASE_URL) {
|
||||||
|
return {
|
||||||
|
connectionString: process.env.DATABASE_URL,
|
||||||
|
ssl: process.env.PGSSLMODE === "require" ? { rejectUnauthorized: false } : undefined,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
host: process.env.PGHOST,
|
||||||
|
port: Number(process.env.PGPORT || "5432"),
|
||||||
|
user: process.env.PGUSER,
|
||||||
|
password: process.env.PGPASSWORD,
|
||||||
|
database: process.env.PGDATABASE || "postgres",
|
||||||
|
ssl: process.env.PGSSLMODE === "require" ? { rejectUnauthorized: false } : undefined,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Собираем AMQP URL из переменных окружения.
|
||||||
|
function buildAmqpUrl() {
|
||||||
|
if (process.env.AMQP_URL) {
|
||||||
|
return process.env.AMQP_URL;
|
||||||
|
}
|
||||||
|
const host = process.env.RABBIT_HOST || "";
|
||||||
|
const port = process.env.RABBIT_PORT || "5672";
|
||||||
|
const user = process.env.RABBIT_USER || "";
|
||||||
|
const pass = process.env.RABBIT_PASSWORD || "";
|
||||||
|
const vhost = process.env.RABBIT_VHOST || "/";
|
||||||
|
if (!host || !user || !pass) {
|
||||||
|
throw new Error("Missing RABBIT_HOST/RABBIT_USER/RABBIT_PASSWORD or AMQP_URL");
|
||||||
|
}
|
||||||
|
return `amqp://${user}:${pass}@${host}:${port}${vhost}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const pool = new Pool(buildPgConfig());
|
||||||
|
|
||||||
|
// Минимальная таблица для демо, если БД пустая.
|
||||||
|
async function ensureTable(table) {
|
||||||
|
await pool.query(
|
||||||
|
`CREATE TABLE IF NOT EXISTS ${table} (id SERIAL PRIMARY KEY, test_data TEXT, created_at TIMESTAMP DEFAULT NOW())`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Парсим JSON, нормализуем ключи, иначе считаем текстом.
|
||||||
|
function parseMessage(buffer) {
|
||||||
|
const body = buffer.toString("utf8");
|
||||||
|
try {
|
||||||
|
const parsed = JSON.parse(body);
|
||||||
|
const normalized = Object.keys(parsed || {}).reduce((acc, key) => {
|
||||||
|
acc[key.toLowerCase()] = parsed[key];
|
||||||
|
return acc;
|
||||||
|
}, {});
|
||||||
|
return {
|
||||||
|
action: normalized.action || "create",
|
||||||
|
id: normalized.id,
|
||||||
|
text: normalized.text,
|
||||||
|
requestId: normalized.request_id || normalized.requestid,
|
||||||
|
};
|
||||||
|
} catch {
|
||||||
|
return {
|
||||||
|
action: "create",
|
||||||
|
text: body.trim() || "(empty)",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// CRUD-операции по сообщению.
|
||||||
|
async function handleMessage(table, msg) {
|
||||||
|
const action = (msg.action || "create").toLowerCase();
|
||||||
|
if (action === "create") {
|
||||||
|
const text = msg.text || "(empty)";
|
||||||
|
await pool.query(`INSERT INTO ${table} (test_data) VALUES ($1)`, [text]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (action === "update") {
|
||||||
|
if (!msg.id) {
|
||||||
|
throw new Error("missing id for update");
|
||||||
|
}
|
||||||
|
await pool.query(`UPDATE ${table} SET test_data=$1 WHERE id=$2`, [msg.text || "", msg.id]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (action === "delete") {
|
||||||
|
if (!msg.id) {
|
||||||
|
throw new Error("missing id for delete");
|
||||||
|
}
|
||||||
|
await pool.query(`DELETE FROM ${table} WHERE id=$1`, [msg.id]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
throw new Error("invalid action");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Небольшой HTTP-сервер для /healthz и HTML-статуса.
|
||||||
|
function startHttpServer() {
|
||||||
|
const port = process.env.PORT || 3000;
|
||||||
|
const server = http.createServer((req, res) => {
|
||||||
|
if (req.url === "/healthz") {
|
||||||
|
res.writeHead(200, { "Content-Type": "application/json" });
|
||||||
|
res.end(JSON.stringify(status));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
|
||||||
|
res.end(renderStatusPage());
|
||||||
|
});
|
||||||
|
server.listen(port, () => {
|
||||||
|
console.log(`health server listening on ${port}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Основной цикл: подключение к RabbitMQ и обработка сообщений.
|
||||||
|
async function consumeLoop() {
|
||||||
|
const table = process.env.PG_TABLE || "rabbit_messages";
|
||||||
|
const queueName = (process.env.RABBIT_QUEUES || "crud_queue")
|
||||||
|
.split(",")
|
||||||
|
.map((q) => q.trim())
|
||||||
|
.filter(Boolean)[0] || "crud_queue";
|
||||||
|
const durable = String(process.env.RABBIT_DURABLE || "true").toLowerCase() === "true";
|
||||||
|
const prefetch = Number(process.env.RABBIT_PREFETCH || "1");
|
||||||
|
const requeueOnError = String(process.env.REQUEUE_ON_ERROR || "true").toLowerCase() === "true";
|
||||||
|
|
||||||
|
// Гарантируем наличие таблицы перед стартом консьюмера.
|
||||||
|
await ensureTable(table);
|
||||||
|
|
||||||
|
let backoffMs = 2000;
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
const amqpUrl = buildAmqpUrl();
|
||||||
|
const connection = await amqp.connect(amqpUrl);
|
||||||
|
const channel = await connection.createChannel();
|
||||||
|
await channel.prefetch(prefetch);
|
||||||
|
await channel.assertQueue(queueName, { durable });
|
||||||
|
|
||||||
|
setStatus("running", "");
|
||||||
|
|
||||||
|
// Обрабатываем сообщения и фиксируем успех/ошибки.
|
||||||
|
channel.consume(queueName, async (msg) => {
|
||||||
|
if (!msg) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const parsed = parseMessage(msg.content);
|
||||||
|
await handleMessage(table, parsed);
|
||||||
|
metrics.processed += 1;
|
||||||
|
metrics.lastAction = parsed.action || "create";
|
||||||
|
metrics.lastMessageAt = new Date().toISOString();
|
||||||
|
channel.ack(msg);
|
||||||
|
} catch (err) {
|
||||||
|
metrics.failed += 1;
|
||||||
|
metrics.lastError = String(err.message || err);
|
||||||
|
metrics.lastMessageAt = new Date().toISOString();
|
||||||
|
if (String(err.message || "").includes("missing id") || String(err.message || "").includes("invalid action")) {
|
||||||
|
channel.nack(msg, false, false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
channel.nack(msg, false, requeueOnError);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
await new Promise((resolve, reject) => {
|
||||||
|
connection.on("close", resolve);
|
||||||
|
connection.on("error", reject);
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
setStatus("degraded", String(err.message || err));
|
||||||
|
await new Promise((r) => setTimeout(r, backoffMs));
|
||||||
|
if (backoffMs < 30000) {
|
||||||
|
backoffMs *= 2;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
startHttpServer();
|
||||||
|
consumeLoop().catch((err) => {
|
||||||
|
setStatus("failed", String(err.message || err));
|
||||||
|
console.error(err);
|
||||||
|
});
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
bin/
|
||||||
|
*.log
|
||||||
|
.env
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
# Сборочный этап: собираем статический бинарник.
|
||||||
|
FROM golang:1.22-alpine AS build
|
||||||
|
|
||||||
|
WORKDIR /src
|
||||||
|
|
||||||
|
COPY go.mod ./
|
||||||
|
RUN go mod download
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /out/worker ./
|
||||||
|
|
||||||
|
# Рантайм-этап: минимальный образ с ca-certificates.
|
||||||
|
FROM alpine:3.20
|
||||||
|
|
||||||
|
RUN apk add --no-cache ca-certificates
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
COPY --from=build /out/worker /app/worker
|
||||||
|
|
||||||
|
EXPOSE 8080
|
||||||
|
|
||||||
|
CMD ["/app/worker"]
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
# Rabbit Worker (Go)
|
||||||
|
|
||||||
|
Consumes CRUD messages from RabbitMQ and writes to Postgres.
|
||||||
|
|
||||||
|
Комментарий: HTTP-эндпоинт нужен только для health-check, остальная логика работает через RabbitMQ.
|
||||||
|
|
||||||
|
## Environment variables
|
||||||
|
|
||||||
|
RabbitMQ:
|
||||||
|
- `AMQP_URL` (preferred), example: `amqp://user:pass@host:5672/vhost`
|
||||||
|
- or `RABBIT_HOST`, `RABBIT_PORT` (default 5672), `RABBIT_USER`, `RABBIT_PASSWORD`, `RABBIT_VHOST` (default `/`)
|
||||||
|
- `RABBIT_QUEUES` (comma-separated, default `crud_queue`)
|
||||||
|
- `RABBIT_DURABLE` (default `true`)
|
||||||
|
- `RABBIT_PREFETCH` (default `1`)
|
||||||
|
- `REQUEUE_ON_ERROR` (default `true`)
|
||||||
|
|
||||||
|
Postgres:
|
||||||
|
- `DATABASE_URL` (preferred)
|
||||||
|
- or `PGHOST`, `PGPORT` (default 5432), `PGUSER`, `PGPASSWORD`, `PGDATABASE` (default `postgres`), `PGSSLMODE` (default `require`)
|
||||||
|
- `PG_TABLE` (default `nubes_test_table`)
|
||||||
|
|
||||||
|
## Message format
|
||||||
|
|
||||||
|
JSON payload:
|
||||||
|
```json
|
||||||
|
{"action":"create","text":"hello","id":1}
|
||||||
|
```
|
||||||
|
|
||||||
|
Fields:
|
||||||
|
- `action`: `create` | `update` | `delete`
|
||||||
|
- `id`: required for `update` and `delete`
|
||||||
|
- `text`: used by `create` and `update`
|
||||||
|
|
||||||
|
If the payload is not JSON, it is treated as `create` with body as text.
|
||||||
|
|
||||||
|
Примечание: сообщения без `id` для update/delete считаются некорректными и отбрасываются.
|
||||||
|
|
||||||
|
## Build
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd PROD_STAND/RABBIT/rabbit-worker
|
||||||
|
|
||||||
|
docker build -t <registry>/<image>:<tag> .
|
||||||
|
```
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
module gitea-naeel.giteak8s.services.ngcloud.ru/naeel/rabbit-worker
|
||||||
|
|
||||||
|
go 1.22
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/jackc/pgx/v5 v5.5.5
|
||||||
|
github.com/rabbitmq/amqp091-go v1.10.0
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||||
|
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
|
||||||
|
github.com/jackc/puddle/v2 v2.2.1 // indirect
|
||||||
|
golang.org/x/crypto v0.17.0 // indirect
|
||||||
|
golang.org/x/sync v0.1.0 // indirect
|
||||||
|
golang.org/x/text v0.14.0 // indirect
|
||||||
|
)
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||||
|
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||||
|
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
|
||||||
|
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||||
|
github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw=
|
||||||
|
github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
|
||||||
|
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
|
||||||
|
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/rabbitmq/amqp091-go v1.10.0 h1:STpn5XsHlHGcecLmMFCtg7mqq0RnD+zFr4uzukfVhBw=
|
||||||
|
github.com/rabbitmq/amqp091-go v1.10.0/go.mod h1:Hy4jKW5kQART1u+JkDTF9YYOQUHXqMuhrgxOEeS7G4o=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||||
|
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||||
|
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||||
|
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||||
|
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
|
||||||
|
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
|
||||||
|
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
|
||||||
|
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||||
|
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
@@ -0,0 +1,341 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
amqp "github.com/rabbitmq/amqp091-go"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Воркер держит HTTP /healthz и параллельно слушает RabbitMQ,
|
||||||
|
// записывая события в Postgres. Даже при ошибках контейнер остается живым.
|
||||||
|
|
||||||
|
type Message struct {
|
||||||
|
Action string `json:"action"`
|
||||||
|
ID *int64 `json:"id,omitempty"`
|
||||||
|
Text string `json:"text,omitempty"`
|
||||||
|
Source string `json:"source,omitempty"`
|
||||||
|
RequestID string `json:"request_id,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type workerStatus struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
state string
|
||||||
|
lastError string
|
||||||
|
lastSuccess time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *workerStatus) set(state, err string) {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
s.state = state
|
||||||
|
s.lastError = err
|
||||||
|
if err == "" {
|
||||||
|
s.lastSuccess = time.Now()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *workerStatus) snapshot() map[string]string {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
result := map[string]string{
|
||||||
|
"state": s.state,
|
||||||
|
"error": s.lastError,
|
||||||
|
"timestamp": s.lastSuccess.Format(time.RFC3339),
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// getenv читает переменную окружения с дефолтом.
|
||||||
|
func getenv(key, def string) string {
|
||||||
|
val := os.Getenv(key)
|
||||||
|
if val == "" {
|
||||||
|
return def
|
||||||
|
}
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
// getenvInt читает int-переменную окружения с дефолтом.
|
||||||
|
func getenvInt(key string, def int) int {
|
||||||
|
val := os.Getenv(key)
|
||||||
|
if val == "" {
|
||||||
|
return def
|
||||||
|
}
|
||||||
|
parsed, err := strconv.Atoi(val)
|
||||||
|
if err != nil {
|
||||||
|
return def
|
||||||
|
}
|
||||||
|
return parsed
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildAMQPURL собирает URL для RabbitMQ.
|
||||||
|
func buildAMQPURL() (string, error) {
|
||||||
|
if url := os.Getenv("AMQP_URL"); url != "" {
|
||||||
|
return url, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
host := getenv("RABBIT_HOST", "")
|
||||||
|
port := getenv("RABBIT_PORT", "5672")
|
||||||
|
user := getenv("RABBIT_USER", "")
|
||||||
|
pass := getenv("RABBIT_PASSWORD", "")
|
||||||
|
vhost := getenv("RABBIT_VHOST", "/")
|
||||||
|
|
||||||
|
if host == "" || user == "" || pass == "" {
|
||||||
|
return "", errors.New("missing RABBIT_HOST/RABBIT_USER/RABBIT_PASSWORD or AMQP_URL")
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("amqp://%s:%s@%s:%s%s", user, pass, host, port, vhost), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildPgConnString собирает строку подключения к Postgres.
|
||||||
|
func buildPgConnString() (string, error) {
|
||||||
|
if url := os.Getenv("DATABASE_URL"); url != "" {
|
||||||
|
return url, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
host := getenv("PGHOST", "")
|
||||||
|
if host == "" {
|
||||||
|
return "", errors.New("missing PGHOST or DATABASE_URL")
|
||||||
|
}
|
||||||
|
port := getenv("PGPORT", "5432")
|
||||||
|
user := getenv("PGUSER", "")
|
||||||
|
pass := getenv("PGPASSWORD", "")
|
||||||
|
db := getenv("PGDATABASE", "postgres")
|
||||||
|
ssl := getenv("PGSSLMODE", "require")
|
||||||
|
|
||||||
|
if user == "" || pass == "" {
|
||||||
|
return "", errors.New("missing PGUSER/PGPASSWORD or DATABASE_URL")
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf(
|
||||||
|
"postgresql://%s:%s@%s:%s/%s?sslmode=%s",
|
||||||
|
user,
|
||||||
|
pass,
|
||||||
|
host,
|
||||||
|
port,
|
||||||
|
db,
|
||||||
|
ssl,
|
||||||
|
), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ensureTable гарантирует наличие таблицы для демо.
|
||||||
|
func ensureTable(ctx context.Context, pool *pgxpool.Pool, table string) error {
|
||||||
|
query := fmt.Sprintf(
|
||||||
|
"CREATE TABLE IF NOT EXISTS %s (id SERIAL PRIMARY KEY, test_data TEXT, created_at TIMESTAMP DEFAULT NOW())",
|
||||||
|
table,
|
||||||
|
)
|
||||||
|
_, err := pool.Exec(ctx, query)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseMessage принимает JSON и fallback-ит в текст.
|
||||||
|
func parseMessage(body []byte) Message {
|
||||||
|
msg := Message{Action: "create"}
|
||||||
|
if err := json.Unmarshal(body, &msg); err != nil {
|
||||||
|
msg.Text = strings.TrimSpace(string(body))
|
||||||
|
if msg.Text == "" {
|
||||||
|
msg.Text = "(empty)"
|
||||||
|
}
|
||||||
|
return msg
|
||||||
|
}
|
||||||
|
if msg.Action == "" {
|
||||||
|
msg.Action = "create"
|
||||||
|
}
|
||||||
|
return msg
|
||||||
|
}
|
||||||
|
|
||||||
|
var errInvalidMessage = errors.New("invalid message")
|
||||||
|
|
||||||
|
// handleMessage выполняет CRUD над таблицей.
|
||||||
|
func handleMessage(ctx context.Context, pool *pgxpool.Pool, table string, msg Message) error {
|
||||||
|
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
switch strings.ToLower(msg.Action) {
|
||||||
|
case "create":
|
||||||
|
text := msg.Text
|
||||||
|
if text == "" {
|
||||||
|
text = "(empty)"
|
||||||
|
}
|
||||||
|
query := fmt.Sprintf("INSERT INTO %s (test_data) VALUES ($1)", table)
|
||||||
|
_, err := pool.Exec(ctx, query, text)
|
||||||
|
return err
|
||||||
|
case "update":
|
||||||
|
if msg.ID == nil {
|
||||||
|
return errInvalidMessage
|
||||||
|
}
|
||||||
|
query := fmt.Sprintf("UPDATE %s SET test_data=$1 WHERE id=$2", table)
|
||||||
|
_, err := pool.Exec(ctx, query, msg.Text, *msg.ID)
|
||||||
|
return err
|
||||||
|
case "delete":
|
||||||
|
if msg.ID == nil {
|
||||||
|
return errInvalidMessage
|
||||||
|
}
|
||||||
|
query := fmt.Sprintf("DELETE FROM %s WHERE id=$1", table)
|
||||||
|
_, err := pool.Exec(ctx, query, *msg.ID)
|
||||||
|
return err
|
||||||
|
default:
|
||||||
|
return errInvalidMessage
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// startHTTPServer поднимает /healthz и базовый ответ на /.
|
||||||
|
func startHTTPServer(port string, status *workerStatus) {
|
||||||
|
http.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
payload, _ := json.Marshal(status.snapshot())
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
_, _ = w.Write(payload)
|
||||||
|
})
|
||||||
|
|
||||||
|
http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
_, _ = w.Write([]byte("rabbit-worker ready"))
|
||||||
|
})
|
||||||
|
|
||||||
|
log.Printf("listening on :%s", port)
|
||||||
|
if err := http.ListenAndServe(fmt.Sprintf(":%s", port), nil); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// runWorker подключается к Rabbit и обрабатывает сообщения.
|
||||||
|
func runWorker(ctx context.Context, status *workerStatus) error {
|
||||||
|
amqpURL, err := buildAMQPURL()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
pgConn, err := buildPgConnString()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Список очередей поддерживает несколько значений через запятую.
|
||||||
|
queues := strings.Split(getenv("RABBIT_QUEUES", "crud_queue"), ",")
|
||||||
|
for i := range queues {
|
||||||
|
queues[i] = strings.TrimSpace(queues[i])
|
||||||
|
}
|
||||||
|
queueDurable := getenv("RABBIT_DURABLE", "true") == "true"
|
||||||
|
prefetch := getenvInt("RABBIT_PREFETCH", 1)
|
||||||
|
requeueOnError := getenv("REQUEUE_ON_ERROR", "true") == "true"
|
||||||
|
table := getenv("PG_TABLE", "nubes_test_table")
|
||||||
|
|
||||||
|
pool, err := pgxpool.New(ctx, pgConn)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("pg pool: %w", err)
|
||||||
|
}
|
||||||
|
defer pool.Close()
|
||||||
|
|
||||||
|
if err := pool.Ping(ctx); err != nil {
|
||||||
|
return fmt.Errorf("pg ping: %w", err)
|
||||||
|
}
|
||||||
|
if err := ensureTable(ctx, pool, table); err != nil {
|
||||||
|
return fmt.Errorf("ensure table: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err := amqp.Dial(amqpURL)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("amqp dial: %w", err)
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
ch, err := conn.Channel()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("amqp channel: %w", err)
|
||||||
|
}
|
||||||
|
defer ch.Close()
|
||||||
|
|
||||||
|
if err := ch.Qos(prefetch, 0, false); err != nil {
|
||||||
|
return fmt.Errorf("amqp qos: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, q := range queues {
|
||||||
|
if q == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if _, err := ch.QueueDeclare(q, queueDurable, false, false, false, nil); err != nil {
|
||||||
|
return fmt.Errorf("queue declare %s: %w", q, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Сливаем несколько очередей в один канал сообщений.
|
||||||
|
msgs := make(chan amqp.Delivery)
|
||||||
|
for _, q := range queues {
|
||||||
|
if q == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
delivery, err := ch.Consume(q, "", false, false, false, false, nil)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("consume %s: %w", q, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
go func(d <-chan amqp.Delivery) {
|
||||||
|
for m := range d {
|
||||||
|
msgs <- m
|
||||||
|
}
|
||||||
|
}(delivery)
|
||||||
|
}
|
||||||
|
|
||||||
|
status.set("running", "")
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return nil
|
||||||
|
case m := <-msgs:
|
||||||
|
msg := parseMessage(m.Body)
|
||||||
|
if err := handleMessage(ctx, pool, table, msg); err != nil {
|
||||||
|
if errors.Is(err, errInvalidMessage) {
|
||||||
|
_ = m.Nack(false, false)
|
||||||
|
log.Printf("reject message: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
_ = m.Nack(false, requeueOnError)
|
||||||
|
log.Printf("error handling message: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
_ = m.Ack(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
port := getenv("PORT", "8080")
|
||||||
|
status := &workerStatus{state: "starting"}
|
||||||
|
|
||||||
|
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
defer stop()
|
||||||
|
|
||||||
|
go startHTTPServer(port, status)
|
||||||
|
|
||||||
|
// Экспоненциальный backoff при ошибках подключения.
|
||||||
|
backoff := 2 * time.Second
|
||||||
|
for {
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := runWorker(ctx, status); err != nil {
|
||||||
|
status.set("degraded", err.Error())
|
||||||
|
log.Printf("worker error: %v", err)
|
||||||
|
time.Sleep(backoff)
|
||||||
|
if backoff < 30*time.Second {
|
||||||
|
backoff *= 2
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
status.set("stopped", "")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
# Disabled rabbit worker resource (create-only json_env issue).
|
||||||
|
# Отключено из-за create-only json_env у nubes_http.
|
||||||
|
#
|
||||||
|
# resource "nubes_http" "rabbit_worker" {
|
||||||
|
# resource_name = "rb-worker-02"
|
||||||
|
# resource_realm = "k8s-3.ext.nubes.ru"
|
||||||
|
# domain = "rb-worker-02"
|
||||||
|
# registry_path = "naeel/rabbit-worker:0.3"
|
||||||
|
# resource_c_p_u = 100
|
||||||
|
# resource_memory = 128
|
||||||
|
# resource_instances = 1
|
||||||
|
#
|
||||||
|
# json_env = jsonencode({
|
||||||
|
# RABBIT_HOST = try(nubes_rabbitmq.rb1.state_out_flat["internalConnect.master"], nubes_rabbitmq.rb1.state_out_flat["inernalConnect.master"])
|
||||||
|
# RABBIT_PORT = "5672"
|
||||||
|
# RABBIT_USER = nubes_rabbitmq.rb1.vault_secrets["adminUser"]
|
||||||
|
# RABBIT_PASSWORD = nubes_rabbitmq.rb1.vault_secrets["adminPass"]
|
||||||
|
# RABBIT_VHOST = "/"
|
||||||
|
# RABBIT_QUEUES = "crud_queue"
|
||||||
|
# RABBIT_DURABLE = "true"
|
||||||
|
# RABBIT_PREFETCH = "1"
|
||||||
|
# REQUEUE_ON_ERROR = "true"
|
||||||
|
# PGHOST = var.PGHOST
|
||||||
|
# PGPORT = "5432"
|
||||||
|
# PGUSER = var.PGUSER
|
||||||
|
# PGPASSWORD = var.PGPASSWORD
|
||||||
|
# PGDATABASE = "postgres"
|
||||||
|
# PGSSLMODE = "require"
|
||||||
|
# PG_TABLE = "rabbit_messages"
|
||||||
|
# })
|
||||||
|
#
|
||||||
|
# depends_on = [nubes_rabbitmq.rb1]
|
||||||
|
# }
|
||||||
@@ -0,0 +1,104 @@
|
|||||||
|
# RabbitMQ кластер для демо.
|
||||||
|
resource "nubes_rabbitmq" "rb1" {
|
||||||
|
resource_name = "rabbit_0"
|
||||||
|
resource_realm = "k8s-3.ext.nubes.ru"
|
||||||
|
resource_instances = 1
|
||||||
|
resource_memory = 512
|
||||||
|
resource_c_p_u = 500
|
||||||
|
resource_disk = 5
|
||||||
|
need_external_address_master = false
|
||||||
|
need_external_address_slave = false
|
||||||
|
}
|
||||||
|
|
||||||
|
# Lucee UI, который пишет в Rabbit и читает из Postgres.
|
||||||
|
resource "nubes_lucee" "rabbit_ui" {
|
||||||
|
resource_name = "rb-lucee-ui"
|
||||||
|
resource_realm = "k8s-3.ext.nubes.ru"
|
||||||
|
domain = "rb-ui"
|
||||||
|
app_version = "5.4"
|
||||||
|
git_path = "https://gitea-naeel.giteak8s.services.ngcloud.ru/naeel/rabbit-lsd.git"
|
||||||
|
resource_c_p_u = 300
|
||||||
|
resource_memory = 512
|
||||||
|
resource_instances = 1
|
||||||
|
|
||||||
|
# Переменные окружения для datasource, Rabbit и UI.
|
||||||
|
json_env = jsonencode({
|
||||||
|
testds_bundleName = "org.postgresql.jdbc"
|
||||||
|
testds_bundleVersion = "42.6.0"
|
||||||
|
testds_class = "org.postgresql.Driver"
|
||||||
|
testds_connectionLimit = "5"
|
||||||
|
testds_connectionString = "jdbc:postgresql://${var.PGHOST}:5432/postgres"
|
||||||
|
testds_liveTimeout = "15"
|
||||||
|
testds_username = var.PGUSER
|
||||||
|
testds_password = var.PGPASSWORD
|
||||||
|
testds_validate = "false"
|
||||||
|
PG_TABLE = "rabbit_messages"
|
||||||
|
UI_LOG_TABLE = "rabbit_ui_log"
|
||||||
|
RABBIT_HOST = try(nubes_rabbitmq.rb1.state_out_flat["internalConnect.master"], nubes_rabbitmq.rb1.state_out_flat["inernalConnect.master"])
|
||||||
|
RABBIT_PORT = "5672"
|
||||||
|
RABBIT_USER = nubes_rabbitmq.rb1.vault_secrets["adminUser"]
|
||||||
|
RABBIT_PASSWORD = nubes_rabbitmq.rb1.vault_secrets["adminPass"]
|
||||||
|
RABBIT_VHOST = "/"
|
||||||
|
RABBIT_QUEUES = "crud_queue"
|
||||||
|
RABBIT_DURABLE = "true"
|
||||||
|
RABBIT_ADMIN_URL = "https://${nubes_rabbitmq.rb1.state_out_flat["externalConnect.admin.fqdn"]}/#/"
|
||||||
|
NODEWORKER_URL = "https://nodeworker.nodejsk8s.services.ngcloud.ru/"
|
||||||
|
})
|
||||||
|
|
||||||
|
depends_on = [nubes_rabbitmq.rb1]
|
||||||
|
}
|
||||||
|
|
||||||
|
# NodeJS воркер, который переносит CRUD из очереди в Postgres.
|
||||||
|
resource "nubes_nodejs" "rabbit_nodeworker" {
|
||||||
|
resource_name = "nodeworker"
|
||||||
|
resource_realm = "k8s-3.ext.nubes.ru"
|
||||||
|
domain = "nodeworker"
|
||||||
|
app_version = "23"
|
||||||
|
git_path = "https://gitea-naeel.giteak8s.services.ngcloud.ru/naeel/rabbit-nodeworker.git"
|
||||||
|
health_path = "/healthz"
|
||||||
|
resource_c_p_u = 100
|
||||||
|
resource_memory = 256
|
||||||
|
resource_instances = 1
|
||||||
|
|
||||||
|
# Параметры подключения к Rabbit и Postgres.
|
||||||
|
json_env = jsonencode({
|
||||||
|
RABBIT_HOST = try(nubes_rabbitmq.rb1.state_out_flat["internalConnect.master"], nubes_rabbitmq.rb1.state_out_flat["inernalConnect.master"])
|
||||||
|
RABBIT_PORT = "5672"
|
||||||
|
RABBIT_USER = nubes_rabbitmq.rb1.vault_secrets["adminUser"]
|
||||||
|
RABBIT_PASSWORD = nubes_rabbitmq.rb1.vault_secrets["adminPass"]
|
||||||
|
RABBIT_VHOST = "/"
|
||||||
|
RABBIT_QUEUES = "crud_queue"
|
||||||
|
RABBIT_DURABLE = "true"
|
||||||
|
RABBIT_PREFETCH = "1"
|
||||||
|
REQUEUE_ON_ERROR = "true"
|
||||||
|
PGHOST = var.PGHOST
|
||||||
|
PGPORT = "5432"
|
||||||
|
PGUSER = var.PGUSER
|
||||||
|
PGPASSWORD = var.PGPASSWORD
|
||||||
|
PGDATABASE = "postgres"
|
||||||
|
PGSSLMODE = "require"
|
||||||
|
PG_TABLE = "rabbit_messages"
|
||||||
|
})
|
||||||
|
|
||||||
|
depends_on = [nubes_rabbitmq.rb1]
|
||||||
|
}
|
||||||
|
|
||||||
|
# # Ручной триггер redeploy для воркера.
|
||||||
|
# resource "nubes_nodejs_redeploy" "rabbit_nodeworker_redeploy" {
|
||||||
|
# nodejs_id = nubes_nodejs.rabbit_nodeworker.id
|
||||||
|
# resource_realm = "k8s-3.ext.nubes.ru"
|
||||||
|
# run_id = "redeploy-2026-02-2-01134"
|
||||||
|
# }
|
||||||
|
|
||||||
|
# # Ручной триггер restart для Lucee UI.
|
||||||
|
# resource "nubes_lucee_restart" "rabbit_ui_restart" {
|
||||||
|
# lucee_id = nubes_lucee.rabbit_ui.id
|
||||||
|
# run_id = "restart-2026-02-23-023"
|
||||||
|
# }
|
||||||
|
|
||||||
|
# S3 бакет baba.
|
||||||
|
resource "nubes_s3bucket" "baba_bucket" {
|
||||||
|
resource_name = "baba-buck"
|
||||||
|
s3_user_uid = "s3-111805"
|
||||||
|
bucket_name = "baba"
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
// Пользователь Postgres для воркера и UI.
|
||||||
|
// Важно: Postgres здесь внешний, уже запущенный — параметры берутся из ЛК.
|
||||||
|
variable "PGUSER" {
|
||||||
|
type = string
|
||||||
|
default = "postgres"
|
||||||
|
}
|
||||||
|
// Пароль Postgres для воркера и UI.
|
||||||
|
// Узнайте пароль в UI Личного кабинета и укажите здесь.
|
||||||
|
variable "PGPASSWORD" {
|
||||||
|
type = string
|
||||||
|
default = "TMGMCN5GYzx8gj1GiqA4rmf19HEJKgQXSUHcN17hJNnbyZmicfhU5AfFpLQyF22I"
|
||||||
|
}
|
||||||
|
// FQDN мастера Postgres (важно для DNS внутри кластера).
|
||||||
|
// Значение берется из UI Личного кабинета для уже созданного Postgres.
|
||||||
|
variable "PGHOST" {
|
||||||
|
type = string
|
||||||
|
default = "postgresqlk8s-master.a71094c1-b0ed-4bfb-a264-37b41d279567.svc.k8s-3.ext.nubes.ru"
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
// 2026-03-19
|
||||||
|
// main.tf — провайдер и переменные для managed RabbitMQ в облаке Nubes.
|
||||||
|
// Тест-стенд: deck-api-test.ngcloud.ru
|
||||||
|
terraform {
|
||||||
|
required_providers {
|
||||||
|
nubes = {
|
||||||
|
source = "terra.k8c.ru/nubes/nubes"
|
||||||
|
version = "5.0.19"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Токен доступа к Nubes API.
|
||||||
|
variable "api_token" {
|
||||||
|
type = string
|
||||||
|
sensitive = true
|
||||||
|
description = "Nubes API token"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Реалм, в котором создаётся RabbitMQ (например, k8s-5.ext.nubes.ru).
|
||||||
|
variable "realm" {
|
||||||
|
type = string
|
||||||
|
sensitive = true
|
||||||
|
description = "resource_realm для nubes_rabbitmq"
|
||||||
|
}
|
||||||
|
|
||||||
|
provider "nubes" {
|
||||||
|
api_token = var.api_token
|
||||||
|
api_endpoint = "https://deck-api-test.ngcloud.ru/api/v1/index.cfm"
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
// 2026-03-19
|
||||||
|
// resources.tf — managed RabbitMQ в облаке Nubes для sless и других сервисов.
|
||||||
|
|
||||||
|
resource "nubes_rabbitmq" "rmq" {
|
||||||
|
// Общий брокер для sless event-triggers и других сервисов облака.
|
||||||
|
resource_name = "sless-rabbit"
|
||||||
|
resource_realm = var.realm
|
||||||
|
resource_instances = 1
|
||||||
|
resource_memory = 512
|
||||||
|
resource_c_p_u = 500
|
||||||
|
resource_disk = 5
|
||||||
|
need_external_address_master = false
|
||||||
|
need_external_address_slave = false
|
||||||
|
}
|
||||||
|
|
||||||
|
locals {
|
||||||
|
// try() — обходим опечатку в API: поле называется как internalConnect, так и inernalConnect.
|
||||||
|
rmq_host = try(nubes_rabbitmq.rmq.state_out_flat["internalConnect.master"], nubes_rabbitmq.rmq.state_out_flat["inernalConnect.master"])
|
||||||
|
rmq_user = nubes_rabbitmq.rmq.vault_secrets["adminUser"]
|
||||||
|
rmq_password = nubes_rabbitmq.rmq.vault_secrets["adminPass"]
|
||||||
|
}
|
||||||
|
|
||||||
|
output "rabbitmq_host" {
|
||||||
|
value = local.rmq_host
|
||||||
|
}
|
||||||
|
|
||||||
|
output "rabbitmq_user" {
|
||||||
|
value = local.rmq_user
|
||||||
|
sensitive = true
|
||||||
|
}
|
||||||
|
|
||||||
|
output "rabbitmq_password" {
|
||||||
|
value = local.rmq_password
|
||||||
|
sensitive = true
|
||||||
|
}
|
||||||
|
|
||||||
|
output "rabbitmq_amqp_url" {
|
||||||
|
// Готовый URL для передачи в оператор sless (env RABBITMQ_URL).
|
||||||
|
value = "amqp://${local.rmq_user}:${local.rmq_password}@${local.rmq_host}:5672/"
|
||||||
|
sensitive = true
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
terraform {
|
||||||
|
required_providers {
|
||||||
|
nubes = {
|
||||||
|
source = "terra.k8c.ru/nubes/nubes"
|
||||||
|
version = "2.0.6"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "api_token" {
|
||||||
|
type = string
|
||||||
|
sensitive = true
|
||||||
|
description = "Nubes API token"
|
||||||
|
}
|
||||||
|
|
||||||
|
provider "nubes" {
|
||||||
|
api_token = var.api_token
|
||||||
|
api_endpoint = "https://deck-api-test.ngcloud.ru/api/v1/index.cfm"
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,95 @@
|
|||||||
|
resource "nubes_postgres" "db2" {
|
||||||
|
resource_name = "pg-tst0"
|
||||||
|
s3_uid = "s01325"
|
||||||
|
# s3_uid = "s3-111805"
|
||||||
|
resource_realm = "k8s-4-sandbox-nubes-ru"
|
||||||
|
# resource_realm = "k8s-3.ext.nubes.ru"
|
||||||
|
resource_instances = 1
|
||||||
|
resource_memory = 512
|
||||||
|
resource_c_p_u = 500
|
||||||
|
resource_disk = "1"
|
||||||
|
app_version = "17"
|
||||||
|
json_parameters = jsonencode({
|
||||||
|
log_connections = "off"
|
||||||
|
log_disconnections = "off"
|
||||||
|
})
|
||||||
|
enable_pg_pooler_master = false
|
||||||
|
enable_pg_pooler_slave = false
|
||||||
|
allow_no_s_s_l = false
|
||||||
|
auto_scale = false
|
||||||
|
auto_scale_percentage = 10
|
||||||
|
auto_scale_tech_window = 0
|
||||||
|
auto_scale_quota_gb = "1"
|
||||||
|
need_external_address_master = false
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "nubes_lucee" "app1" {
|
||||||
|
# Lucee-приложение, зависит от Postgres
|
||||||
|
resource_name = "lucy_0"
|
||||||
|
# resource_realm = "k8s-3.ext.nubes.ru"
|
||||||
|
resource_realm = "k8s-4-sandbox-nubes-ru"
|
||||||
|
domain = "web03"
|
||||||
|
|
||||||
|
# git_path = "https://gitea.services.ngcloud.ru/naeel/testlucee-mirror"
|
||||||
|
# git_path = "https://gitea.services.ngcloud.ru/smishchuk/testlucee"
|
||||||
|
# ⬆️ даёт ошибку, с том числе и при обращении к API by CURL
|
||||||
|
# Приложение не запустилось. Производится полный откат установки.
|
||||||
|
# Ошибка: jlib.k8s [correctReplicaActive] | ERROR | Под(ы) не работают: 'luceek8s' (Deployment).
|
||||||
|
|
||||||
|
# ⬇️ клон "https://gitea.services.ngcloud.ru/smishchuk/testlucee.git"
|
||||||
|
git_path = "https://gitea-naeel.giteak8s.services.ngcloud.ru/naeel/testlucee"
|
||||||
|
|
||||||
|
# ⬇️ OK
|
||||||
|
# git_path = "https://github.com/xahys/testlucee"
|
||||||
|
|
||||||
|
json_env = jsonencode({
|
||||||
|
# 🔗 Настройки Data Source 'testds' для Lucee (Application.cfc)
|
||||||
|
testds_class = "org.postgresql.Driver" # 📂 Драйвер БД
|
||||||
|
testds_bundleName = "org.postgresql.jdbc" # 📦 Имя бандла JDBC
|
||||||
|
testds_bundleVersion = "42.6.0" # 🔢 Версия драйвера
|
||||||
|
testds_connectionString = "jdbc:postgresql://${nubes_postgres.db2.state_out_flat["internalConnect.master"]}:5432/postgres" # 🚀 Строка подключения
|
||||||
|
testds_username = nubes_postgres.db2.vault_secrets["adminUser"]# 👤 Логин
|
||||||
|
testds_password = nubes_postgres.db2.vault_secrets["adminPass"] # 🔑 Пароль
|
||||||
|
testds_connectionLimit = "5" # 🚦 Лимит соединений
|
||||||
|
testds_liveTimeout = "15" # ⏳ Таймаут жизни
|
||||||
|
testds_validate = "false" # ✅ Валидация при запросе
|
||||||
|
})
|
||||||
|
|
||||||
|
resource_c_p_u = 300
|
||||||
|
resource_memory = 512
|
||||||
|
resource_instances = 1
|
||||||
|
app_version = "5.4"
|
||||||
|
|
||||||
|
depends_on = [nubes_postgres.db2]
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "nubes_nodejs" "app2" {
|
||||||
|
# NodeJS-приложение, использует тот же Postgres
|
||||||
|
resource_name = "node_0"
|
||||||
|
# resource_realm = "k8s-3.ext.nubes.ru"
|
||||||
|
resource_realm = "k8s-4-sandbox-nubes-ru"
|
||||||
|
domain = "node"
|
||||||
|
git_path = "https://gitea-naeel.giteak8s.services.ngcloud.ru/naeel/testnode"
|
||||||
|
health_path = "/healthz"
|
||||||
|
app_version = "23"
|
||||||
|
|
||||||
|
json_env = jsonencode({
|
||||||
|
PGHOST = nubes_postgres.db2.state_out_flat["internalConnect.master"]
|
||||||
|
PGPORT = "5432"
|
||||||
|
PGUSER = nubes_postgres.db2.vault_secrets["adminUser"]
|
||||||
|
PGPASSWORD = nubes_postgres.db2.vault_secrets["adminPass"]
|
||||||
|
PGSSLMODE = "require"
|
||||||
|
DATABASE_URL = format(
|
||||||
|
"postgresql://%s:%s@%s:5432/postgres",
|
||||||
|
nubes_postgres.db2.vault_secrets["adminUser"],
|
||||||
|
nubes_postgres.db2.vault_secrets["adminPass"],
|
||||||
|
nubes_postgres.db2.state_out_flat["internalConnect.master"]
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
resource_c_p_u = 300
|
||||||
|
resource_memory = 256
|
||||||
|
resource_instances = 1
|
||||||
|
|
||||||
|
depends_on = [nubes_postgres.db2]
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
api_token = ""
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
terraform {
|
||||||
|
required_providers {
|
||||||
|
nubes = {
|
||||||
|
source = "terra.k8c.ru/nubes/nubes"
|
||||||
|
version = "2.0.8"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "api_token" {
|
||||||
|
type = string
|
||||||
|
sensitive = true
|
||||||
|
description = "Nubes API token"
|
||||||
|
}
|
||||||
|
|
||||||
|
provider "nubes" {
|
||||||
|
api_token = var.api_token
|
||||||
|
api_endpoint = "https://deck-api-test.ngcloud.ru/api/v1/index.cfm"
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,126 @@
|
|||||||
|
|
||||||
|
resource "nubes_mariadb" "db2" {
|
||||||
|
resource_name = "mariadb-ops-01"
|
||||||
|
resource_realm = "k8s-4-sandbox-nubes-ru"
|
||||||
|
resource_instances = 1
|
||||||
|
resource_memory = 1024
|
||||||
|
resource_c_p_u = 500
|
||||||
|
resource_disk = 1
|
||||||
|
app_version = "9.4.0"
|
||||||
|
need_external_address_master = false
|
||||||
|
auto_scale = false
|
||||||
|
s3_uid = "s01325"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "nubes_lucee" "app1" {
|
||||||
|
# Lucee-prilozhenie, zavisit ot MariaDB
|
||||||
|
resource_name = "lucy_0"
|
||||||
|
resource_realm = nubes_mariadb.db2.resource_realm
|
||||||
|
domain = "web03"
|
||||||
|
|
||||||
|
git_path = "https://gitea-naeel.giteak8s.services.ngcloud.ru/naeel/testlucee"
|
||||||
|
|
||||||
|
json_env = jsonencode({
|
||||||
|
# Nastroyki Data Source 'testds' dlya Lucee (Application.cfc)
|
||||||
|
testds_class = "org.mariadb.jdbc.Driver"
|
||||||
|
testds_bundleName = "org.mariadb.jdbc"
|
||||||
|
testds_bundleVersion = "3.3.2"
|
||||||
|
testds_connectionString = "jdbc:mariadb://${nubes_mariadb.db2.state_out_flat["internalConnect.master"]}:3306/postgres"
|
||||||
|
testds_username = nubes_mariadb.db2.vault_secrets["adminUser"]
|
||||||
|
testds_password = nubes_mariadb.db2.vault_secrets["adminPass"]
|
||||||
|
testds_connectionLimit = "5"
|
||||||
|
testds_liveTimeout = "15"
|
||||||
|
testds_validate = "false"
|
||||||
|
})
|
||||||
|
|
||||||
|
resource_c_p_u = 300
|
||||||
|
resource_memory = 512
|
||||||
|
resource_instances = 1
|
||||||
|
app_version = "5.4"
|
||||||
|
|
||||||
|
depends_on = [nubes_mariadb.db2]
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "nubes_lucee_restart" "app1_restart" {
|
||||||
|
lucee_id = nubes_lucee.app1.id
|
||||||
|
# Bump run_id to trigger another restart.
|
||||||
|
run_id = "restart-1"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "nubes_flask" "app2" {
|
||||||
|
resource_name = "flask_0"
|
||||||
|
resource_realm = nubes_mariadb.db2.resource_realm
|
||||||
|
domain = "flask"
|
||||||
|
git_path = "https://github.com/Foxyhhd/Baldurs-Gate-test.git"
|
||||||
|
|
||||||
|
json_env = jsonencode({
|
||||||
|
DB_HOST = nubes_mariadb.db2.state_out_flat["internalConnect.master"]
|
||||||
|
DB_PORT = "3306"
|
||||||
|
DB_USER = nubes_mariadb.db2.vault_secrets["adminUser"]
|
||||||
|
DB_PASSWORD = nubes_mariadb.db2.vault_secrets["adminPass"]
|
||||||
|
DB_NAME = "postgres"
|
||||||
|
DATABASE_URL = format(
|
||||||
|
"mysql+pymysql://%s:%s@%s:3306/postgres",
|
||||||
|
nubes_mariadb.db2.vault_secrets["adminUser"],
|
||||||
|
nubes_mariadb.db2.vault_secrets["adminPass"],
|
||||||
|
nubes_mariadb.db2.state_out_flat["internalConnect.master"]
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
resource_c_p_u = 300
|
||||||
|
resource_memory = 256
|
||||||
|
resource_instances = 1
|
||||||
|
|
||||||
|
depends_on = [nubes_mariadb.db2]
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "nubes_nodejs" "app3" {
|
||||||
|
resource_name = "node_0"
|
||||||
|
resource_realm = nubes_mariadb.db2.resource_realm
|
||||||
|
domain = "node"
|
||||||
|
git_path = "https://gitea-naeel.giteak8s.services.ngcloud.ru/naeel/testnode"
|
||||||
|
health_path = "/healthz"
|
||||||
|
app_version = "23"
|
||||||
|
|
||||||
|
json_env = jsonencode({
|
||||||
|
DB_HOST = nubes_mariadb.db2.state_out_flat["internalConnect.master"]
|
||||||
|
DB_PORT = "3306"
|
||||||
|
DB_USER = nubes_mariadb.db2.vault_secrets["adminUser"]
|
||||||
|
DB_PASSWORD = nubes_mariadb.db2.vault_secrets["adminPass"]
|
||||||
|
DB_NAME = "postgres"
|
||||||
|
DATABASE_URL = format(
|
||||||
|
"mysql://%s:%s@%s:3306/postgres",
|
||||||
|
nubes_mariadb.db2.vault_secrets["adminUser"],
|
||||||
|
nubes_mariadb.db2.vault_secrets["adminPass"],
|
||||||
|
nubes_mariadb.db2.state_out_flat["internalConnect.master"]
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
resource_c_p_u = 300
|
||||||
|
resource_memory = 256
|
||||||
|
resource_instances = 1
|
||||||
|
|
||||||
|
depends_on = [nubes_mariadb.db2]
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "nubes_http" "mariadb_check" {
|
||||||
|
resource_name = "mariadb-check-01"
|
||||||
|
resource_realm = nubes_mariadb.db2.resource_realm
|
||||||
|
domain = "mariadb-check"
|
||||||
|
registry_path = "naeel/mariadb-check:0.1"
|
||||||
|
resource_c_p_u = 100
|
||||||
|
resource_memory = 256
|
||||||
|
resource_instances = 1
|
||||||
|
|
||||||
|
json_env = jsonencode({
|
||||||
|
DB_HOST = nubes_mariadb.db2.state_out_flat["internalConnect.master"]
|
||||||
|
DB_PORT = "3306"
|
||||||
|
DB_USER = nubes_mariadb.db2.vault_secrets["adminUser"]
|
||||||
|
DB_PASSWORD = nubes_mariadb.db2.vault_secrets["adminPass"]
|
||||||
|
DB_NAME = "postgres"
|
||||||
|
})
|
||||||
|
|
||||||
|
depends_on = [nubes_mariadb.db2]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
api_token = ""
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
provider_installation {
|
||||||
|
dev_overrides {
|
||||||
|
"terra.k8c.ru/nubes/nubes" = "../../universal_rebuild/bin"
|
||||||
|
}
|
||||||
|
direct {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
|
||||||
|
# resource "nubes_lucee" "app1" {
|
||||||
|
# # Lucee-приложение, зависит от Postgres
|
||||||
|
# resource_name = "lucy_teststand_0"
|
||||||
|
# resource_realm = nubes_postgres.npg.resource_realm
|
||||||
|
# domain = "web-test-stand"
|
||||||
|
|
||||||
|
# git_path = "https://gitea-naeel.giteak8s.services.ngcloud.ru/naeel/testlucee.git"
|
||||||
|
|
||||||
|
# json_env = jsonencode({
|
||||||
|
# # 🔗 Настройки Data Source 'testds' для Lucee (Application.cfc)
|
||||||
|
# testds_class = "org.postgresql.Driver" # 📂 Драйвер БД
|
||||||
|
# testds_bundleName = "org.postgresql.jdbc" # 📦 Имя бандла JDBC
|
||||||
|
# testds_bundleVersion = "42.6.0" # 🔢 Версия драйвера
|
||||||
|
# testds_connectionString = "jdbc:postgresql://${nubes_postgres.npg.state_out_flat["internalConnect.master"]}:5432/${nubes_postgres_database.db2_app.db_name}?sslmode=require" # 🚀 Строка подключения
|
||||||
|
# testds_username = nubes_postgres_user.pg_user.username # 👤 Логин
|
||||||
|
# testds_password = jsondecode(nubes_postgres.npg.vault_secrets["users"])[nubes_postgres_user.pg_user.username]["password"] # 🔑 Пароль
|
||||||
|
# testds_connectionLimit = "5" # 🚦 Лимит соединений
|
||||||
|
# testds_liveTimeout = "15" # ⏳ Таймаут жизни
|
||||||
|
# testds_validate = "false" # ✅ Валидация при запросе
|
||||||
|
# })
|
||||||
|
|
||||||
|
# resource_c_p_u = 300
|
||||||
|
# resource_memory = 512
|
||||||
|
# resource_instances = 1
|
||||||
|
# app_version = "5.4"
|
||||||
|
|
||||||
|
# depends_on = [nubes_postgres.npg]
|
||||||
|
# }
|
||||||
|
|
||||||
|
# resource "nubes_nodejs" "app3" {
|
||||||
|
# # NodeJS демо, работающий с тем же Postgres.
|
||||||
|
# resource_name = "node_01"
|
||||||
|
# resource_realm = nubes_postgres.npg.resource_realm
|
||||||
|
# domain = "node07"
|
||||||
|
# git_path = "https://gitea-naeel.giteak8s.services.ngcloud.ru/naeel/testnode.git"
|
||||||
|
# health_path = "/healthz"
|
||||||
|
# app_version = "23"
|
||||||
|
|
||||||
|
# json_env = jsonencode({
|
||||||
|
# # Переменные подключения к Postgres.
|
||||||
|
# PGHOST = nubes_postgres.npg.state_out_flat["internalConnect.master"]
|
||||||
|
# PGPORT = "5432"
|
||||||
|
# PGUSER = nubes_postgres_user.pg_user.username
|
||||||
|
# PGPASSWORD = jsondecode(nubes_postgres.npg.vault_secrets["users"])[nubes_postgres_user.pg_user.username]["password"]
|
||||||
|
# PGDATABASE = nubes_postgres_database.db2_app.db_name
|
||||||
|
# PGSSLMODE = "require"
|
||||||
|
# DATABASE_URL = format(
|
||||||
|
# "postgresql://%s:%s@%s:5432/%s?sslmode=require",
|
||||||
|
# nubes_postgres_user.pg_user.username,
|
||||||
|
# jsondecode(nubes_postgres.npg.vault_secrets["users"])[nubes_postgres_user.pg_user.username]["password"],
|
||||||
|
# nubes_postgres.npg.state_out_flat["internalConnect.master"],
|
||||||
|
# nubes_postgres_database.db2_app.db_name
|
||||||
|
# )
|
||||||
|
# })
|
||||||
|
|
||||||
|
# resource_c_p_u = 300
|
||||||
|
# resource_memory = 256
|
||||||
|
# resource_instances = 1
|
||||||
|
|
||||||
|
# depends_on = [nubes_postgres.npg]
|
||||||
|
# }
|
||||||
|
|
||||||
|
# output "pg_vault_secrets" {
|
||||||
|
# value = nubes_postgres.npg.vault_secrets
|
||||||
|
# sensitive = true
|
||||||
|
# }
|
||||||
|
|
||||||
|
# terraform output -json pg_vault_secrets
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
terraform {
|
||||||
|
required_providers {
|
||||||
|
nubes = {
|
||||||
|
source = "terra.k8c.ru/nubes/nubes"
|
||||||
|
version = "5.0.52"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "api_token" {
|
||||||
|
type = string
|
||||||
|
sensitive = true
|
||||||
|
description = "Nubes API token"
|
||||||
|
}
|
||||||
|
variable "s3_uid" {
|
||||||
|
type = string
|
||||||
|
sensitive = true
|
||||||
|
description = "Nubes S3 UID"
|
||||||
|
}
|
||||||
|
variable "realm" {
|
||||||
|
type = string
|
||||||
|
sensitive = true
|
||||||
|
description = "resource_realm parameter for nubes_postgres resource"
|
||||||
|
}
|
||||||
|
|
||||||
|
provider "nubes" {
|
||||||
|
api_token = var.api_token
|
||||||
|
api_endpoint = "https://deck-api-test.ngcloud.ru/api/v1/index.cfm"
|
||||||
|
log_level = "debug" # none | info | debug, default = "none"
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
resource "nubes_postgres" "npg" {
|
||||||
|
resource_name = "pg4tf033"
|
||||||
|
s3_uid = var.s3_uid
|
||||||
|
resource_realm = var.realm
|
||||||
|
resource_instances = 1
|
||||||
|
resource_memory = 512
|
||||||
|
resource_c_p_u = 500
|
||||||
|
resource_disk = "1"
|
||||||
|
app_version = "17"
|
||||||
|
json_parameters = jsonencode({
|
||||||
|
log_connections = "off"
|
||||||
|
log_disconnections = "off"
|
||||||
|
})
|
||||||
|
enable_pg_pooler_master = false
|
||||||
|
enable_pg_pooler_slave = false
|
||||||
|
allow_no_s_s_l = false
|
||||||
|
auto_scale = false
|
||||||
|
auto_scale_percentage = 10
|
||||||
|
auto_scale_tech_window = 0
|
||||||
|
auto_scale_quota_gb = "1"
|
||||||
|
need_external_address_master = false
|
||||||
|
|
||||||
|
# suspend_on_destroy = false
|
||||||
|
operation_timeout = "11m"
|
||||||
|
adopt_existing_on_create = true
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# resource "nubes_s3bucket" "baba_bucket" {
|
||||||
|
# resource_name = "baba-buck-test1"
|
||||||
|
# s3_user_uid = var.s3_uid
|
||||||
|
# bucket_name = "baba000"
|
||||||
|
# adopt_existing_on_create=true
|
||||||
|
# }
|
||||||
|
|
||||||
|
resource "nubes_postgres_user" "pg_user" {
|
||||||
|
postgres_id = nubes_postgres.npg.id
|
||||||
|
username = "user0"
|
||||||
|
role = "ddl_user"
|
||||||
|
adopt_existing_on_create = true
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "nubes_postgres_database" "db2_app" {
|
||||||
|
postgres_id = nubes_postgres.npg.id
|
||||||
|
db_name = "db_terra"
|
||||||
|
db_owner = nubes_postgres_user.pg_user.username
|
||||||
|
adopt_existing_on_create = true
|
||||||
|
# suspend_on_destroy = false
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
# S3 Event Function PoC (один apply, без создания `nubes_s3`)
|
||||||
|
|
||||||
|
Стенд делает всё в рамках одного `terraform apply`:
|
||||||
|
|
||||||
|
1. Создаёт `nubes_s3bucket` на существующем `s3_uid`.
|
||||||
|
2. Автоматически запускает demo-check (`terraform_data + local-exec`).
|
||||||
|
3. Demo-check пишет тестовый объект в бакет.
|
||||||
|
4. Событие бакета доставляется в простую функцию, и apply печатает `CONFIRMED`.
|
||||||
|
|
||||||
|
## Подготовка
|
||||||
|
|
||||||
|
- Нужны: `terraform`, `python3`, `mc`.
|
||||||
|
- В `terraform.tfvars` задайте `api_token`, `s3_uid`, `bucket_name`.
|
||||||
|
- Путь к `.s3cfg_registry` можно оставить по умолчанию или задать переменной `s3cfg_path`.
|
||||||
|
|
||||||
|
Можно взять `api_token` и `s3_uid` из `TEST_STAND/POSTGRES/terraform.tfvars`.
|
||||||
|
|
||||||
|
## Запуск (одна команда apply)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd TEST_STAND/S3_EVENT_FUNCTION_POC
|
||||||
|
terraform init
|
||||||
|
terraform apply -auto-approve
|
||||||
|
```
|
||||||
|
|
||||||
|
## Подтверждение, что сработало
|
||||||
|
|
||||||
|
- Во время `apply` появится строка:
|
||||||
|
- `CONFIRMED: bucket event reached function ...`
|
||||||
|
- После `apply` доступен output:
|
||||||
|
- `event_demo_confirmation`
|
||||||
|
|
||||||
|
Проверить output:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
terraform output event_demo_confirmation
|
||||||
|
```
|
||||||
|
|
||||||
|
## Отладка (если failed)
|
||||||
|
|
||||||
|
Смотрите файлы в каталоге стенда:
|
||||||
|
|
||||||
|
- `function_stdout.log`
|
||||||
|
- `watcher_stdout.log`
|
||||||
|
- `function.log`
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
CONFIRMED ✓
|
||||||
|
|
||||||
|
Source bucket : poc-s3evt-src
|
||||||
|
Dest bucket : poc-s3evt-dst
|
||||||
|
Object : event-trigger-1773587291.txt
|
||||||
|
Time : Sun Mar 15 03:08:32 PM UTC 2026
|
||||||
|
|
||||||
|
Что произошло:
|
||||||
|
1. terraform создал два бакета: poc-s3evt-src и poc-s3evt-dst
|
||||||
|
2. Скрипт написал 'event-trigger-1773587291.txt' в poc-s3evt-src
|
||||||
|
3. Срабатывание подтверждено через: fallback check
|
||||||
|
4. «Функция» скопировала объект в poc-s3evt-dst
|
||||||
|
5. Копия подтверждена — тест пройден
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
provider "nubes" {
|
||||||
|
api_token = var.api_token
|
||||||
|
api_endpoint = var.api_endpoint
|
||||||
|
}
|
||||||
|
|
||||||
|
# Бакет-источник — сюда пишем объект
|
||||||
|
resource "nubes_s3bucket" "source" {
|
||||||
|
resource_name = "${var.bucket_name}-src"
|
||||||
|
s3_user_uid = var.s3_uid
|
||||||
|
bucket_name = "${var.bucket_name}-src"
|
||||||
|
adopt_existing_on_create = true # бакет уже создан предыдущим apply
|
||||||
|
|
||||||
|
max_size = -1
|
||||||
|
read_all = false
|
||||||
|
list_all = false
|
||||||
|
cors_all = false
|
||||||
|
placement = "HOT"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Бакет-назначение — сюда копирует «функция» по событию
|
||||||
|
resource "nubes_s3bucket" "dest" {
|
||||||
|
resource_name = "${var.bucket_name}-dst"
|
||||||
|
s3_user_uid = var.s3_uid
|
||||||
|
bucket_name = "${var.bucket_name}-dst"
|
||||||
|
|
||||||
|
max_size = -1
|
||||||
|
read_all = false
|
||||||
|
list_all = false
|
||||||
|
cors_all = false
|
||||||
|
placement = "HOT"
|
||||||
|
}
|
||||||
|
|
||||||
|
locals {
|
||||||
|
src_bucket = try(nubes_s3bucket.source.state_params["bucketName"], "${var.bucket_name}-src")
|
||||||
|
dst_bucket = try(nubes_s3bucket.dest.state_params["bucketName"], "${var.bucket_name}-dst")
|
||||||
|
}
|
||||||
|
|
||||||
|
# «Функция»: mc watch ловит событие в source → копирует объект в dest
|
||||||
|
resource "terraform_data" "event_delivery_check" {
|
||||||
|
depends_on = [nubes_s3bucket.source, nubes_s3bucket.dest]
|
||||||
|
|
||||||
|
triggers_replace = {
|
||||||
|
src = local.src_bucket
|
||||||
|
dst = local.dst_bucket
|
||||||
|
s3cfg_path = var.s3cfg_path
|
||||||
|
}
|
||||||
|
|
||||||
|
provisioner "local-exec" {
|
||||||
|
command = "bash ${path.module}/scripts/copy_on_event.sh \"${var.s3cfg_path}\" \"${local.src_bucket}\" \"${local.dst_bucket}\" \"${var.demo_timeout_sec}\""
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
output "source_bucket" {
|
||||||
|
value = nubes_s3bucket.source.state_params["bucketName"]
|
||||||
|
}
|
||||||
|
|
||||||
|
output "dest_bucket" {
|
||||||
|
value = nubes_s3bucket.dest.state_params["bucketName"]
|
||||||
|
}
|
||||||
|
|
||||||
|
output "event_demo_confirmation" {
|
||||||
|
value = try(trimspace(file("${path.module}/apply_confirmation.txt")), "confirmation file not found")
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||||
|
|
||||||
|
if [[ ! -f "$ROOT_DIR/terraform.tfvars" ]]; then
|
||||||
|
echo "ERROR: $ROOT_DIR/terraform.tfvars not found"
|
||||||
|
echo "Create it from terraform.tfvars.example or reuse values from TEST_STAND/POSTGRES/terraform.tfvars"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
cd "$ROOT_DIR"
|
||||||
|
terraform init
|
||||||
|
terraform apply -auto-approve
|
||||||
|
|
||||||
|
BUCKET="$(terraform output -raw bucket_name 2>/dev/null || true)"
|
||||||
|
if [[ -z "$BUCKET" ]]; then
|
||||||
|
echo "ERROR: bucket_name output is empty"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Bucket created: $BUCKET"
|
||||||
|
|
||||||
|
echo "Running event->function demo..."
|
||||||
|
export S3CFG="${S3CFG:-/home/naeel/remote_dev/terraform/secrets/.s3cfg_registry}"
|
||||||
|
export BUCKET
|
||||||
|
bash "$ROOT_DIR/scripts/run_demo.sh"
|
||||||
+138
@@ -0,0 +1,138 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# Симуляция event-driven функции:
|
||||||
|
# 1. mc watch следит за SOURCE бакетом (как S3 notification)
|
||||||
|
# 2. При появлении объекта — "функция" копирует его в DEST бакет
|
||||||
|
# 3. Подтверждение записывается в apply_confirmation.txt
|
||||||
|
#
|
||||||
|
# Аргументы: <s3cfg_path> <src_bucket> <dst_bucket> [timeout_sec]
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
S3CFG="${1:?s3cfg_path required}"
|
||||||
|
SRC_BUCKET="${2:?src_bucket required}"
|
||||||
|
DST_BUCKET="${3:?dst_bucket required}"
|
||||||
|
TIMEOUT="${4:-35}"
|
||||||
|
|
||||||
|
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||||
|
POC_DIR="$(dirname "$SCRIPT_DIR")"
|
||||||
|
CONFIRM_FILE="${POC_DIR}/apply_confirmation.txt"
|
||||||
|
EVENTS_FILE="/tmp/poc_events_$$.json"
|
||||||
|
ALIAS="poc$$"
|
||||||
|
|
||||||
|
cleanup() {
|
||||||
|
kill "$WATCHER_PID" 2>/dev/null || true
|
||||||
|
rm -f "$EVENTS_FILE"
|
||||||
|
mc alias rm "$ALIAS" >/dev/null 2>&1 || true
|
||||||
|
}
|
||||||
|
trap cleanup EXIT
|
||||||
|
|
||||||
|
echo "[poc] S3CFG: $S3CFG"
|
||||||
|
echo "[poc] Source bucket : $SRC_BUCKET"
|
||||||
|
echo "[poc] Dest bucket : $DST_BUCKET"
|
||||||
|
echo "[poc] Timeout : ${TIMEOUT}s"
|
||||||
|
|
||||||
|
# Парсим .s3cfg
|
||||||
|
HOST=$(grep -E '^\s*host_base\s*=' "$S3CFG" | head -1 | sed 's/.*=\s*//' | tr -d '[:space:]')
|
||||||
|
AK=$(grep -E '^\s*access_key\s*=' "$S3CFG" | head -1 | sed 's/.*=\s*//' | tr -d '[:space:]')
|
||||||
|
SK=$(grep -E '^\s*secret_key\s*=' "$S3CFG" | head -1 | sed 's/.*=\s*//' | tr -d '[:space:]')
|
||||||
|
USE_HTTPS=$(grep -E '^\s*use_https\s*=' "$S3CFG" | head -1 | sed 's/.*=\s*//' | tr -d '[:space:]' | tr '[:upper:]' '[:lower:]')
|
||||||
|
|
||||||
|
if [[ -z "$HOST" || -z "$AK" || -z "$SK" ]]; then
|
||||||
|
echo "[poc] ERROR: не удалось разобрать .s3cfg" >&2; exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
SCHEME="https"
|
||||||
|
if [[ "$USE_HTTPS" == "false" || "$USE_HTTPS" == "0" || "$USE_HTTPS" == "no" ]]; then
|
||||||
|
SCHEME="http"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "$HOST" == http://* || "$HOST" == https://* ]]; then
|
||||||
|
ENDPOINT="$HOST"
|
||||||
|
else
|
||||||
|
ENDPOINT="${SCHEME}://${HOST}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Настраиваем mc alias
|
||||||
|
mc alias set "$ALIAS" "$ENDPOINT" "$AK" "$SK" --api S3v4 >/dev/null 2>&1
|
||||||
|
echo "[poc] mc alias '$ALIAS' → ${ENDPOINT}"
|
||||||
|
|
||||||
|
# Запускаем mc watch на SOURCE (стримит события в файл)
|
||||||
|
touch "$EVENTS_FILE"
|
||||||
|
mc watch --json "${ALIAS}/${SRC_BUCKET}" >"$EVENTS_FILE" 2>&1 &
|
||||||
|
WATCHER_PID=$!
|
||||||
|
echo "[poc] mc watch запущен (pid=$WATCHER_PID)"
|
||||||
|
sleep 2 # даём watch время подключиться
|
||||||
|
|
||||||
|
# Имя триггерного объекта
|
||||||
|
TRIGGER_KEY="event-trigger-$(date +%s).txt"
|
||||||
|
echo "[poc] Пишем объект '${TRIGGER_KEY}' в ${SRC_BUCKET} (trigger)..."
|
||||||
|
echo "S3 event PoC trigger — $(date -u)" | mc pipe "${ALIAS}/${SRC_BUCKET}/${TRIGGER_KEY}"
|
||||||
|
echo "[poc] Объект записан. Ждём события через mc watch (макс ${TIMEOUT}s)..."
|
||||||
|
|
||||||
|
# Ждём появления ключа в потоке событий mc watch
|
||||||
|
ELAPSED=0
|
||||||
|
EVENT_DETECTED=false
|
||||||
|
EVENT_METHOD=""
|
||||||
|
while [[ $ELAPSED -lt $TIMEOUT ]]; do
|
||||||
|
if grep -q "${TRIGGER_KEY}" "$EVENTS_FILE" 2>/dev/null; then
|
||||||
|
EVENT_DETECTED=true
|
||||||
|
EVENT_METHOD="mc watch"
|
||||||
|
echo "[poc] Событие обнаружено в потоке mc watch (через ${ELAPSED}s)"
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
sleep 2
|
||||||
|
ELAPSED=$((ELAPSED + 2))
|
||||||
|
done
|
||||||
|
|
||||||
|
if [[ "$EVENT_DETECTED" != "true" ]]; then
|
||||||
|
echo "[poc] WARN: mc watch не поймал объект за ${TIMEOUT}s, проверяем напрямую..."
|
||||||
|
if mc ls "${ALIAS}/${SRC_BUCKET}/${TRIGGER_KEY}" >/dev/null 2>&1; then
|
||||||
|
EVENT_DETECTED=true
|
||||||
|
EVENT_METHOD="fallback check"
|
||||||
|
echo "[poc] Объект есть в source — выполняем копирование (fallback)"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "$EVENT_DETECTED" != "true" ]]; then
|
||||||
|
echo "[poc] FAILED: объект не найден в source bucket" >&2
|
||||||
|
cat >"$CONFIRM_FILE" <<EOF
|
||||||
|
FAILED
|
||||||
|
Объект ${TRIGGER_KEY} не появился в ${SRC_BUCKET} за ${TIMEOUT}s
|
||||||
|
EOF
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# «Функция» — копируем объект из source в dest
|
||||||
|
echo "[poc] Вызываем 'функцию': mc cp ${SRC_BUCKET}/${TRIGGER_KEY} → ${DST_BUCKET}/${TRIGGER_KEY}"
|
||||||
|
mc cp "${ALIAS}/${SRC_BUCKET}/${TRIGGER_KEY}" "${ALIAS}/${DST_BUCKET}/${TRIGGER_KEY}"
|
||||||
|
|
||||||
|
# Проверяем что файл появился в dest
|
||||||
|
if ! mc ls "${ALIAS}/${DST_BUCKET}/${TRIGGER_KEY}" >/dev/null 2>&1; then
|
||||||
|
echo "[poc] FAILED: файл не найден в dest bucket" >&2
|
||||||
|
cat >"$CONFIRM_FILE" <<EOF
|
||||||
|
FAILED
|
||||||
|
Копирование в ${DST_BUCKET}/${TRIGGER_KEY} не подтверждено
|
||||||
|
EOF
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "[poc] SUCCESS: файл скопирован в ${DST_BUCKET}"
|
||||||
|
|
||||||
|
cat >"$CONFIRM_FILE" <<EOF
|
||||||
|
CONFIRMED ✓
|
||||||
|
|
||||||
|
Source bucket : ${SRC_BUCKET}
|
||||||
|
Dest bucket : ${DST_BUCKET}
|
||||||
|
Object : ${TRIGGER_KEY}
|
||||||
|
Time : $(date -u)
|
||||||
|
|
||||||
|
Что произошло:
|
||||||
|
1. terraform создал два бакета: ${SRC_BUCKET} и ${DST_BUCKET}
|
||||||
|
2. Скрипт написал '${TRIGGER_KEY}' в ${SRC_BUCKET}
|
||||||
|
3. Срабатывание подтверждено через: ${EVENT_METHOD}
|
||||||
|
4. «Функция» скопировала объект в ${DST_BUCKET}
|
||||||
|
5. Копия подтверждена — тест пройден
|
||||||
|
EOF
|
||||||
|
|
||||||
|
echo "[poc] apply_confirmation.txt записан"
|
||||||
|
cat "$CONFIRM_FILE"
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import datetime
|
||||||
|
import json
|
||||||
|
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||||
|
|
||||||
|
LOG_FILE = "function.log"
|
||||||
|
|
||||||
|
|
||||||
|
class Handler(BaseHTTPRequestHandler):
|
||||||
|
def do_POST(self):
|
||||||
|
length = int(self.headers.get("Content-Length", 0))
|
||||||
|
body = self.rfile.read(length).decode("utf-8", errors="ignore")
|
||||||
|
|
||||||
|
now = datetime.datetime.utcnow().isoformat() + "Z"
|
||||||
|
line = f"[{now}] path={self.path} body={body}\n"
|
||||||
|
|
||||||
|
with open(LOG_FILE, "a", encoding="utf-8") as f:
|
||||||
|
f.write(line)
|
||||||
|
|
||||||
|
print(line, end="")
|
||||||
|
self.send_response(200)
|
||||||
|
self.end_headers()
|
||||||
|
self.wfile.write(b"ok")
|
||||||
|
|
||||||
|
def log_message(self, *_args):
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
server = HTTPServer(("127.0.0.1", 8787), Handler)
|
||||||
|
print("Function server started at http://127.0.0.1:8787/invoke")
|
||||||
|
server.serve_forever()
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import json
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import urllib.request
|
||||||
|
|
||||||
|
if len(sys.argv) != 3:
|
||||||
|
print("usage: poc_watch_and_invoke.py <mc_alias> <bucket>")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
alias = sys.argv[1]
|
||||||
|
bucket = sys.argv[2]
|
||||||
|
|
||||||
|
cmd = ["mc", "watch", "--json", f"{alias}/{bucket}"]
|
||||||
|
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
|
||||||
|
|
||||||
|
print(f"Watcher started for {alias}/{bucket}")
|
||||||
|
|
||||||
|
for line in proc.stdout:
|
||||||
|
line = line.strip()
|
||||||
|
if not line:
|
||||||
|
continue
|
||||||
|
|
||||||
|
try:
|
||||||
|
payload = json.loads(line)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
continue
|
||||||
|
|
||||||
|
event = payload.get("eventName", "")
|
||||||
|
if not event:
|
||||||
|
continue
|
||||||
|
|
||||||
|
data = json.dumps(payload).encode("utf-8")
|
||||||
|
req = urllib.request.Request(
|
||||||
|
"http://127.0.0.1:8787/invoke",
|
||||||
|
method="POST",
|
||||||
|
data=data,
|
||||||
|
headers={"Content-Type": "application/json"},
|
||||||
|
)
|
||||||
|
|
||||||
|
with urllib.request.urlopen(req, timeout=5) as r:
|
||||||
|
_ = r.read()
|
||||||
|
|
||||||
|
print(f"forwarded event: {event}")
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
if ! command -v mc >/dev/null 2>&1; then
|
||||||
|
echo "ERROR: mc is not installed"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ -z "${S3CFG:-}" ]]; then
|
||||||
|
echo "ERROR: set S3CFG (path to .s3cfg_registry)"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ -z "${BUCKET:-}" ]]; then
|
||||||
|
echo "ERROR: set BUCKET"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
RUNTIME_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
PROJECT_DIR="$(dirname "$RUNTIME_DIR")"
|
||||||
|
|
||||||
|
python3 - <<PY > "$PROJECT_DIR/.mc_creds.tmp"
|
||||||
|
import configparser
|
||||||
|
cfg=configparser.RawConfigParser(); cfg.read("$S3CFG")
|
||||||
|
sec=cfg["default"]
|
||||||
|
print(sec.get("access_key",""))
|
||||||
|
print(sec.get("secret_key",""))
|
||||||
|
print(sec.get("host_base",""))
|
||||||
|
PY
|
||||||
|
|
||||||
|
ACCESS_KEY="$(sed -n '1p' "$PROJECT_DIR/.mc_creds.tmp")"
|
||||||
|
SECRET_KEY="$(sed -n '2p' "$PROJECT_DIR/.mc_creds.tmp")"
|
||||||
|
HOST_BASE="$(sed -n '3p' "$PROJECT_DIR/.mc_creds.tmp")"
|
||||||
|
rm -f "$PROJECT_DIR/.mc_creds.tmp"
|
||||||
|
|
||||||
|
if [[ -z "$ACCESS_KEY" || -z "$SECRET_KEY" || -z "$HOST_BASE" ]]; then
|
||||||
|
echo "ERROR: failed to read credentials from S3CFG"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
ALIAS="poc"
|
||||||
|
mc alias rm "$ALIAS" >/dev/null 2>&1 || true
|
||||||
|
mc alias set "$ALIAS" "https://$HOST_BASE" "$ACCESS_KEY" "$SECRET_KEY" >/dev/null
|
||||||
|
|
||||||
|
cd "$PROJECT_DIR"
|
||||||
|
rm -f function.log .function.pid .watcher.pid
|
||||||
|
|
||||||
|
python3 "$RUNTIME_DIR/function_server.py" > function_stdout.log 2>&1 &
|
||||||
|
echo $! > .function.pid
|
||||||
|
|
||||||
|
sleep 1
|
||||||
|
python3 "$RUNTIME_DIR/poc_watch_and_invoke.py" "$ALIAS" "$BUCKET" > watcher_stdout.log 2>&1 &
|
||||||
|
echo $! > .watcher.pid
|
||||||
|
|
||||||
|
sleep 2
|
||||||
|
printf "demo-%s\n" "$(date -u +%s)" > /tmp/poc_object.txt
|
||||||
|
mc cp /tmp/poc_object.txt "$ALIAS/$BUCKET/poc/demo-$(date -u +%s).txt" >/dev/null
|
||||||
|
|
||||||
|
sleep 3
|
||||||
|
|
||||||
|
echo "=== function.log ==="
|
||||||
|
if [[ -f function.log ]]; then
|
||||||
|
cat function.log
|
||||||
|
else
|
||||||
|
echo "function.log is empty"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "=== watcher_stdout.log (tail) ==="
|
||||||
|
tail -n 20 watcher_stdout.log || true
|
||||||
|
|
||||||
|
echo "Done. To stop background processes:"
|
||||||
|
WATCHER_PID="$(cat .watcher.pid)"
|
||||||
|
FUNCTION_PID="$(cat .function.pid)"
|
||||||
|
echo " kill $WATCHER_PID $FUNCTION_PID"
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
if [[ $# -lt 2 ]]; then
|
||||||
|
echo "usage: single_apply_demo.sh <s3cfg_path> <bucket> [timeout_sec]"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
S3CFG="$1"
|
||||||
|
BUCKET="$2"
|
||||||
|
TIMEOUT_SEC="${3:-25}"
|
||||||
|
|
||||||
|
WORKDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||||
|
ALIAS="applypoc"
|
||||||
|
|
||||||
|
cleanup() {
|
||||||
|
set +e
|
||||||
|
if [[ -f "$WORKDIR/.watcher.pid" ]]; then kill "$(cat "$WORKDIR/.watcher.pid")" >/dev/null 2>&1 || true; fi
|
||||||
|
if [[ -f "$WORKDIR/.function.pid" ]]; then kill "$(cat "$WORKDIR/.function.pid")" >/dev/null 2>&1 || true; fi
|
||||||
|
mc alias rm "$ALIAS" >/dev/null 2>&1 || true
|
||||||
|
}
|
||||||
|
trap cleanup EXIT
|
||||||
|
|
||||||
|
if ! command -v mc >/dev/null 2>&1; then
|
||||||
|
echo "FAILED: mc is not installed"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ ! -f "$S3CFG" ]]; then
|
||||||
|
echo "FAILED: S3 cfg not found: $S3CFG"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
readarray -t CREDS < <(python3 - <<PY
|
||||||
|
import configparser
|
||||||
|
cfg=configparser.RawConfigParser(); cfg.read("$S3CFG")
|
||||||
|
sec=cfg["default"]
|
||||||
|
print(sec.get("access_key",""))
|
||||||
|
print(sec.get("secret_key",""))
|
||||||
|
print(sec.get("host_base",""))
|
||||||
|
PY
|
||||||
|
)
|
||||||
|
ACCESS_KEY="${CREDS[0]:-}"
|
||||||
|
SECRET_KEY="${CREDS[1]:-}"
|
||||||
|
HOST_BASE="${CREDS[2]:-}"
|
||||||
|
|
||||||
|
if [[ -z "$ACCESS_KEY" || -z "$SECRET_KEY" || -z "$HOST_BASE" ]]; then
|
||||||
|
echo "FAILED: invalid credentials in $S3CFG"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
mc alias set "$ALIAS" "https://$HOST_BASE" "$ACCESS_KEY" "$SECRET_KEY" >/dev/null
|
||||||
|
|
||||||
|
cd "$WORKDIR"
|
||||||
|
rm -f function.log function_stdout.log watcher_stdout.log .function.pid .watcher.pid apply_confirmation.txt
|
||||||
|
|
||||||
|
python3 "$WORKDIR/scripts/function_server.py" > "$WORKDIR/function_stdout.log" 2>&1 &
|
||||||
|
echo $! > "$WORKDIR/.function.pid"
|
||||||
|
|
||||||
|
sleep 1
|
||||||
|
python3 "$WORKDIR/scripts/poc_watch_and_invoke.py" "$ALIAS" "$BUCKET" > "$WORKDIR/watcher_stdout.log" 2>&1 &
|
||||||
|
echo $! > "$WORKDIR/.watcher.pid"
|
||||||
|
|
||||||
|
sleep 2
|
||||||
|
OBJ="poc/terraform-apply-$(date -u +%s).txt"
|
||||||
|
printf "poc-%s\n" "$(date -u +%s)" > /tmp/s3_event_poc_payload.txt
|
||||||
|
mc cp /tmp/s3_event_poc_payload.txt "$ALIAS/$BUCKET/$OBJ" >/dev/null
|
||||||
|
|
||||||
|
FOUND=0
|
||||||
|
for _i in $(seq 1 "$TIMEOUT_SEC"); do
|
||||||
|
if [[ -s "$WORKDIR/function.log" ]] && grep -q "eventName" "$WORKDIR/function.log"; then
|
||||||
|
FOUND=1
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
sleep 1
|
||||||
|
done
|
||||||
|
|
||||||
|
if [[ "$FOUND" -eq 1 ]]; then
|
||||||
|
MSG="CONFIRMED: bucket event reached function for bucket=$BUCKET object=$OBJ"
|
||||||
|
echo "$MSG"
|
||||||
|
echo "$MSG" > "$WORKDIR/apply_confirmation.txt"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "FAILED: no event received by function within ${TIMEOUT_SEC}s"
|
||||||
|
echo "--- function_stdout.log ---"
|
||||||
|
tail -n 40 "$WORKDIR/function_stdout.log" || true
|
||||||
|
echo "--- watcher_stdout.log ---"
|
||||||
|
tail -n 40 "$WORKDIR/watcher_stdout.log" || true
|
||||||
|
exit 1
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
# Используйте существующий token/s3_uid из:
|
||||||
|
# TEST_STAND/POSTGRES/terraform.tfvars
|
||||||
|
|
||||||
|
api_token = "<token>"
|
||||||
|
api_endpoint = "https://deck-api-test.ngcloud.ru/api/v1/index.cfm"
|
||||||
|
s3_uid = "332cdb0d-34bf-43bf-864d-4adcc3b556fb"
|
||||||
|
bucket_name = "poc-s3-event-demo-001"
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
variable "api_token" {
|
||||||
|
description = "Nubes API token"
|
||||||
|
type = string
|
||||||
|
sensitive = true
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "api_endpoint" {
|
||||||
|
description = "Deck API endpoint"
|
||||||
|
type = string
|
||||||
|
default = "https://deck-api-test.ngcloud.ru/api/v1/index.cfm"
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "s3_uid" {
|
||||||
|
description = "UID существующего nubes_s3 (root S3 service)"
|
||||||
|
type = string
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "bucket_name" {
|
||||||
|
description = "Имя создаваемого бакета"
|
||||||
|
type = string
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "s3cfg_path" {
|
||||||
|
description = "Путь до .s3cfg_registry для доступа к S3"
|
||||||
|
type = string
|
||||||
|
default = "/home/naeel/remote_dev/terraform/secrets/.s3cfg_registry"
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "demo_timeout_sec" {
|
||||||
|
description = "Таймаут ожидания события в функции"
|
||||||
|
type = number
|
||||||
|
default = 25
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
terraform {
|
||||||
|
required_version = ">= 1.5.0"
|
||||||
|
|
||||||
|
required_providers {
|
||||||
|
nubes = {
|
||||||
|
source = "terra.k8c.ru/nubes/nubes"
|
||||||
|
version = "2.1.23"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user