From bb3f1cf78924b3f7ec79d082eb41246bd24f3a2c Mon Sep 17 00:00:00 2001 From: ashwin-godura Date: Wed, 3 May 2023 18:08:12 -0400 Subject: [PATCH 1/3] created clicks property for user endpoints --- backend/.dockerignore | 5 +++++ backend/Dockerfile | 44 ++++++++++++++++++++++++++++++++++++++ backend/fly.toml | 44 ++++++++++++++++++++++++++++++++++++++ backend/src/models/user.ts | 5 +++++ backend/src/routes/user.ts | 13 +++++++++++ 5 files changed, 111 insertions(+) create mode 100644 backend/.dockerignore create mode 100644 backend/Dockerfile create mode 100644 backend/fly.toml diff --git a/backend/.dockerignore b/backend/.dockerignore new file mode 100644 index 0000000..47719be --- /dev/null +++ b/backend/.dockerignore @@ -0,0 +1,5 @@ +fly.toml +Dockerfile +.dockerignore +node_modules +.git diff --git a/backend/Dockerfile b/backend/Dockerfile new file mode 100644 index 0000000..ea2b450 --- /dev/null +++ b/backend/Dockerfile @@ -0,0 +1,44 @@ +# syntax = docker/dockerfile:1 + +# Adjust NODE_VERSION as desired +ARG NODE_VERSION=18.13.0 +FROM node:${NODE_VERSION}-slim as base + +LABEL fly_launch_runtime="NodeJS" + +# NodeJS app lives here +WORKDIR /app + +# Set production environment +ENV NODE_ENV=production + + +# Throw-away build stage to reduce size of final image +FROM base as build + +# Install packages needed to build node modules +RUN apt-get update -qq && \ + apt-get install -y python-is-python3 pkg-config build-essential + +# Install node modules +COPY --link package.json package-lock.json . +RUN npm install --production=false + +# Copy application code +COPY --link . . + +# Build application +RUN npm run build + +# Remove development dependencies +RUN npm prune --production + + +# Final stage for app image +FROM base + +# Copy built application +COPY --from=build /app /app + +# Start the server by default, this can be overwritten at runtime +CMD [ "npm", "run", "start" ] diff --git a/backend/fly.toml b/backend/fly.toml new file mode 100644 index 0000000..509ca10 --- /dev/null +++ b/backend/fly.toml @@ -0,0 +1,44 @@ +# fly.toml app configuration file generated for doneserver on 2023-05-03T16:40:48-04:00 +# +# See https://fly.io/docs/reference/configuration/ for information about how to use this file. +# + +app = "doneserver" +kill_signal = "SIGINT" +kill_timeout = 5 +primary_region = "iad" +processes = [] + +[build] + +[env] + PORT = "8080" + +[experimental] + auto_rollback = true + +[[services]] + http_checks = [] + internal_port = 8080 + processes = ["app"] + protocol = "tcp" + script_checks = [] + [services.concurrency] + hard_limit = 25 + soft_limit = 20 + type = "connections" + + [[services.ports]] + force_https = true + handlers = ["http"] + port = 80 + + [[services.ports]] + handlers = ["tls", "http"] + port = 443 + + [[services.tcp_checks]] + grace_period = "1s" + interval = "15s" + restart_limit = 0 + timeout = "2s" diff --git a/backend/src/models/user.ts b/backend/src/models/user.ts index 43bed2a..4fddec3 100644 --- a/backend/src/models/user.ts +++ b/backend/src/models/user.ts @@ -6,6 +6,7 @@ export interface IUser { last_name: string username: string password: string + history_clicks: number } const UserSchema = new Schema({ @@ -25,6 +26,10 @@ const UserSchema = new Schema({ type: String, required: true }, + history_clicks: { + type: Number, + required: true + } }) export const User = mongoose.model('users', UserSchema) \ No newline at end of file diff --git a/backend/src/routes/user.ts b/backend/src/routes/user.ts index e6558b8..8a58ecf 100644 --- a/backend/src/routes/user.ts +++ b/backend/src/routes/user.ts @@ -25,6 +25,19 @@ router.get("/:username", (req, res) => { }); }); +// UPDATE a Task +router.put("/:username", async (req, res) => { + const user = req.body; + console.log("update task", user, req.params.username) + User.findOneAndUpdate({ username: user.username}, { $set: user }, (err: any, result: any) => { + if (err) { + res.json(err); + } else { + res.json(result); + } + }); +}); + // CREATE a new User router.post("/", async (req, res) => { console.log('post request') From 3ce4d5b987a8c1d9c5c0a30faf6b40a9fabb1c24 Mon Sep 17 00:00:00 2001 From: ashwin-godura Date: Wed, 3 May 2023 18:13:48 -0400 Subject: [PATCH 2/3] changed url for fly.io --- backend/fly.toml | 2 +- frontend/src/backend-adapter.ts | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/backend/fly.toml b/backend/fly.toml index 509ca10..61645af 100644 --- a/backend/fly.toml +++ b/backend/fly.toml @@ -1,4 +1,4 @@ -# fly.toml app configuration file generated for doneserver on 2023-05-03T16:40:48-04:00 +# fly.toml app configuration file generated for doneserver on 2023-05-03T18:12:02-04:00 # # See https://fly.io/docs/reference/configuration/ for information about how to use this file. # diff --git a/frontend/src/backend-adapter.ts b/frontend/src/backend-adapter.ts index 65ead96..523e7f1 100644 --- a/frontend/src/backend-adapter.ts +++ b/frontend/src/backend-adapter.ts @@ -17,7 +17,7 @@ import mongoose from "mongoose"; let instance = axios.create({ - baseURL: "http://localhost:8080/api/", + baseURL: "https://doneserver.fly.dev/api", }); @@ -83,6 +83,11 @@ export async function addUser(user) { } } +export async function updateUser(user) { + const id = user.username + await instance.put("tasks/" + id, user).then((response) => response.data) +} + export async function authenticateUser(username: string, password: string) { const response = await instance.get("users/" + username) if (response.data.length == 0) { From 4e800fd53924faa78788e723dc0230c82e76881a Mon Sep 17 00:00:00 2001 From: ashwin-godura Date: Wed, 3 May 2023 19:17:34 -0400 Subject: [PATCH 3/3] fixed package.json --- backend/package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/package.json b/backend/package.json index aaef71c..3e69a0a 100644 --- a/backend/package.json +++ b/backend/package.json @@ -58,13 +58,13 @@ } }, "scripts": { - "build": "react-scripts build", + "build": "", "eject": "react-scripts eject", "lint": "eslint \"**/*.{ts,tsx}\"", "test": "jest --verbose", "start-backend": "ts-node --experimental-specifier-resolution=node ./server.ts", "start-backend-alt": "node --experimental-specifier-resolution=node --loader ts-node/esm ./server.ts", - "start": "node server.js", + "start": "node --experimental-specifier-resolution=node --loader ts-node/esm ./server.ts", "client": "npm start --prefix ../frontend", "dev": "concurrently \"npm run start-backend\" \"npm run client\"" },