From 0695a01f175e94f0f3c0fa9155318673fe383766 Mon Sep 17 00:00:00 2001 From: JBB0807 <104856796+JBB0807@users.noreply.github.com> Date: Mon, 21 Apr 2025 17:14:11 -0700 Subject: [PATCH] Code changes to make it work with Fly.io --- auth-service/.env | 15 ++++-- auth-service/Dockerfile | 2 +- auth-service/fly.toml | 36 +++++++++----- auth-service/passport.js | 6 +-- auth-service/routes/auth.js | 4 +- auth-service/server.js | 4 +- user-db-service/.env | 4 +- user-db-service/Dockerfile | 20 ++++++++ user-db-service/fly.toml | 94 ++++++++++++++----------------------- 9 files changed, 100 insertions(+), 85 deletions(-) create mode 100644 user-db-service/Dockerfile diff --git a/auth-service/.env b/auth-service/.env index 9e9d028..f65f2f8 100644 --- a/auth-service/.env +++ b/auth-service/.env @@ -1,5 +1,10 @@ -CLIENT_ID = "485880105639-1in8tvb6ondnn198rasuj2d8ank06ntp.apps.googleusercontent.com" -CLIENT_SECRET = "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" -CLIENT_URL = "http://localhost:5173/" -DB_USER_SERVICE_URL = "http://localhost:3000/" -SESSION_KEY = "f3f4d8e6b17a4b3abdc8e9a2c0457aaf91c0d5f6e3b7a9c8df624bd71ea35f42" \ No newline at end of file +GOOGLE_CLIENT_ID = "485880105639-1in8tvb6ondnn198rasuj2d8ank06ntp.apps.googleusercontent.com" +GOOGLE_CLIENT_SECRET = "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" +GOOGLE_CALLBACK_URL = "https://byte-camp-auth-service.fly.dev/auth/google/callback" +LOGIN_REDIRECT_URL = "https://bytecamp-web.fly.dev/" +#DB_USER_SERVICE_URL = "http://localhost:3000/" +DB_USER_SERVICE_URL = "http://db-user-service.internal:3000/" +AUTH_SESSION_KEY = "f3f4d8e6b17a4b3abdc8e9a2c0457aaf91c0d5f6e3b7a9c8df624bd71ea35f42" + +fly secrets set GOOGLE_CALLBACK_URL=https://byte-camp-auth-service.fly.dev/auth/google/callback +#fly secrets set GOOGLE_CLIENT_ID=485880105639-1in8tvb6ondnn198rasuj2d8ank06ntp.apps.googleusercontent.com GOOGLE_CLIENT_SECRET=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 LOGIN_REDIRECT_URL=https://bytecamp-web.fly.dev/ DB_USER_SERVICE_URL=https://db-user-service.fly.dev:3000/ AUTH_SESSION_KEY=f3f4d8e6b17a4b3abdc8e9a2c0457aaf91c0d5f6e3b7a9c8df624bd71ea35f42 \ No newline at end of file diff --git a/auth-service/Dockerfile b/auth-service/Dockerfile index d5182d9..a172788 100644 --- a/auth-service/Dockerfile +++ b/auth-service/Dockerfile @@ -11,7 +11,7 @@ COPY . . RUN npm install # Install dependencies using npm ci for deterministic builds -RUN --mount=type=cache,target=/root/.npm npm ci --production +# RUN --mount=type=cache,target=/root/.npm npm ci --production # Copy the application source code COPY --link . . diff --git a/auth-service/fly.toml b/auth-service/fly.toml index a1099ec..34f4cbc 100644 --- a/auth-service/fly.toml +++ b/auth-service/fly.toml @@ -1,28 +1,42 @@ -# fly.toml app configuration file generated for snakebyte on 2025-01-04T01:06:05-08:00 +# fly.toml app configuration file generated for byte-camp-auth-service on 2025-04-21T14:38:25-07:00 # # See https://fly.io/docs/reference/configuration/ for information about how to use this file. # -app = "auth-service" -primary_region = "sea" +app = 'byte-camp-auth-service' +primary_region = 'sea' + +[build] [env] - PORT = "8080" + PORT = '8080' + +[http_service] + internal_port = 8080 + force_https = true + auto_stop_machines = 'stop' + auto_start_machines = true + min_machines_running = 0 + processes = ['app'] [[services]] + protocol = 'tcp' internal_port = 8080 - protocol = "tcp" [[services.ports]] - handlers = ["http"] port = 80 + handlers = ['http'] [[services.ports]] - handlers = ["tls", "http"] port = 443 + handlers = ['tls', 'http'] [[services.tcp_checks]] - interval = "10s" - timeout = "2s" - grace_period = "5s" - restart_limit = 0 \ No newline at end of file + interval = '10s' + timeout = '2s' + grace_period = '5s' + +[[vm]] + memory = '1gb' + cpu_kind = 'shared' + cpus = 1 diff --git a/auth-service/passport.js b/auth-service/passport.js index 7f9a2e6..e65e7c8 100644 --- a/auth-service/passport.js +++ b/auth-service/passport.js @@ -6,9 +6,9 @@ const passport = require("passport"); passport.use( new GoogleStrategy( { - clientID: process.env.CLIENT_ID, - clientSecret: process.env.CLIENT_SECRET, - callbackURL: "/auth/google/callback", + clientID: process.env.GOOGLE_CLIENT_ID, + clientSecret: process.env.GOOGLE_CLIENT_SECRET, + callbackURL: process.env.GOOGLE_CALLBACK_URL, scope: ["profile", "email"], }, function (accessToken, refreshToken, profile, callback) { diff --git a/auth-service/routes/auth.js b/auth-service/routes/auth.js index 3877d59..b735b83 100644 --- a/auth-service/routes/auth.js +++ b/auth-service/routes/auth.js @@ -19,7 +19,7 @@ router.get("/login", (req, res) => { }) .then(response => { console.log("User registration response:", response.data); - res.redirect(process.env.CLIENT_URL); + res.redirect(process.env.LOGIN_REDIRECT_URL); }) .catch(error => { console.error("Error registering user:", error.message); @@ -42,7 +42,7 @@ router.get("/google", passport.authenticate("google", ["profile", "email"])); router.get("/logout", (req, res) => { req.logOut(); - res.redirect(process.env.CLIENT_URL); + res.redirect(process.env.LOGIN_REDIRECT_URL); }); module.exports = router; \ No newline at end of file diff --git a/auth-service/server.js b/auth-service/server.js index 5e42bd8..ca47aef 100644 --- a/auth-service/server.js +++ b/auth-service/server.js @@ -11,7 +11,7 @@ const app = express(); app.use( session({ - secret: process.env.SESSION_KEY, + secret: process.env.AUTH_SESSION_KEY, resave: false, saveUninitialized: false, cookie: { @@ -25,7 +25,7 @@ app.use(passport.session()); app.use( cors({ - origin: "http://localhost:5173", + origin: "https://bytecamp-web.fly.dev", methods: "GET", credentials: true, }) diff --git a/user-db-service/.env b/user-db-service/.env index 399d6c3..3aaa685 100644 --- a/user-db-service/.env +++ b/user-db-service/.env @@ -5,8 +5,8 @@ # See the documentation for all the connection string options: https://pris.ly/d/connection-strings #use this when testing local, remmber to run the proxy command -DATABASE_URL="postgresql://postgres:wly9H8gjjmxYfg1@localhost:15432/postgres?schema=public" +# DATABASE_URL="postgresql://postgres:wly9H8gjjmxYfg1@localhost:15432/postgres?schema=public" -# DATABASE_URL="postgres://postgres:w2eSd47GJEdqvMf@snakebyte.internal:5432" +DATABASE_URL="postgres://postgres:wly9H8gjjmxYfg1@bytecamp-db.flycast:5432?sslmode=disable" # DATABASE_URL=postgres://snakebyte:zVB7lgOiKr89dq6@localhost:5432/snakebyte?sslmode=disable NODE_PORT=3000 diff --git a/user-db-service/Dockerfile b/user-db-service/Dockerfile new file mode 100644 index 0000000..d443c94 --- /dev/null +++ b/user-db-service/Dockerfile @@ -0,0 +1,20 @@ +# syntax=docker/dockerfile:1 + +# Use the official Node.js image as the base image +ARG NODE_VERSION=22.13.1 +FROM node:${NODE_VERSION}-slim AS base + +# Set the working directory +WORKDIR /app + +COPY . . +RUN apt-get update -y && apt-get install -y openssl && npm install && npx prisma generate + +# Copy the application source code +COPY --link . . + +# Expose the application port +EXPOSE 3000 + +# Define the command to run the application +CMD ["node", "app.js"] \ No newline at end of file diff --git a/user-db-service/fly.toml b/user-db-service/fly.toml index 28cfd0f..eeef656 100644 --- a/user-db-service/fly.toml +++ b/user-db-service/fly.toml @@ -1,69 +1,45 @@ -# fly.toml app configuration file generated for snakebyte on 2025-01-04T01:06:05-08:00 -# -# See https://fly.io/docs/reference/configuration/ for information about how to use this file. -# +# fly.toml file for a Node.js service that connects to an external Postgres DB -app = 'db-user-service' -primary_region = 'sea' +app = "db-user-service" +primary_region = "sea" -[env] - PRIMARY_REGION = 'sea' +[build] +# Only needed if you're using a Dockerfile — can be empty if using buildpacks or Node preset -[[mounts]] - source = 'pg_data' - destination = '/data' +#[env] + #NODE_ENV = "production" + #DATABASE_URL = "postgresql://user:password@hostname:port/dbname" + # you can also leave DATABASE_URL unset here and use secrets instead + +# Removed the [http_service] section to disable public HTTP/HTTPS access +# [http_service] +# internal_port = 3000 +# force_https = true +# auto_stop_machines = true +# auto_start_machines = true +# min_machines_running = 0 +# processes = ["app"] [[services]] - protocol = 'tcp' - internal_port = 5432 - auto_start_machines = false + protocol = "tcp" + internal_port = 3000 + internal_only = true # Makes this service only accessible internally - [[services.ports]] - port = 5432 - handlers = ['pg_tls'] + # Removed public port exposure + # [[services.ports]] + # port = 80 + # handlers = ["http"] + + # [[services.ports]] + # port = 443 + # handlers = ["tls", "http"] [services.concurrency] - type = 'connections' + type = "requests" hard_limit = 1000 - soft_limit = 1000 + soft_limit = 500 -[[services]] - protocol = 'tcp' - internal_port = 5433 - auto_start_machines = false - - [[services.ports]] - port = 5433 - handlers = ['pg_tls'] - - [services.concurrency] - type = 'connections' - hard_limit = 1000 - soft_limit = 1000 - -[checks] - [checks.pg] - port = 5500 - type = 'http' - interval = '15s' - timeout = '10s' - path = '/flycheck/pg' - - [checks.role] - port = 5500 - type = 'http' - interval = '15s' - timeout = '10s' - path = '/flycheck/role' - - [checks.vm] - port = 5500 - type = 'http' - interval = '15s' - timeout = '10s' - path = '/flycheck/vm' - -[[metrics]] - port = 9187 - path = '/metrics' - https = false +[[vm]] + memory = "512mb" + cpu_kind = "shared" + cpus = 1