import { StackContext, Api, Config } from 'sst/constructs';
export function API({ stack }: StackContext) {
// Environment variables
const DATABASE_URL = new Config.Secret(stack, 'DATABASE_URL');
const API_TOKEN = new Config.Secret(stack, 'SG_CARS_TRENDS_API_TOKEN');
const REDIS_URL = new Config.Secret(stack, 'UPSTASH_REDIS_REST_URL');
const REDIS_TOKEN = new Config.Secret(stack, 'UPSTASH_REDIS_REST_TOKEN');
// API Gateway
const api = new Api(stack, 'Api', {
runtime: 'nodejs18.x',
architecture: 'arm64',
timeout: '30 seconds',
memorySize: '1024 MB',
environment: {
NODE_ENV: stack.stage === 'prod' ? 'production' : 'development',
},
bind: [DATABASE_URL, API_TOKEN, REDIS_URL, REDIS_TOKEN],
routes: {
'ANY /': 'src/index.handler',
},
customDomain: {
domainName: stack.stage === 'prod'
? 'api.sgcarstrends.com'
: `api-${stack.stage}.sgcarstrends.com`,
hostedZone: 'sgcarstrends.com',
},
});
// Output API URL
stack.addOutputs({
ApiEndpoint: api.url,
CustomDomain: api.customDomainUrl,
});
return { api };
}