Skip to content

Commit 4024e6b

Browse files
committed
fix: add CORS config and cross-origin API URL for production deployment
- add CorsConfig.java backend bean to allow frontend App Service origin - update programService.ts to use VITE_API_BASE_URL env var - set VITE_API_BASE_URL to backend URL and APP_CORS_ALLOWED_ORIGIN in cd.yml - update application-azuresql.yml to read CORS origin from env var 🔧 - Generated by Copilot
1 parent 7a834ed commit 4024e6b

4 files changed

Lines changed: 37 additions & 3 deletions

File tree

.github/workflows/cd.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ jobs:
8080
- name: Build for production
8181
run: npm run build
8282
env:
83-
VITE_API_BASE_URL: /api
83+
VITE_API_BASE_URL: https://${{ env.BACKEND_APP_NAME }}.azurewebsites.net/api
8484

8585
- name: Upload frontend artifact
8686
uses: actions/upload-artifact@v4
@@ -126,6 +126,7 @@ jobs:
126126
--settings \
127127
SPRING_PROFILES_ACTIVE=azuresql \
128128
SPRING_DATASOURCE_URL="jdbc:sqlserver://${{ env.SQL_SERVER_NAME }}.database.windows.net:1433;database=${{ env.SQL_DATABASE_NAME }};encrypt=true;trustServerCertificate=true;hostNameInCertificate=*.database.windows.net;loginTimeout=60;authentication=ActiveDirectoryManagedIdentity;msiClientId=${{ vars.SQL_IDENTITY_CLIENT_ID }};connectRetryCount=3;connectRetryInterval=10;" \
129+
APP_CORS_ALLOWED_ORIGIN="https://${{ env.FRONTEND_APP_NAME }}.azurewebsites.net" \
129130
--only-show-errors
130131
131132
- name: Restart App Service
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.ontario.program.config;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.cors.CorsConfiguration;
7+
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
8+
import org.springframework.web.filter.CorsFilter;
9+
10+
/**
11+
* CORS configuration — allows the frontend origin to call the API
12+
* when frontend and backend are on different App Service instances.
13+
*/
14+
@Configuration
15+
public class CorsConfig {
16+
17+
@Value("${app.cors.allowed-origin:http://localhost:3000}")
18+
private String allowedOrigin;
19+
20+
@Bean
21+
public CorsFilter corsFilter() {
22+
final CorsConfiguration config = new CorsConfiguration();
23+
config.addAllowedOrigin(allowedOrigin);
24+
config.addAllowedHeader("*");
25+
config.addAllowedMethod("*");
26+
config.setAllowCredentials(true);
27+
config.setMaxAge(3600L);
28+
29+
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
30+
source.registerCorsConfiguration("/api/**", config);
31+
return new CorsFilter(source);
32+
}
33+
}

backend/src/main/resources/application-azuresql.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ server:
4646

4747
app:
4848
cors:
49-
allowed-origin: http://localhost:3000
49+
allowed-origin: ${APP_CORS_ALLOWED_ORIGIN:http://localhost:3000}
5050

5151
logging:
5252
level:

frontend/src/services/programService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import axios from 'axios';
22

33
const api = axios.create({
4-
baseURL: '/api',
4+
baseURL: import.meta.env.VITE_API_BASE_URL || '/api',
55
headers: {
66
'Content-Type': 'application/json',
77
},

0 commit comments

Comments
 (0)