diff --git a/.gitignore b/.gitignore index 7d20b95..6f18c35 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,7 @@ ggcode_modules/ # Expamples target directory -examples/ +/app/ # Lock file ggcode-info.lock diff --git a/actions/generate.luau b/actions/generate.luau index 6b68a09..8464e7e 100644 --- a/actions/generate.luau +++ b/actions/generate.luau @@ -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', @@ -18,7 +19,7 @@ local datasource_app = { } ggcode.generate "@/compose" { - target = '@examples', + target = '@app', variables = { config = { include = { @@ -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'); @@ -46,30 +48,33 @@ 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), @@ -77,7 +82,7 @@ ggcode.generate "@/pgadmin" { } ggcode.generate "@/postgres" { - target = '@examples', + target = '@app', variables = postgres.build { postgres.Database:from_datasource(datasource_keycloak), postgres.Database:from_datasource(datasource_app), @@ -85,14 +90,14 @@ ggcode.generate "@/postgres" { } 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'), @@ -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" }, @@ -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() +} diff --git a/ggcode-info.yaml b/ggcode-info.yaml index 1b51582..1053d6d 100644 --- a/ggcode-info.yaml +++ b/ggcode-info.yaml @@ -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 @@ -29,5 +31,5 @@ repositories: - name: core uri: git@github.com:ntr1x/ggcode-repo-core.git targets: -- name: '@examples' - path: examples +- name: '@app' + path: app diff --git a/lib/compose/app.luau b/lib/compose/app.luau new file mode 100644 index 0000000..11df4e6 --- /dev/null +++ b/lib/compose/app.luau @@ -0,0 +1,5 @@ +local Service = require('compose/app/Service') + +return { + Service = Service, +} diff --git a/lib/compose/app/Name.luau b/lib/compose/app/Name.luau new file mode 100644 index 0000000..d9c0d3e --- /dev/null +++ b/lib/compose/app/Name.luau @@ -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 diff --git a/lib/compose/app/Service.luau b/lib/compose/app/Service.luau new file mode 100644 index 0000000..1a17639 --- /dev/null +++ b/lib/compose/app/Service.luau @@ -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 diff --git a/scrolls/app/templates/!trace.luau b/scrolls/app/templates/!trace.luau new file mode 100644 index 0000000..97537f6 --- /dev/null +++ b/scrolls/app/templates/!trace.luau @@ -0,0 +1,5 @@ +-- print(yaml:stringify({ +-- bundle = bundle, +-- service = service, +-- volumes = volumes, +-- })) diff --git a/scrolls/app/templates/compose/{bundle.name}/{service.name}.compose.yaml.luau b/scrolls/app/templates/compose/{bundle.name}/{service.name}.compose.yaml.luau new file mode 100644 index 0000000..cf0cf26 --- /dev/null +++ b/scrolls/app/templates/compose/{bundle.name}/{service.name}.compose.yaml.luau @@ -0,0 +1,8 @@ +local config = { + services = { + [service.name] = service.manifest + }, + volumes = volumes, +} + +template:println(yaml:stringify(config)) diff --git a/scrolls/app/variables/bundle.yaml b/scrolls/app/variables/bundle.yaml new file mode 100644 index 0000000..7c7e49c --- /dev/null +++ b/scrolls/app/variables/bundle.yaml @@ -0,0 +1 @@ +name: app diff --git a/scrolls/app/variables/service.yaml b/scrolls/app/variables/service.yaml new file mode 100644 index 0000000..8a1f707 --- /dev/null +++ b/scrolls/app/variables/service.yaml @@ -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: {} diff --git a/scrolls/haproxy/variables/service.yaml b/scrolls/haproxy/variables/service.yaml index 9955cbd..7980017 100644 --- a/scrolls/haproxy/variables/service.yaml +++ b/scrolls/haproxy/variables/service.yaml @@ -13,6 +13,7 @@ manifest: - "host.docker.internal:host-gateway" profiles: - env + - app ports: - "80:80" - "8404:8404" diff --git a/scrolls/kafka/variables/service.yaml b/scrolls/kafka/variables/service.yaml index a3ccfd1..29ad57c 100644 --- a/scrolls/kafka/variables/service.yaml +++ b/scrolls/kafka/variables/service.yaml @@ -10,6 +10,7 @@ manifest: - env_kafka:/bitnami/kafka profiles: - env + - app ports: - "9092:9092" - "29092:29092" diff --git a/scrolls/kafka_ui/variables/service.yaml b/scrolls/kafka_ui/variables/service.yaml index e2e04db..4a2ecf3 100644 --- a/scrolls/kafka_ui/variables/service.yaml +++ b/scrolls/kafka_ui/variables/service.yaml @@ -10,6 +10,7 @@ manifest: - env_kafka profiles: - env + - admin environment: KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS: env_kafka:29092 KAFKA_CLUSTERS_0_NAME: local diff --git a/scrolls/keycloak/variables/service.yaml b/scrolls/keycloak/variables/service.yaml index 3a35a3d..37e5325 100644 --- a/scrolls/keycloak/variables/service.yaml +++ b/scrolls/keycloak/variables/service.yaml @@ -13,6 +13,7 @@ manifest: - env_postgres profiles: - env + - app environment: KEYCLOAK_ADMIN: admin KEYCLOAK_ADMIN_PASSWORD: GfhjkmRfhjkm1 diff --git a/scrolls/pgadmin/variables/service.yaml b/scrolls/pgadmin/variables/service.yaml index 22316d1..b1f38ec 100644 --- a/scrolls/pgadmin/variables/service.yaml +++ b/scrolls/pgadmin/variables/service.yaml @@ -12,6 +12,7 @@ manifest: - env_postgres profiles: - env + - admin environment: PGADMIN_DEFAULT_EMAIL: admin@example.com PGADMIN_DEFAULT_PASSWORD: admin diff --git a/scrolls/postgres/variables/service.yaml b/scrolls/postgres/variables/service.yaml index c73c2a9..32fd54a 100644 --- a/scrolls/postgres/variables/service.yaml +++ b/scrolls/postgres/variables/service.yaml @@ -11,6 +11,7 @@ manifest: - env_postgres:/var/lib/postgresql/data profiles: - env + - app ports: - "5432:5432" environment: diff --git a/scrolls/swagger_ui/variables/service.yaml b/scrolls/swagger_ui/variables/service.yaml index d4e27cc..6a761bc 100644 --- a/scrolls/swagger_ui/variables/service.yaml +++ b/scrolls/swagger_ui/variables/service.yaml @@ -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: ""