Skip to content

Commit

Permalink
#1 Implement ability to configure spring-based docker containers
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Pavlenko committed May 5, 2024
1 parent 6f489bb commit a025587
Show file tree
Hide file tree
Showing 17 changed files with 199 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
ggcode_modules/

# Expamples target directory
examples/
/app/

# Lock file
ggcode-info.lock
50 changes: 36 additions & 14 deletions actions/generate.luau
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local postgres = require('compose/postgres')
local pgadmin = require('compose/pgadmin')
local keycloak = require('compose/keycloak')
local kcadm = require('compose/kcadm')
local app = require('compose/app')

local datasource_keycloak = {
uri = 'jdbc:postgresql://env_postgres:5432/keycloak',
Expand All @@ -18,7 +19,7 @@ local datasource_app = {
}

ggcode.generate "@/compose" {
target = '@examples',
target = '@app',
variables = {
config = {
include = {
Expand All @@ -30,13 +31,14 @@ ggcode.generate "@/compose" {
{ path = "compose/env/env_swagger_ui.compose.yaml" },
{ path = "compose/env/env_keycloak.compose.yaml" },
{ path = "compose/env/env_kcadm.compose.yaml" },
{ path = "compose/app/app_assembly_web.compose.yaml" },
}
}
}
}

ggcode.generate "@/haproxy" {
target = '@examples',
target = '@app',
variables = haproxy.build {
haproxy.Route:from_arrow('http://kafka-ui.local.example.com', 'http://env_kafka_ui:8080');
haproxy.Route:from_arrow('http://pgadmin.local.example.com', 'http://env_pgadmin:80');
Expand All @@ -46,53 +48,56 @@ ggcode.generate "@/haproxy" {
haproxy.Route:from_uri_string('http://api.local.example.com')
:with_name('api')
:with_proxies({
haproxy.Proxy:from_path_string('/api/assembly_web')
:with_cors('*')
:with_server(haproxy.Server:from_uri_string('http://app_assembly_web:8080'):with_optional(true));
haproxy.Proxy:from_path_string('/api/service_catalog')
:with_cors('*')
:with_server(haproxy.Server:from_uri_string('http://host.docker.internal:8080'):with_optional(true));
:with_server(haproxy.Server:from_uri_string('http://host.docker.internal:8081'):with_optional(true));
haproxy.Proxy:from_path_string('/api/service_customers')
:with_cors('*')
:with_server(haproxy.Server:from_uri_string('http://host.docker.internal:8081'):with_optional(true));
:with_server(haproxy.Server:from_uri_string('http://host.docker.internal:8082'):with_optional(true));
haproxy.Proxy:from_path_string('/api/service_basket')
:with_cors('*')
:with_server(haproxy.Server:from_uri_string('http://host.docker.internal:8082'):with_optional(true));
:with_server(haproxy.Server:from_uri_string('http://host.docker.internal:8083'):with_optional(true));
haproxy.Proxy:from_path_string('/api/service_payments')
:with_cors('*')
:with_server(haproxy.Server:from_uri_string('http://host.docker.internal:8083'):with_optional(true));
:with_server(haproxy.Server:from_uri_string('http://host.docker.internal:8084'):with_optional(true));
haproxy.Proxy:from_path_string('/api/service_events')
:with_cors('*')
:with_server(haproxy.Server:from_uri_string('http://host.docker.internal:8084'):with_optional(true));
:with_server(haproxy.Server:from_uri_string('http://host.docker.internal:8085'):with_optional(true));
});
}
}

ggcode.generate "@/kafka" { target = '@examples' }
ggcode.generate "@/kafka_ui" { target = '@examples' }
ggcode.generate "@/kafka" { target = '@app' }
ggcode.generate "@/kafka_ui" { target = '@app' }

ggcode.generate "@/pgadmin" {
target = '@examples',
target = '@app',
variables = pgadmin.build {
pgadmin.Server:from_datasource(datasource_keycloak),
pgadmin.Server:from_datasource(datasource_app),
}
}

ggcode.generate "@/postgres" {
target = '@examples',
target = '@app',
variables = postgres.build {
postgres.Database:from_datasource(datasource_keycloak),
postgres.Database:from_datasource(datasource_app),
}
}

ggcode.generate "@/keycloak" {
target = '@examples',
target = '@app',
variables = keycloak.build {
keycloak.Manifest:new():with_datasource(datasource_keycloak),
}
}

ggcode.generate "@/kcadm" {
target = '@examples',
target = '@app',
variables = kcadm.build {
kcadm.Realm:from('application'),

Expand Down Expand Up @@ -121,10 +126,11 @@ ggcode.generate "@/kcadm" {
}

ggcode.generate "@/swagger_ui" {
target = '@examples',
target = '@app',
variables = {
config = {
urls = {
{ name = "Assembly Web", url = "http://api.local.example.com/api/assembly_web/v3/api-docs" },
{ name = "Service Catalogue", url = "http://api.local.example.com/api/service_catalog/v3/api-docs" },
{ name = "Service Customers", url = "http://api.local.example.com/api/service_customers/v3/api-docs" },
{ name = "Service Basket", url = "http://api.local.example.com/api/service_basket/v3/api-docs" },
Expand All @@ -134,3 +140,19 @@ ggcode.generate "@/swagger_ui" {
}
}
}

local service_assembly_web =

ggcode.generate "@/app" {
target = '@app',
variables = app.Service
:from('app_assembly_web', 'example/assembly_web:1.0-SNAPSHOT')
:with_depends_on('env_kafka')
:with_depends_on('env_postgres')
:with_depends_on('env_keycloak')
:with_spring_datasource(datasource_app)
:with_env_variable('SPRING_KAFKA_BOOTSTRAP_SERVERS', 'env_kafka:29092')
:with_link('env_haproxy:auth.local.example.com')
-- :with_env_variable('SPRING_SECURITY_OAUTH2_RESOURCESERVER_JWT_ISSUERURI', 'http://env_keycloak:8080/auth/realms/application')
:unwrap()
}
6 changes: 4 additions & 2 deletions ggcode-info.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ actions:
- name: test/pgadmin
path: actions/test/pgadmin.luau
scrolls:
- name: app
path: scrolls/app
- name: compose
path: scrolls/compose
- name: haproxy
Expand All @@ -29,5 +31,5 @@ repositories:
- name: core
uri: [email protected]:ntr1x/ggcode-repo-core.git
targets:
- name: '@examples'
path: examples
- name: '@app'
path: app
5 changes: 5 additions & 0 deletions lib/compose/app.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
local Service = require('compose/app/Service')

return {
Service = Service,
}
27 changes: 27 additions & 0 deletions lib/compose/app/Name.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
local Meta = require('core/Meta')

local Name = {
__type = 'Name',
name = nil,
}

function Name:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end

function Name:from(name: string)
return Name:new({
name = name,
})
end

function Name:unwrap()
return {
name = self.name,
}
end

return Name
91 changes: 91 additions & 0 deletions lib/compose/app/Service.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
local Meta = require('core/Meta')

local Service = {
__type = 'Service',
name = nil,
image = nil,
environment = nil,
depends_on = nil,
links = nil,
}

function Service:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end

function Service:from(name: string, image: string, environment: table)
return Service:new({
name = name,
image = image,
depends_on = Meta:array {},
links = Meta:array {},
environment = environment or Meta:table {},
})
end

function Service:with_env_variable(name: string, value: string)
self.environment[name] = value
return self
end

function Service:with_spring_datasource(datasource: table)
self.environment.SPRING_DATASOURCE_URL = datasource.uri
self.environment.SPRING_DATASOURCE_USERNAME = datasource.username
self.environment.SPRING_DATASOURCE_PASSWORD = datasource.password
return self
end

function Service:with_env_variables(environment: table)
environment = environment or Meta:table {}

for name, value in environment do
self.environment[name] = value
end

return self
end

function Service:with_depends_on(name: string)
self.depends_on[#self.depends_on + 1] = name
return self
end

function Service:with_link(link: string)
self.links[#self.links + 1] = link
return self
end

function Service:unwrap_depends_on()
if #self.depends_on > 0 then
return self.depends_on
end

return nil
end

function Service:unwrap_links()
if #self.links > 0 then
return self.links
end

return nil
end

function Service:unwrap()
return {
service = {
name = self.name,
manifest = {
image = self.image,
environment = self.environment,
depends_on = self:unwrap_depends_on(),
links = self:unwrap_links()
}
}
}
end

return Service
5 changes: 5 additions & 0 deletions scrolls/app/templates/!trace.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- print(yaml:stringify({
-- bundle = bundle,
-- service = service,
-- volumes = volumes,
-- }))
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
local config = {
services = {
[service.name] = service.manifest
},
volumes = volumes,
}

template:println(yaml:stringify(config))
1 change: 1 addition & 0 deletions scrolls/app/variables/bundle.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name: app
14 changes: 14 additions & 0 deletions scrolls/app/variables/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: app_service_api
manifest:
image: example/service_api:1.0-SNAPSHOT
restart: unless-stopped
deploy:
resources:
limits:
memory: 600M
profiles:
- app
environment:
JAVA_OPTS: "-Xmx512m -Xms256m"
networks:
default: {}
1 change: 1 addition & 0 deletions scrolls/haproxy/variables/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ manifest:
- "host.docker.internal:host-gateway"
profiles:
- env
- app
ports:
- "80:80"
- "8404:8404"
Expand Down
1 change: 1 addition & 0 deletions scrolls/kafka/variables/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ manifest:
- env_kafka:/bitnami/kafka
profiles:
- env
- app
ports:
- "9092:9092"
- "29092:29092"
Expand Down
1 change: 1 addition & 0 deletions scrolls/kafka_ui/variables/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ manifest:
- env_kafka
profiles:
- env
- admin
environment:
KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS: env_kafka:29092
KAFKA_CLUSTERS_0_NAME: local
Expand Down
1 change: 1 addition & 0 deletions scrolls/keycloak/variables/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ manifest:
- env_postgres
profiles:
- env
- app
environment:
KEYCLOAK_ADMIN: admin
KEYCLOAK_ADMIN_PASSWORD: GfhjkmRfhjkm1
Expand Down
1 change: 1 addition & 0 deletions scrolls/pgadmin/variables/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ manifest:
- env_postgres
profiles:
- env
- admin
environment:
PGADMIN_DEFAULT_EMAIL: [email protected]
PGADMIN_DEFAULT_PASSWORD: admin
Expand Down
1 change: 1 addition & 0 deletions scrolls/postgres/variables/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ manifest:
- env_postgres:/var/lib/postgresql/data
profiles:
- env
- app
ports:
- "5432:5432"
environment:
Expand Down
1 change: 1 addition & 0 deletions scrolls/swagger_ui/variables/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ manifest:
- ../../config/env/env_swagger_ui/swagger-config.json:/usr/share/nginx/html/swagger-config.json
profiles:
- env
- app
environment:
CONFIG_URL: swagger-config.json
URL: ""
Expand Down

0 comments on commit a025587

Please sign in to comment.