feat: WhatsApp service skeleton, settings, gateway route and mock
Adds solvway-services-whatsapp with per-company WhatsApp settings (secrets masked), the gateway route /whatsapp/**, compose entries for the service and a local whatsapp-mock, the example env keys, and the service in CI. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
1 parent
6e5362fa2c
commit
3ac3e108ff
17 files changed
+309
-3
No files matched your search
@@ -41,6 +41,7 @@ jobs:
|
||||
service-member:solvway-services-member
|
||||
service-log:solvway-services-log
|
||||
service-email:solvway-services-email
|
||||
service-whatsapp:solvway-services-whatsapp
|
||||
admin-portal:solvway-admin-portal
|
||||
steps:
|
||||
- name: Checkout
|
||||
@@ -49,7 +50,7 @@ jobs:
|
||||
- name: Build and test the services
|
||||
run: |
|
||||
mvn -T 1C -B -Dmaven.repo.local=/root/.m2/repository-co-lite verify \
|
||||
-pl solvway-api-gateway,solvway-mobile-api,solvway-services-auth,solvway-services-system,solvway-services-counter,solvway-services-member,solvway-services-log,solvway-services-email -am
|
||||
-pl solvway-api-gateway,solvway-mobile-api,solvway-services-auth,solvway-services-system,solvway-services-counter,solvway-services-member,solvway-services-log,solvway-services-email,solvway-services-whatsapp -am
|
||||
|
||||
- name: Registry login
|
||||
env:
|
||||
|
||||
@@ -18,3 +18,15 @@ APIKEY=colite-local-gateway-key-000000000000
|
||||
# Inbound keys clients send to mobile-api: "<key>:<PLATFORM>" (APP or CP).
|
||||
AUTH_API_KEYS=colite-local-app-key:APP,colite-local-cp-key:CP
|
||||
JAVA_OPTS=-Xmx384m
|
||||
# WhatsApp inbox. Mock-safe dummy values; the service talks to whatsapp-mock, never to Meta.
|
||||
WHATSAPP_COMPANIES=INS,CV
|
||||
WHATSAPP_INS_PHONE_NUMBER_ID=100000000000001
|
||||
WHATSAPP_INS_WABA_ID=200000000000001
|
||||
WHATSAPP_INS_ACCESS_TOKEN=mock-token-ins
|
||||
WHATSAPP_INS_APP_SECRET=mock-secret-ins
|
||||
WHATSAPP_CV_PHONE_NUMBER_ID=100000000000002
|
||||
WHATSAPP_CV_WABA_ID=200000000000002
|
||||
WHATSAPP_CV_ACCESS_TOKEN=mock-token-cv
|
||||
WHATSAPP_CV_APP_SECRET=mock-secret-cv
|
||||
WHATSAPP_VERIFY_TOKEN=mock-verify
|
||||
WHATSAPP_GRAPH_BASE_URL=http://whatsapp-mock:8080
|
||||
@@ -112,5 +112,14 @@ services:
|
||||
mysql: {condition: service_healthy}
|
||||
redis: {condition: service_healthy}
|
||||
pubsub-init: {condition: service_completed_successfully}
|
||||
service-whatsapp:
|
||||
build:
|
||||
context: ../solvway-services-whatsapp
|
||||
args: {BUILD_PROFILE: local}
|
||||
env_file: .env
|
||||
depends_on:
|
||||
mysql: {condition: service_healthy}
|
||||
whatsapp-mock:
|
||||
build: ./whatsapp-mock
|
||||
volumes:
|
||||
mysql-data:
|
||||
@@ -0,0 +1,6 @@
|
||||
FROM node:24-alpine
|
||||
WORKDIR /app
|
||||
COPY server.js .
|
||||
EXPOSE 8080
|
||||
USER node
|
||||
CMD ["node", "server.js"]
|
||||
@@ -0,0 +1,11 @@
|
||||
// Local stand-in for the WhatsApp Cloud (Graph) API. Later tasks add Graph endpoints and signed webhooks.
|
||||
const http = require('node:http');
|
||||
|
||||
http.createServer((req, res) => {
|
||||
if (req.method === 'GET' && req.url === '/health') {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
return res.end('{"status":"UP"}');
|
||||
}
|
||||
res.writeHead(404, { 'Content-Type': 'application/json' });
|
||||
res.end('{"error":"not found"}');
|
||||
}).listen(8080);
|
||||
@@ -8,11 +8,11 @@ Verify commands:
|
||||
|
||||
## Wave 1 (serial: contracts and hotspots)
|
||||
|
||||
- [ ] 1.1 Schema and modules: `sql/01-whatsapp-schema.sql`, `sql/02-whatsapp-modules.sql` and `sql/99-rollback.sql` in this change folder, mirrored into `local/seed/`.
|
||||
- [x] 1.1 Schema and modules: `sql/01-whatsapp-schema.sql`, `sql/02-whatsapp-modules.sql` and `sql/99-rollback.sql` in this change folder, mirrored into `local/seed/`.
|
||||
- Depends on: none
|
||||
- Scope: `openspec/changes/add-whatsapp-inbox/sql/**`, `local/seed/**`
|
||||
- Verify: load the seed into a fresh local MySQL volume (compose `down -v` on a throwaway project name, then `up mysql`) and list the tables.
|
||||
- [ ] 1.2 Service skeleton: `solvway-services-whatsapp` module (pom, Dockerfile, `application.yml`, settings binding, health), root pom module entry, the gateway route `Path=/whatsapp/**`, and compose entries for the service and `whatsapp-mock`. The mock is a skeleton that answers health.
|
||||
- [x] 1.2 Service skeleton: `solvway-services-whatsapp` module (pom, Dockerfile, `application.yml`, settings binding, health), root pom module entry, the gateway route `Path=/whatsapp/**`, and compose entries for the service and `whatsapp-mock`. The mock is a skeleton that answers health.
|
||||
- Depends on: 1.1
|
||||
- Scope: `solvway-services-whatsapp/**`, `pom.xml`, `solvway-api-gateway/src/main/resources/application.yml`, `local/docker-compose.yml`, `local/whatsapp-mock/**`, `local/.env.example`
|
||||
- Verify: `mvn -T 1C -q -pl solvway-services-whatsapp,solvway-api-gateway verify`
|
||||
|
||||
@@ -19,5 +19,6 @@
|
||||
<module>solvway-services-member</module>
|
||||
<module>solvway-services-log</module>
|
||||
<module>solvway-services-email</module>
|
||||
<module>solvway-services-whatsapp</module>
|
||||
</modules>
|
||||
</project>
|
||||
@@ -19,6 +19,7 @@ app:
|
||||
name:
|
||||
auth: service-auth
|
||||
email: service-email
|
||||
whatsapp: service-whatsapp
|
||||
system: service-system
|
||||
member: service-member
|
||||
log: service-log
|
||||
@@ -176,6 +177,10 @@ spring:
|
||||
uri: http://${app.services.name.email}
|
||||
predicates:
|
||||
- Path=/email/**
|
||||
- id: whatsapp
|
||||
uri: http://${app.services.name.whatsapp}
|
||||
predicates:
|
||||
- Path=/whatsapp/**
|
||||
- id: printTicket
|
||||
uri: http://${app.services.name.system}
|
||||
predicates:
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
# The image needs only the built jar; nothing else from the module enters the build context.
|
||||
*
|
||||
!target/*.jar
|
||||
@@ -0,0 +1,4 @@
|
||||
target/
|
||||
.idea
|
||||
*.iml
|
||||
.vscode/
|
||||
@@ -0,0 +1,8 @@
|
||||
FROM gitea.containerverse.dev/solvway/docker-java-21
|
||||
ARG BUILD_PROFILE
|
||||
ENV BUILD_PROFILE ${BUILD_PROFILE:-default}
|
||||
VOLUME /tmp
|
||||
ADD target/*.jar app.jar
|
||||
RUN sh -c 'touch /app.jar'
|
||||
ENV JAVA_OPTS=""
|
||||
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Dspring.profiles.active=$BUILD_PROFILE -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
|
||||
@@ -0,0 +1,54 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.2.8</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<groupId>com.solvway.services.whatsapp</groupId>
|
||||
<artifactId>solvway-services-whatsapp</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>solvway-services-whatsapp</name>
|
||||
<description>WhatsApp inbox service</description>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>21</java.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.solvway.services.whatsapp;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SolvwayServicesWhatsappApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SolvwayServicesWhatsappApplication.class, args);
|
||||
}
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package com.solvway.services.whatsapp.config;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* WhatsApp Cloud API settings from env: WHATSAPP_COMPANIES (comma-separated codes) and, per code,
|
||||
* WHATSAPP_<CODE>_PHONE_NUMBER_ID / _WABA_ID / _ACCESS_TOKEN / _APP_SECRET. No companies means WhatsApp is off.
|
||||
*/
|
||||
@Component
|
||||
public class WhatsAppSettings {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(WhatsAppSettings.class);
|
||||
private static final String[] KEYS = {"PHONE_NUMBER_ID", "WABA_ID", "ACCESS_TOKEN", "APP_SECRET"};
|
||||
|
||||
public record CompanyConfig(String code, String phoneNumberId, String wabaId, String accessToken, String appSecret) {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CompanyConfig[code=" + code + ", phoneNumberId=" + phoneNumberId + ", wabaId=" + wabaId + ", secrets=***]";
|
||||
}
|
||||
}
|
||||
|
||||
private final List<CompanyConfig> companies = new ArrayList<>();
|
||||
private final String verifyToken;
|
||||
private final String graphBaseUrl;
|
||||
|
||||
public WhatsAppSettings(Environment env) {
|
||||
for (String raw : env.getProperty("WHATSAPP_COMPANIES", "").split(",")) {
|
||||
String code = raw.trim().toUpperCase();
|
||||
if (code.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
String[] values = new String[KEYS.length];
|
||||
boolean complete = true;
|
||||
for (int i = 0; i < KEYS.length; i++) {
|
||||
String key = "WHATSAPP_" + code + "_" + KEYS[i];
|
||||
values[i] = env.getProperty(key);
|
||||
if (!StringUtils.hasText(values[i])) {
|
||||
log.warn("WhatsApp company {} skipped: {} is not set", code, key);
|
||||
complete = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (complete) {
|
||||
companies.add(new CompanyConfig(code, values[0], values[1], values[2], values[3]));
|
||||
}
|
||||
}
|
||||
verifyToken = env.getProperty("WHATSAPP_VERIFY_TOKEN", "");
|
||||
graphBaseUrl = env.getProperty("WHATSAPP_GRAPH_BASE_URL", "https://graph.facebook.com/v21.0");
|
||||
}
|
||||
|
||||
public List<CompanyConfig> companies() {
|
||||
return List.copyOf(companies);
|
||||
}
|
||||
|
||||
public Optional<CompanyConfig> findByPhoneNumberId(String phoneNumberId) {
|
||||
return companies.stream().filter(c -> c.phoneNumberId().equals(phoneNumberId)).findFirst();
|
||||
}
|
||||
|
||||
public String verifyToken() {
|
||||
return verifyToken;
|
||||
}
|
||||
|
||||
public String graphBaseUrl() {
|
||||
return graphBaseUrl;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
# Local docker-compose stack only (BUILD_PROFILE=local). WhatsApp settings come from WHATSAPP_* env (local/.env).
|
||||
@@ -0,0 +1,34 @@
|
||||
info:
|
||||
app:
|
||||
name: Solvway WhatsApp Service
|
||||
description: WhatsApp inbox service
|
||||
version: 1.0.0
|
||||
|
||||
management:
|
||||
health:
|
||||
probes:
|
||||
enabled: true
|
||||
db:
|
||||
enabled: true
|
||||
defaults:
|
||||
enabled: true
|
||||
endpoint:
|
||||
health:
|
||||
show-details: always
|
||||
server:
|
||||
port: 80
|
||||
servlet:
|
||||
encoding:
|
||||
charset: UTF-8
|
||||
force: true
|
||||
spring:
|
||||
datasource:
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://${MYSQLHOST}:${MYSQLPORT}/service-whatsapp?useSSL=false&allowPublicKeyRetrieval=true&sslMode=DISABLED&serverTimezone=Asia/Kuala_Lumpur&characterEncoding=UTF-8
|
||||
username: ${MYSQLUSER}
|
||||
password: ${MYSQLPWD}
|
||||
jpa:
|
||||
open-in-view: false
|
||||
threads:
|
||||
virtual:
|
||||
enabled: true
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
package com.solvway.services.whatsapp.config;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class WhatsAppSettingsTest {
|
||||
|
||||
private static MockEnvironment company(MockEnvironment env, String code, String phoneId) {
|
||||
return env.withProperty("WHATSAPP_" + code + "_PHONE_NUMBER_ID", phoneId)
|
||||
.withProperty("WHATSAPP_" + code + "_WABA_ID", "waba-" + code)
|
||||
.withProperty("WHATSAPP_" + code + "_ACCESS_TOKEN", "token-" + code)
|
||||
.withProperty("WHATSAPP_" + code + "_APP_SECRET", "secret-" + code);
|
||||
}
|
||||
|
||||
@Test
|
||||
void bindsTwoCompaniesAndLooksUpByPhoneNumberId() {
|
||||
MockEnvironment env = new MockEnvironment().withProperty("WHATSAPP_COMPANIES", "INS, cv");
|
||||
company(env, "INS", "111");
|
||||
company(env, "CV", "222");
|
||||
WhatsAppSettings s = new WhatsAppSettings(env);
|
||||
|
||||
assertThat(s.companies()).extracting(WhatsAppSettings.CompanyConfig::code).containsExactly("INS", "CV");
|
||||
WhatsAppSettings.CompanyConfig cv = s.findByPhoneNumberId("222").orElseThrow();
|
||||
assertThat(cv.code()).isEqualTo("CV");
|
||||
assertThat(cv.wabaId()).isEqualTo("waba-CV");
|
||||
assertThat(cv.accessToken()).isEqualTo("token-CV");
|
||||
assertThat(cv.appSecret()).isEqualTo("secret-CV");
|
||||
assertThat(s.findByPhoneNumberId("999")).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void skipsCompanyMissingAKey() {
|
||||
MockEnvironment env = new MockEnvironment().withProperty("WHATSAPP_COMPANIES", "INS,CV");
|
||||
company(env, "INS", "111");
|
||||
env.withProperty("WHATSAPP_CV_PHONE_NUMBER_ID", "222"); // the other three keys are missing
|
||||
WhatsAppSettings s = new WhatsAppSettings(env);
|
||||
|
||||
assertThat(s.companies()).extracting(WhatsAppSettings.CompanyConfig::code).containsExactly("INS");
|
||||
assertThat(s.findByPhoneNumberId("222")).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyWhenNoWhatsAppEnv() {
|
||||
WhatsAppSettings s = new WhatsAppSettings(new MockEnvironment());
|
||||
|
||||
assertThat(s.companies()).isEmpty();
|
||||
assertThat(s.verifyToken()).isEmpty();
|
||||
assertThat(s.graphBaseUrl()).isEqualTo("https://graph.facebook.com/v21.0");
|
||||
}
|
||||
|
||||
@Test
|
||||
void readsVerifyTokenAndGraphBaseUrl() {
|
||||
WhatsAppSettings s = new WhatsAppSettings(new MockEnvironment()
|
||||
.withProperty("WHATSAPP_VERIFY_TOKEN", "vt")
|
||||
.withProperty("WHATSAPP_GRAPH_BASE_URL", "http://whatsapp-mock:8080"));
|
||||
|
||||
assertThat(s.verifyToken()).isEqualTo("vt");
|
||||
assertThat(s.graphBaseUrl()).isEqualTo("http://whatsapp-mock:8080");
|
||||
}
|
||||
|
||||
@Test
|
||||
void toStringMasksSecrets() {
|
||||
MockEnvironment env = new MockEnvironment().withProperty("WHATSAPP_COMPANIES", "INS");
|
||||
company(env, "INS", "111");
|
||||
String text = new WhatsAppSettings(env).companies().get(0).toString();
|
||||
|
||||
assertThat(text).contains("INS", "111").doesNotContain("token-INS").doesNotContain("secret-INS");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user