console.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import { domain } from "./stage"
  2. import { EMAILOCTOPUS_API_KEY } from "./app"
  3. import { SECRET } from "./secret"
  4. ////////////////
  5. // DATABASE
  6. ////////////////
  7. const cluster = planetscale.getDatabaseOutput({
  8. name: "opencode",
  9. organization: "anomalyco",
  10. })
  11. const branch =
  12. $app.stage === "production"
  13. ? planetscale.getBranchOutput({
  14. name: "production",
  15. organization: cluster.organization,
  16. database: cluster.name,
  17. })
  18. : new planetscale.Branch("DatabaseBranch", {
  19. database: cluster.name,
  20. organization: cluster.organization,
  21. name: $app.stage,
  22. parentBranch: "production",
  23. })
  24. const password = new planetscale.Password("DatabasePassword", {
  25. name: $app.stage,
  26. database: cluster.name,
  27. organization: cluster.organization,
  28. branch: branch.name,
  29. })
  30. export const database = new sst.Linkable("Database", {
  31. properties: {
  32. host: password.accessHostUrl,
  33. database: cluster.name,
  34. username: password.username,
  35. password: password.plaintext,
  36. port: 3306,
  37. },
  38. })
  39. new sst.x.DevCommand("Studio", {
  40. link: [database],
  41. dev: {
  42. command: "bun db studio",
  43. directory: "packages/console/core",
  44. autostart: true,
  45. },
  46. })
  47. ////////////////
  48. // AUTH
  49. ////////////////
  50. const GITHUB_CLIENT_ID_CONSOLE = new sst.Secret("GITHUB_CLIENT_ID_CONSOLE")
  51. const GITHUB_CLIENT_SECRET_CONSOLE = new sst.Secret("GITHUB_CLIENT_SECRET_CONSOLE")
  52. const GOOGLE_CLIENT_ID = new sst.Secret("GOOGLE_CLIENT_ID")
  53. const authStorage = new sst.cloudflare.Kv("AuthStorage")
  54. export const auth = new sst.cloudflare.Worker("AuthApi", {
  55. domain: `auth.${domain}`,
  56. handler: "packages/console/function/src/auth.ts",
  57. url: true,
  58. link: [database, authStorage, GITHUB_CLIENT_ID_CONSOLE, GITHUB_CLIENT_SECRET_CONSOLE, GOOGLE_CLIENT_ID],
  59. })
  60. ////////////////
  61. // GATEWAY
  62. ////////////////
  63. export const stripeWebhook = new stripe.WebhookEndpoint("StripeWebhookEndpoint", {
  64. url: $interpolate`https://${domain}/stripe/webhook`,
  65. enabledEvents: [
  66. "checkout.session.async_payment_failed",
  67. "checkout.session.async_payment_succeeded",
  68. "checkout.session.completed",
  69. "checkout.session.expired",
  70. "charge.refunded",
  71. "invoice.payment_succeeded",
  72. "invoice.payment_failed",
  73. "invoice.payment_action_required",
  74. "customer.created",
  75. "customer.deleted",
  76. "customer.updated",
  77. "customer.discount.created",
  78. "customer.discount.deleted",
  79. "customer.discount.updated",
  80. "customer.source.created",
  81. "customer.source.deleted",
  82. "customer.source.expiring",
  83. "customer.source.updated",
  84. "customer.subscription.created",
  85. "customer.subscription.deleted",
  86. "customer.subscription.paused",
  87. "customer.subscription.pending_update_applied",
  88. "customer.subscription.pending_update_expired",
  89. "customer.subscription.resumed",
  90. "customer.subscription.trial_will_end",
  91. "customer.subscription.updated",
  92. ],
  93. })
  94. const zenLiteProduct = new stripe.Product("ZenLite", {
  95. name: "OpenCode Go",
  96. })
  97. const zenLiteCouponFirstMonth50 = new stripe.Coupon("ZenLiteCouponFirstMonth50", {
  98. name: "First month 50% off",
  99. percentOff: 50,
  100. appliesToProducts: [zenLiteProduct.id],
  101. duration: "once",
  102. })
  103. const zenLiteCouponFirstMonth100 = new stripe.Coupon("ZenLiteCouponFirstMonth100", {
  104. name: "First month 100% off",
  105. percentOff: 100,
  106. appliesToProducts: [zenLiteProduct.id],
  107. duration: "once",
  108. })
  109. const zenLiteCouponThreeMonths100 = new stripe.Coupon("ZenLiteCoupon3Months100", {
  110. name: "3 months 100% off",
  111. percentOff: 100,
  112. appliesToProducts: [zenLiteProduct.id],
  113. duration: "repeating",
  114. durationInMonths: 3,
  115. })
  116. const zenLiteCouponSixMonths100 = new stripe.Coupon("ZenLiteCoupon6Months100", {
  117. name: "6 months 100% off",
  118. percentOff: 100,
  119. appliesToProducts: [zenLiteProduct.id],
  120. duration: "repeating",
  121. durationInMonths: 6,
  122. })
  123. const zenLiteCouponTwelveMonths100 = new stripe.Coupon("ZenLiteCoupon12Months100", {
  124. name: "12 months 100% off",
  125. percentOff: 100,
  126. appliesToProducts: [zenLiteProduct.id],
  127. duration: "repeating",
  128. durationInMonths: 12,
  129. })
  130. const zenLitePrice = new stripe.Price("ZenLitePrice", {
  131. product: zenLiteProduct.id,
  132. currency: "usd",
  133. recurring: {
  134. interval: "month",
  135. intervalCount: 1,
  136. },
  137. unitAmount: 1000,
  138. })
  139. const ZEN_LITE_PRICE = new sst.Linkable("ZEN_LITE_PRICE", {
  140. properties: {
  141. product: zenLiteProduct.id,
  142. price: zenLitePrice.id,
  143. priceInr: 92900,
  144. firstMonth50Coupon: zenLiteCouponFirstMonth50.id,
  145. firstMonth100Coupon: zenLiteCouponFirstMonth100.id,
  146. threeMonths100Coupon: zenLiteCouponThreeMonths100.id,
  147. sixMonths100Coupon: zenLiteCouponSixMonths100.id,
  148. twelveMonths100Coupon: zenLiteCouponTwelveMonths100.id,
  149. },
  150. })
  151. const zenBlackProduct = new stripe.Product("ZenBlack", {
  152. name: "OpenCode Black",
  153. })
  154. const zenBlackPriceProps = {
  155. product: zenBlackProduct.id,
  156. currency: "usd",
  157. recurring: {
  158. interval: "month",
  159. intervalCount: 1,
  160. },
  161. }
  162. const zenBlackPrice200 = new stripe.Price("ZenBlackPrice", { ...zenBlackPriceProps, unitAmount: 20000 })
  163. const zenBlackPrice100 = new stripe.Price("ZenBlack100Price", { ...zenBlackPriceProps, unitAmount: 10000 })
  164. const zenBlackPrice20 = new stripe.Price("ZenBlack20Price", { ...zenBlackPriceProps, unitAmount: 2000 })
  165. const ZEN_BLACK_PRICE = new sst.Linkable("ZEN_BLACK_PRICE", {
  166. properties: {
  167. product: zenBlackProduct.id,
  168. plan200: zenBlackPrice200.id,
  169. plan100: zenBlackPrice100.id,
  170. plan20: zenBlackPrice20.id,
  171. },
  172. })
  173. const ZEN_MODELS = [
  174. new sst.Secret("ZEN_MODELS1"),
  175. new sst.Secret("ZEN_MODELS2"),
  176. new sst.Secret("ZEN_MODELS3"),
  177. new sst.Secret("ZEN_MODELS4"),
  178. new sst.Secret("ZEN_MODELS5"),
  179. new sst.Secret("ZEN_MODELS6"),
  180. new sst.Secret("ZEN_MODELS7"),
  181. new sst.Secret("ZEN_MODELS8"),
  182. new sst.Secret("ZEN_MODELS9"),
  183. new sst.Secret("ZEN_MODELS10"),
  184. new sst.Secret("ZEN_MODELS11"),
  185. new sst.Secret("ZEN_MODELS12"),
  186. new sst.Secret("ZEN_MODELS13"),
  187. new sst.Secret("ZEN_MODELS14"),
  188. new sst.Secret("ZEN_MODELS15"),
  189. new sst.Secret("ZEN_MODELS16"),
  190. new sst.Secret("ZEN_MODELS17"),
  191. new sst.Secret("ZEN_MODELS18"),
  192. new sst.Secret("ZEN_MODELS19"),
  193. new sst.Secret("ZEN_MODELS20"),
  194. new sst.Secret("ZEN_MODELS21"),
  195. new sst.Secret("ZEN_MODELS22"),
  196. new sst.Secret("ZEN_MODELS23"),
  197. new sst.Secret("ZEN_MODELS24"),
  198. new sst.Secret("ZEN_MODELS25"),
  199. new sst.Secret("ZEN_MODELS26"),
  200. new sst.Secret("ZEN_MODELS27"),
  201. new sst.Secret("ZEN_MODELS28"),
  202. new sst.Secret("ZEN_MODELS29"),
  203. new sst.Secret("ZEN_MODELS30"),
  204. ]
  205. const STRIPE_SECRET_KEY = new sst.Secret("STRIPE_SECRET_KEY")
  206. const STRIPE_PUBLISHABLE_KEY = new sst.Secret("STRIPE_PUBLISHABLE_KEY")
  207. const AUTH_API_URL = new sst.Linkable("AUTH_API_URL", {
  208. properties: { value: auth.url.apply((url) => url!) },
  209. })
  210. const STRIPE_WEBHOOK_SECRET = new sst.Linkable("STRIPE_WEBHOOK_SECRET", {
  211. properties: { value: stripeWebhook.secret },
  212. })
  213. const gatewayKv = new sst.cloudflare.Kv("GatewayKv")
  214. ////////////////
  215. // CONSOLE
  216. ////////////////
  217. const bucket = new sst.cloudflare.Bucket("ZenData")
  218. const bucketNew = new sst.cloudflare.Bucket("ZenDataNew")
  219. const DISCORD_INCIDENT_WEBHOOK_URL = new sst.Secret("DISCORD_INCIDENT_WEBHOOK_URL")
  220. const AWS_SES_ACCESS_KEY_ID = new sst.Secret("AWS_SES_ACCESS_KEY_ID")
  221. const AWS_SES_SECRET_ACCESS_KEY = new sst.Secret("AWS_SES_SECRET_ACCESS_KEY")
  222. const SALESFORCE_CLIENT_ID = new sst.Secret("SALESFORCE_CLIENT_ID")
  223. const SALESFORCE_CLIENT_SECRET = new sst.Secret("SALESFORCE_CLIENT_SECRET")
  224. const SALESFORCE_INSTANCE_URL = new sst.Secret("SALESFORCE_INSTANCE_URL")
  225. const logProcessor = new sst.cloudflare.Worker("LogProcessor", {
  226. handler: "packages/console/function/src/log-processor.ts",
  227. link: [new sst.Secret("HONEYCOMB_API_KEY")],
  228. })
  229. new sst.cloudflare.x.SolidStart("Console", {
  230. domain,
  231. path: "packages/console/app",
  232. link: [
  233. bucket,
  234. bucketNew,
  235. database,
  236. AUTH_API_URL,
  237. STRIPE_WEBHOOK_SECRET,
  238. DISCORD_INCIDENT_WEBHOOK_URL,
  239. SECRET.HoneycombWebhookSecret,
  240. STRIPE_SECRET_KEY,
  241. EMAILOCTOPUS_API_KEY,
  242. AWS_SES_ACCESS_KEY_ID,
  243. AWS_SES_SECRET_ACCESS_KEY,
  244. SALESFORCE_CLIENT_ID,
  245. SALESFORCE_CLIENT_SECRET,
  246. SALESFORCE_INSTANCE_URL,
  247. ZEN_BLACK_PRICE,
  248. ZEN_LITE_PRICE,
  249. new sst.Secret("ZEN_LIMITS"),
  250. new sst.Secret("ZEN_SESSION_SECRET"),
  251. ...ZEN_MODELS,
  252. ...($dev
  253. ? [
  254. new sst.Secret("CLOUDFLARE_DEFAULT_ACCOUNT_ID", process.env.CLOUDFLARE_DEFAULT_ACCOUNT_ID!),
  255. new sst.Secret("CLOUDFLARE_API_TOKEN", process.env.CLOUDFLARE_API_TOKEN!),
  256. ]
  257. : []),
  258. gatewayKv,
  259. ],
  260. environment: {
  261. //VITE_DOCS_URL: web.url.apply((url) => url!),
  262. //VITE_API_URL: gateway.url.apply((url) => url!),
  263. VITE_AUTH_URL: auth.url.apply((url) => url!),
  264. VITE_STRIPE_PUBLISHABLE_KEY: STRIPE_PUBLISHABLE_KEY.value,
  265. },
  266. transform: {
  267. server: {
  268. placement: { region: "aws:us-east-1" },
  269. transform: {
  270. worker: {
  271. tailConsumers: [{ service: logProcessor.nodes.worker.scriptName }],
  272. },
  273. },
  274. },
  275. },
  276. })