cloud.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { WebhookEndpoint } from "pulumi-stripe"
  2. import { domain } from "./stage"
  3. import { web } from "./app"
  4. ////////////////
  5. // DATABASE
  6. ////////////////
  7. const DATABASE_USERNAME = new sst.Secret("DATABASE_USERNAME")
  8. const DATABASE_PASSWORD = new sst.Secret("DATABASE_PASSWORD")
  9. export const database = new sst.Linkable("Database", {
  10. properties: {
  11. host: "aws-us-east-2-1.pg.psdb.cloud",
  12. database: "postgres",
  13. username: DATABASE_USERNAME.value,
  14. password: DATABASE_PASSWORD.value,
  15. port: 5432,
  16. },
  17. })
  18. new sst.x.DevCommand("Studio", {
  19. link: [database],
  20. dev: {
  21. command: "bun db studio",
  22. directory: "cloud/core",
  23. autostart: true,
  24. },
  25. })
  26. ////////////////
  27. // AUTH
  28. ////////////////
  29. const GITHUB_CLIENT_ID_CONSOLE = new sst.Secret("GITHUB_CLIENT_ID_CONSOLE")
  30. const GITHUB_CLIENT_SECRET_CONSOLE = new sst.Secret("GITHUB_CLIENT_SECRET_CONSOLE")
  31. const GOOGLE_CLIENT_ID = new sst.Secret("GOOGLE_CLIENT_ID")
  32. const authStorage = new sst.cloudflare.Kv("AuthStorage")
  33. export const auth = new sst.cloudflare.Worker("AuthApi", {
  34. domain: `auth.${domain}`,
  35. handler: "cloud/function/src/auth.ts",
  36. url: true,
  37. link: [database, authStorage, GITHUB_CLIENT_ID_CONSOLE, GITHUB_CLIENT_SECRET_CONSOLE, GOOGLE_CLIENT_ID],
  38. })
  39. ////////////////
  40. // GATEWAY
  41. ////////////////
  42. export const stripeWebhook = new WebhookEndpoint("StripeWebhook", {
  43. url: $interpolate`https://api.gateway.${domain}/stripe/webhook`,
  44. enabledEvents: [
  45. "checkout.session.async_payment_failed",
  46. "checkout.session.async_payment_succeeded",
  47. "checkout.session.completed",
  48. "checkout.session.expired",
  49. "customer.created",
  50. "customer.deleted",
  51. "customer.updated",
  52. "customer.discount.created",
  53. "customer.discount.deleted",
  54. "customer.discount.updated",
  55. "customer.source.created",
  56. "customer.source.deleted",
  57. "customer.source.expiring",
  58. "customer.source.updated",
  59. "customer.subscription.created",
  60. "customer.subscription.deleted",
  61. "customer.subscription.paused",
  62. "customer.subscription.pending_update_applied",
  63. "customer.subscription.pending_update_expired",
  64. "customer.subscription.resumed",
  65. "customer.subscription.trial_will_end",
  66. "customer.subscription.updated",
  67. "customer.tax_id.created",
  68. "customer.tax_id.deleted",
  69. "customer.tax_id.updated",
  70. ],
  71. })
  72. const ANTHROPIC_API_KEY = new sst.Secret("ANTHROPIC_API_KEY")
  73. const OPENAI_API_KEY = new sst.Secret("OPENAI_API_KEY")
  74. const ZHIPU_API_KEY = new sst.Secret("ZHIPU_API_KEY")
  75. const STRIPE_SECRET_KEY = new sst.Secret("STRIPE_SECRET_KEY")
  76. const AUTH_API_URL = new sst.Linkable("AUTH_API_URL", {
  77. properties: { value: auth.url.apply((url) => url!) },
  78. })
  79. const STRIPE_WEBHOOK_SECRET = new sst.Linkable("STRIPE_WEBHOOK_SECRET", {
  80. properties: { value: stripeWebhook.secret },
  81. })
  82. export const gateway = new sst.cloudflare.Worker("GatewayApi", {
  83. domain: `api.gateway.${domain}`,
  84. handler: "cloud/function/src/gateway.ts",
  85. url: true,
  86. link: [
  87. database,
  88. AUTH_API_URL,
  89. STRIPE_WEBHOOK_SECRET,
  90. STRIPE_SECRET_KEY,
  91. ANTHROPIC_API_KEY,
  92. OPENAI_API_KEY,
  93. ZHIPU_API_KEY,
  94. ],
  95. })
  96. ////////////////
  97. // CONSOLE
  98. ////////////////
  99. export const console = new sst.cloudflare.x.StaticSite("Console", {
  100. domain: `console.${domain}`,
  101. path: "cloud/web",
  102. build: {
  103. command: "bun run build",
  104. output: "dist/client",
  105. },
  106. environment: {
  107. VITE_DOCS_URL: web.url.apply((url) => url!),
  108. VITE_API_URL: gateway.url.apply((url) => url!),
  109. VITE_AUTH_URL: auth.url.apply((url) => url!),
  110. },
  111. })