From 39c4e3cf998b698c5eb22c05f1a4620931a4cb00 Mon Sep 17 00:00:00 2001 From: mohammadhnz0 Date: Thu, 16 Feb 2023 22:55:47 +0330 Subject: [PATCH 1/8] feat: update docker compose files --- .gitignore | 2 + auth-service/.idea/.gitignore | 8 --- auth-service/.idea/auth-service.iml | 9 ---- auth-service/.idea/dataSources.xml | 13 ----- auth-service/.idea/modules.xml | 8 --- auth-service/.idea/vcs.xml | 6 --- backend/auth-service/Dockerfile | 12 +++++ .../auth-service}/config/cache.go | 2 +- .../auth-service}/config/db.go | 0 .../auth-service}/config/environment_vars.go | 0 .../controller/user_controllers.go | 0 .../auth-service}/docker-compose.yml | 0 {auth-service => backend/auth-service}/go.mod | 0 {auth-service => backend/auth-service}/go.sum | 0 .../auth-service}/grpc_server/auth.proto | 0 .../auth-service}/grpc_server/auth/auth.go | 3 ++ .../auth-service}/grpc_server/auth/auth.pb.go | 0 .../auth-service}/grpc_server/client.go | 23 +------- .../auth-service}/grpc_server/server.go | 4 +- .../auth-service}/main.go | 0 .../auth-service}/middleware/required_auth.go | 0 .../models/unauthorized_token.go | 0 .../auth-service}/models/user.go | 0 .../repository/user_repository.go | 0 .../auth-service}/routes/user.go | 0 backend/auth-service/run.sh | 6 +++ .../auth-service}/sql/auth.sql | 0 .../auth-service}/utils/jwt-utils.go | 0 backend/docker-compose.yml | 52 ++++++++++++++++++- backend/reverse-proxy/Dockerfile | 2 + backend/reverse-proxy/nginx.conf | 25 +++++++++ backend/server/Dockerfile | 1 - backend/server/authHandler.js | 1 + backend/server/authRoutes.js | 5 +- backend/server/grpc_clients/index.js | 2 +- backend/server/main.js | 1 - backend/server/ticketRoutes.js | 2 + backend/ticketService/Dockerfile | 1 - backend/ticketService/resolvers/ticket.js | 5 ++ 39 files changed, 118 insertions(+), 75 deletions(-) delete mode 100644 auth-service/.idea/.gitignore delete mode 100644 auth-service/.idea/auth-service.iml delete mode 100644 auth-service/.idea/dataSources.xml delete mode 100644 auth-service/.idea/modules.xml delete mode 100644 auth-service/.idea/vcs.xml create mode 100644 backend/auth-service/Dockerfile rename {auth-service => backend/auth-service}/config/cache.go (85%) rename {auth-service => backend/auth-service}/config/db.go (100%) rename {auth-service => backend/auth-service}/config/environment_vars.go (100%) rename {auth-service => backend/auth-service}/controller/user_controllers.go (100%) rename {auth-service => backend/auth-service}/docker-compose.yml (100%) rename {auth-service => backend/auth-service}/go.mod (100%) rename {auth-service => backend/auth-service}/go.sum (100%) rename {auth-service => backend/auth-service}/grpc_server/auth.proto (100%) rename {auth-service => backend/auth-service}/grpc_server/auth/auth.go (98%) rename {auth-service => backend/auth-service}/grpc_server/auth/auth.pb.go (100%) rename {auth-service => backend/auth-service}/grpc_server/client.go (56%) rename {auth-service => backend/auth-service}/grpc_server/server.go (88%) rename {auth-service => backend/auth-service}/main.go (100%) rename {auth-service => backend/auth-service}/middleware/required_auth.go (100%) rename {auth-service => backend/auth-service}/models/unauthorized_token.go (100%) rename {auth-service => backend/auth-service}/models/user.go (100%) rename {auth-service => backend/auth-service}/repository/user_repository.go (100%) rename {auth-service => backend/auth-service}/routes/user.go (100%) create mode 100644 backend/auth-service/run.sh rename {auth-service => backend/auth-service}/sql/auth.sql (100%) rename {auth-service => backend/auth-service}/utils/jwt-utils.go (100%) create mode 100644 backend/reverse-proxy/Dockerfile create mode 100644 backend/reverse-proxy/nginx.conf diff --git a/.gitignore b/.gitignore index ea1fe59..5efb17d 100755 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,8 @@ *.env *.idea* *.vscode* +*auth-data/* +*ticket-data/* # Test binary, built with `go test -c` *.test diff --git a/auth-service/.idea/.gitignore b/auth-service/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/auth-service/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/auth-service/.idea/auth-service.iml b/auth-service/.idea/auth-service.iml deleted file mode 100644 index 5e764c4..0000000 --- a/auth-service/.idea/auth-service.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/auth-service/.idea/dataSources.xml b/auth-service/.idea/dataSources.xml deleted file mode 100644 index ed69b81..0000000 --- a/auth-service/.idea/dataSources.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - postgresql - true - true - org.postgresql.Driver - postgres://postgres:postgres@localhost:8081/postgres - $ProjectFileDir$ - - - \ No newline at end of file diff --git a/auth-service/.idea/modules.xml b/auth-service/.idea/modules.xml deleted file mode 100644 index f48d788..0000000 --- a/auth-service/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/auth-service/.idea/vcs.xml b/auth-service/.idea/vcs.xml deleted file mode 100644 index 6c0b863..0000000 --- a/auth-service/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/backend/auth-service/Dockerfile b/backend/auth-service/Dockerfile new file mode 100644 index 0000000..ff7b75b --- /dev/null +++ b/backend/auth-service/Dockerfile @@ -0,0 +1,12 @@ +# syntax=docker/dockerfile:1 + +FROM golang:1.19 + +WORKDIR / + +COPY ./ ./ +RUN go mod download +RUN chmod 0777 /run.sh + + +ENTRYPOINT ["/run.sh"] \ No newline at end of file diff --git a/auth-service/config/cache.go b/backend/auth-service/config/cache.go similarity index 85% rename from auth-service/config/cache.go rename to backend/auth-service/config/cache.go index 22814e2..aa25bd3 100644 --- a/auth-service/config/cache.go +++ b/backend/auth-service/config/cache.go @@ -6,6 +6,6 @@ var RedisClient *redis.Client func ConnectRedis() { RedisClient = redis.NewClient(&redis.Options{ - Addr: "localhost:6379", + Addr: "authRedis:6380", }) } diff --git a/auth-service/config/db.go b/backend/auth-service/config/db.go similarity index 100% rename from auth-service/config/db.go rename to backend/auth-service/config/db.go diff --git a/auth-service/config/environment_vars.go b/backend/auth-service/config/environment_vars.go similarity index 100% rename from auth-service/config/environment_vars.go rename to backend/auth-service/config/environment_vars.go diff --git a/auth-service/controller/user_controllers.go b/backend/auth-service/controller/user_controllers.go similarity index 100% rename from auth-service/controller/user_controllers.go rename to backend/auth-service/controller/user_controllers.go diff --git a/auth-service/docker-compose.yml b/backend/auth-service/docker-compose.yml similarity index 100% rename from auth-service/docker-compose.yml rename to backend/auth-service/docker-compose.yml diff --git a/auth-service/go.mod b/backend/auth-service/go.mod similarity index 100% rename from auth-service/go.mod rename to backend/auth-service/go.mod diff --git a/auth-service/go.sum b/backend/auth-service/go.sum similarity index 100% rename from auth-service/go.sum rename to backend/auth-service/go.sum diff --git a/auth-service/grpc_server/auth.proto b/backend/auth-service/grpc_server/auth.proto similarity index 100% rename from auth-service/grpc_server/auth.proto rename to backend/auth-service/grpc_server/auth.proto diff --git a/auth-service/grpc_server/auth/auth.go b/backend/auth-service/grpc_server/auth/auth.go similarity index 98% rename from auth-service/grpc_server/auth/auth.go rename to backend/auth-service/grpc_server/auth/auth.go index a89bc98..abbf69a 100644 --- a/auth-service/grpc_server/auth/auth.go +++ b/backend/auth-service/grpc_server/auth/auth.go @@ -105,6 +105,9 @@ func checkIfTokenHasBeenExpired(tokenData *TokenData) (error, *SignOutResponse, Status: SignOutResponse_OK_201, }, nil, true + } + if err != nil { + checkTokenIsNotUnAuthorized } return err, nil, nil, false } diff --git a/auth-service/grpc_server/auth/auth.pb.go b/backend/auth-service/grpc_server/auth/auth.pb.go similarity index 100% rename from auth-service/grpc_server/auth/auth.pb.go rename to backend/auth-service/grpc_server/auth/auth.pb.go diff --git a/auth-service/grpc_server/client.go b/backend/auth-service/grpc_server/client.go similarity index 56% rename from auth-service/grpc_server/client.go rename to backend/auth-service/grpc_server/client.go index 277f00e..bc29ace 100644 --- a/auth-service/grpc_server/client.go +++ b/backend/auth-service/grpc_server/client.go @@ -4,6 +4,7 @@ import ( authorization "awesomeProject/grpc_server/auth" "context" "google.golang.org/grpc" + "fmt" "log" ) @@ -30,25 +31,5 @@ func main() { if err != nil { log.Printf("Could not sign up: %s", err) } - - signInData := authorization.SignInData{ - PhoneNumber: "0933041sdf2310360", - Email: "dfsdfsdfsdfsdf@gmail.com", - Password: "Ali is near 999", - } - - res, err := c.SignIn(context.Background(), &signInData) - if err != nil { - log.Printf("Could not sign up: %s", err) - } - log.Println(res) - userInfoData := authorization.UserInfoData{ - AccessToken: string(res.AccessToken), - RefreshToken: string(res.RefreshToken), - } - res1, err1 := c.UserInfo(context.Background(), &userInfoData) - if err1 != nil { - log.Printf("Could not sign up: %s", err) - } - log.Println(res1) + fmt.Println("Fuck life") } diff --git a/auth-service/grpc_server/server.go b/backend/auth-service/grpc_server/server.go similarity index 88% rename from auth-service/grpc_server/server.go rename to backend/auth-service/grpc_server/server.go index b706996..9986875 100644 --- a/auth-service/grpc_server/server.go +++ b/backend/auth-service/grpc_server/server.go @@ -16,11 +16,11 @@ func init() { } func main() { - lis, err := net.Listen("tcp", "localhost:50052") + lis, err := net.Listen("tcp", "0.0.0.0:50052") if err != nil { log.Fatal("Error running grpc_server!") } - + fmt.Println("HELLOO BABY") s := authorization.Server{} grpcServer := grpc.NewServer() diff --git a/auth-service/main.go b/backend/auth-service/main.go similarity index 100% rename from auth-service/main.go rename to backend/auth-service/main.go diff --git a/auth-service/middleware/required_auth.go b/backend/auth-service/middleware/required_auth.go similarity index 100% rename from auth-service/middleware/required_auth.go rename to backend/auth-service/middleware/required_auth.go diff --git a/auth-service/models/unauthorized_token.go b/backend/auth-service/models/unauthorized_token.go similarity index 100% rename from auth-service/models/unauthorized_token.go rename to backend/auth-service/models/unauthorized_token.go diff --git a/auth-service/models/user.go b/backend/auth-service/models/user.go similarity index 100% rename from auth-service/models/user.go rename to backend/auth-service/models/user.go diff --git a/auth-service/repository/user_repository.go b/backend/auth-service/repository/user_repository.go similarity index 100% rename from auth-service/repository/user_repository.go rename to backend/auth-service/repository/user_repository.go diff --git a/auth-service/routes/user.go b/backend/auth-service/routes/user.go similarity index 100% rename from auth-service/routes/user.go rename to backend/auth-service/routes/user.go diff --git a/backend/auth-service/run.sh b/backend/auth-service/run.sh new file mode 100644 index 0000000..ec59d6c --- /dev/null +++ b/backend/auth-service/run.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +sleep 3 +echo "Running auth service..." + +exec go run ./grpc_server/server.go \ No newline at end of file diff --git a/auth-service/sql/auth.sql b/backend/auth-service/sql/auth.sql similarity index 100% rename from auth-service/sql/auth.sql rename to backend/auth-service/sql/auth.sql diff --git a/auth-service/utils/jwt-utils.go b/backend/auth-service/utils/jwt-utils.go similarity index 100% rename from auth-service/utils/jwt-utils.go rename to backend/auth-service/utils/jwt-utils.go diff --git a/backend/docker-compose.yml b/backend/docker-compose.yml index d2187d1..347ce26 100755 --- a/backend/docker-compose.yml +++ b/backend/docker-compose.yml @@ -6,6 +6,12 @@ services: expose: - "6379" + authRedis: + image: redis:6.2-alpine + command: --port 6380 + expose: + - "6380" + postgres: build: context: ./postgres @@ -18,7 +24,33 @@ services: - POSTGRES_HOST_AUTH_METHOD=trust volumes: - ./postgres/csvs:/var/lib/postgresql/csvs + - ./ticket-data:/var/lib/postgresql/data + authdb: + image: postgres + expose: + - "5432" + environment: + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=postgres + - POSTGRES_DB=postgres + volumes: + - ./auth-service/sql/auth.sql:/docker-entrypoint-initdb.d/create_tables.sql + - ./auth-data:/var/lib/postgresql/data + auth: + build: + context: ./auth-service + dockerfile: Dockerfile + expose: + - "50052" + ports: + - "50052:50052" + depends_on: + - authdb + - authRedis + links: + - authdb + - authRedis bank: build: context: ./bankService @@ -52,7 +84,25 @@ services: - postgres - ticket - bank + - auth links: - postgres - bank - - ticket \ No newline at end of file + - ticket + - auth + reverse-proxy: + build: + context: ./server + dockerfile: Dockerfile + ports: + - "8081:8081" + depends_on: + - postgres + - ticket + - bank + - auth + links: + - postgres + - bank + - ticket + - auth \ No newline at end of file diff --git a/backend/reverse-proxy/Dockerfile b/backend/reverse-proxy/Dockerfile new file mode 100644 index 0000000..57845aa --- /dev/null +++ b/backend/reverse-proxy/Dockerfile @@ -0,0 +1,2 @@ +FROM nginx +COPY nginx.conf /etc/nginx/conf.d/default.conf \ No newline at end of file diff --git a/backend/reverse-proxy/nginx.conf b/backend/reverse-proxy/nginx.conf new file mode 100644 index 0000000..823a6a8 --- /dev/null +++ b/backend/reverse-proxy/nginx.conf @@ -0,0 +1,25 @@ +server { + # this server listens on port 80 + listen 80 default_server; + listen [::]:80 default_server; + + # name this server "nodeserver", but we can call it whatever we like + server_name nodeserver; + + # the location / means that when we visit the root url (localhost:80/), we use this configuration + location / { + # a bunch of boilerplate proxy configuration + proxy_http_version 1.1; + proxy_cache_bypass $http_upgrade; + + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection 'upgrade'; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + + # the real magic is here where we forward requests to the address that the Node.js server is running on + proxy_pass http://localhost:8081; + } +} \ No newline at end of file diff --git a/backend/server/Dockerfile b/backend/server/Dockerfile index e0bde1b..a4c8ec9 100755 --- a/backend/server/Dockerfile +++ b/backend/server/Dockerfile @@ -2,7 +2,6 @@ FROM node:14 WORKDIR / COPY . / -RUN rm -rf node_modules RUN npm install RUN chmod 0777 /run.sh diff --git a/backend/server/authHandler.js b/backend/server/authHandler.js index 20271f6..a07f2ad 100644 --- a/backend/server/authHandler.js +++ b/backend/server/authHandler.js @@ -2,6 +2,7 @@ const {auth_grpc} = require("./grpc_clients"); module.exports = (req, res, next) => { + console.log("Looping in auth handler"); if (!req.cookies.tokens) { return res.status(401).json({ok: false, data: req.cookies}) } diff --git a/backend/server/authRoutes.js b/backend/server/authRoutes.js index 79c405a..db791ec 100644 --- a/backend/server/authRoutes.js +++ b/backend/server/authRoutes.js @@ -6,6 +6,7 @@ const {ticket_grpc, auth_grpc} = require("./grpc_clients"); const authHandler = require("./authHandler"); router.post("/signup/", (req, res) => { + // console.log(auth_grpc.Signup); auth_grpc.Signup({ email: req.body.email, first_name: req.body.first_name, @@ -25,7 +26,7 @@ router.post("/signup/", (req, res) => { }); -router.get("/signin/", (req, res) => { +router.post("/signin/", (req, res) => { auth_grpc.SignIn({ email: req.body.email, phone_number: req.body.phone_number, @@ -42,7 +43,7 @@ router.get("/signin/", (req, res) => { }); -router.get("/signout/", (req, res) => { +router.post("/signout/", (req, res) => { if (!req.cookies.tokens) { return res.status(500).json({ok: false, data: req.cookies}) } diff --git a/backend/server/grpc_clients/index.js b/backend/server/grpc_clients/index.js index 927f691..381391b 100644 --- a/backend/server/grpc_clients/index.js +++ b/backend/server/grpc_clients/index.js @@ -15,7 +15,7 @@ const TicketService = ticketProto.TicketService; const AuthService = authProto.Authentication; const ticket_grpc = new TicketService('ticket:50050', grpc.credentials.createInsecure()); -const auth_grpc = new AuthService('127.0.0.1:50052', grpc.credentials.createInsecure() ); +const auth_grpc = new AuthService('auth:50052', grpc.credentials.createInsecure()); module.exports = { diff --git a/backend/server/main.js b/backend/server/main.js index fc5542a..1e9a24c 100644 --- a/backend/server/main.js +++ b/backend/server/main.js @@ -14,7 +14,6 @@ app.use(cookieParser()); app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); app.use("/auth", authRoutes); -app.use(authHandler); app.use("/", ticketRoutes); app.use(validatorMiddleware); diff --git a/backend/server/ticketRoutes.js b/backend/server/ticketRoutes.js index 9566316..dd1c8bc 100644 --- a/backend/server/ticketRoutes.js +++ b/backend/server/ticketRoutes.js @@ -20,7 +20,9 @@ router.get("/news/", (req, res) => { }); router.get("/flights/", (req, res) => { + console.log("Getting Flights"); validators.validateSearchParams(req.query); + console.log("Validated Flights fields"); ticket_grpc.searchFlights(req.query, (error, response) => { if (!error) { const { list } = response; diff --git a/backend/ticketService/Dockerfile b/backend/ticketService/Dockerfile index e0bde1b..a4c8ec9 100755 --- a/backend/ticketService/Dockerfile +++ b/backend/ticketService/Dockerfile @@ -2,7 +2,6 @@ FROM node:14 WORKDIR / COPY . / -RUN rm -rf node_modules RUN npm install RUN chmod 0777 /run.sh diff --git a/backend/ticketService/resolvers/ticket.js b/backend/ticketService/resolvers/ticket.js index aa7cb69..3bc2089 100755 --- a/backend/ticketService/resolvers/ticket.js +++ b/backend/ticketService/resolvers/ticket.js @@ -104,19 +104,24 @@ function getFullFlightData(request, row) { const searchFlights = async ({request}, callback) => { try { + console.log("ticket_service: Getting flights"); const query = buildQuery(request); const { rows } = await db.complexSelect(query); + console.log("ticket_service: We have rows"); if (rows.length !== 0) { const result = rows.map(r => getFullFlightData(request, r)); + console.log("ticket_service: call back 1"); callback(null, { list: result}); } else { + console.log("ticket_service: call back 2"); callback({ code: grpc.status.NOT_FOUND, details: "Not Found" }) } } catch(e) { + console.log("ticket_service: exception"); console.log(e); callback(e); } From bb40fb29c9d16448b68bb579840e2428045761e0 Mon Sep 17 00:00:00 2001 From: mohammadhnz0 Date: Thu, 16 Feb 2023 22:59:09 +0330 Subject: [PATCH 2/8] fix: add fallback for cache --- backend/auth-service/grpc_server/auth/auth.go | 6 +++++- backend/auth-service/utils/jwt-utils.go | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/backend/auth-service/grpc_server/auth/auth.go b/backend/auth-service/grpc_server/auth/auth.go index abbf69a..b10d44c 100644 --- a/backend/auth-service/grpc_server/auth/auth.go +++ b/backend/auth-service/grpc_server/auth/auth.go @@ -107,7 +107,11 @@ func checkIfTokenHasBeenExpired(tokenData *TokenData) (error, *SignOutResponse, } if err != nil { - checkTokenIsNotUnAuthorized + _, _, err := utils.CheckTokenIsNotUnAuthorized(tokenData.AccessToken) + if err != nil { + return err, nil, nil, false + } + } return err, nil, nil, false } diff --git a/backend/auth-service/utils/jwt-utils.go b/backend/auth-service/utils/jwt-utils.go index 75e4148..c432f28 100644 --- a/backend/auth-service/utils/jwt-utils.go +++ b/backend/auth-service/utils/jwt-utils.go @@ -30,7 +30,7 @@ func GenerateOAuthToken(user models.User, secret string, expireTime time.Duratio func ExtractJwtToken(tokenString string, secret string) (jwt.MapClaims, models.User, error) { var claims jwt.MapClaims - mapClaims, m, err := checkTokenIsNotUnAuthorized(tokenString) + mapClaims, m, err := CheckTokenIsNotUnAuthorized(tokenString) if err != nil { return mapClaims, m, err } @@ -59,7 +59,7 @@ func ExtractJwtToken(tokenString string, secret string) (jwt.MapClaims, models.U return claims, user, nil } -func checkTokenIsNotUnAuthorized(tokenString string) (jwt.MapClaims, models.User, error) { +func CheckTokenIsNotUnAuthorized(tokenString string) (jwt.MapClaims, models.User, error) { var unauthorized_toke models.UnauthorizedToken config.DB.Where("token = ?", tokenString).First(&unauthorized_toke) if unauthorized_toke.User_id != 0 { From 2c31e371503598495c730cfcbe5744d32787fb78 Mon Sep 17 00:00:00 2001 From: mohammadhnz0 Date: Thu, 16 Feb 2023 23:40:59 +0330 Subject: [PATCH 3/8] feat: add reverse proxy --- backend/docker-compose.yml | 6 +++--- backend/reverse-proxy/nginx.conf | 31 ++++++++----------------------- 2 files changed, 11 insertions(+), 26 deletions(-) diff --git a/backend/docker-compose.yml b/backend/docker-compose.yml index 347ce26..6cce433 100755 --- a/backend/docker-compose.yml +++ b/backend/docker-compose.yml @@ -91,11 +91,11 @@ services: - ticket - auth reverse-proxy: + restart: always build: - context: ./server - dockerfile: Dockerfile + context: ./reverse-proxy ports: - - "8081:8081" + - "90:80" depends_on: - postgres - ticket diff --git a/backend/reverse-proxy/nginx.conf b/backend/reverse-proxy/nginx.conf index 823a6a8..9214567 100644 --- a/backend/reverse-proxy/nginx.conf +++ b/backend/reverse-proxy/nginx.conf @@ -1,25 +1,10 @@ server { - # this server listens on port 80 - listen 80 default_server; - listen [::]:80 default_server; - - # name this server "nodeserver", but we can call it whatever we like - server_name nodeserver; - - # the location / means that when we visit the root url (localhost:80/), we use this configuration - location / { - # a bunch of boilerplate proxy configuration - proxy_http_version 1.1; - proxy_cache_bypass $http_upgrade; - - proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection 'upgrade'; - proxy_set_header Host $host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Proto $scheme; - - # the real magic is here where we forward requests to the address that the Node.js server is running on - proxy_pass http://localhost:8081; - } + location / { + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + + proxy_pass http://server:8081; + } } \ No newline at end of file From 3b35a5f0c26cd03cf7c19506af0ae1f962b13b81 Mon Sep 17 00:00:00 2001 From: Mikaeel Date: Fri, 17 Feb 2023 01:37:49 +0330 Subject: [PATCH 4/8] fix: read from postgres when redis is not working --- backend/ticketService/data_access/db.js | 29 ++++++++++++++++++------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/backend/ticketService/data_access/db.js b/backend/ticketService/data_access/db.js index 84e2727..c311156 100644 --- a/backend/ticketService/data_access/db.js +++ b/backend/ticketService/data_access/db.js @@ -48,18 +48,31 @@ const Indexes = { } async function removeCache(index, values) { - if(values){ - const queryKey = createKey(index, values); - console.log("REMOVING", queryKey); - await redisClient.del(queryKey); - }else{ - const keys = await redisClient.keys(index + "_$$$_*"); - for(const key of keys) { - await redisClient.del(key); + try { + if(values){ + const queryKey = createKey(index, values); + console.log("REMOVING", queryKey); + await redisClient.del(queryKey); + }else{ + const keys = await redisClient.keys(index + "_$$$_*"); + for(const key of keys) { + await redisClient.del(key); + } } + } catch (Error) { + return; } } +async function redis_get(index, values) { + try { + return redisClient.get(queryKey); + } catch (Error) { + return null; + } +} + + const functions = { update: async ({text, values, dirtyIndexes}) => { const result = await postgresClient.query(text, values); From 78843366a275f31a8d7b6f839be7933e361b6ffd Mon Sep 17 00:00:00 2001 From: Mikaeel Date: Fri, 17 Feb 2023 01:38:48 +0330 Subject: [PATCH 5/8] fix: remove port mapping to enable scaling in docker-compose --- backend/docker-compose.yml | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/backend/docker-compose.yml b/backend/docker-compose.yml index 6cce433..8efcbb4 100755 --- a/backend/docker-compose.yml +++ b/backend/docker-compose.yml @@ -43,8 +43,6 @@ services: dockerfile: Dockerfile expose: - "50052" - ports: - - "50052:50052" depends_on: - authdb - authRedis @@ -78,8 +76,8 @@ services: build: context: ./server dockerfile: Dockerfile - ports: - - "8081:8081" + expose: + - "8081" depends_on: - postgres - ticket @@ -93,9 +91,10 @@ services: reverse-proxy: restart: always build: - context: ./reverse-proxy + context: ./reverse-proxy + dockerfile: Dockerfile ports: - - "90:80" + - "9000:80" depends_on: - postgres - ticket @@ -105,4 +104,9 @@ services: - postgres - bank - ticket - - auth \ No newline at end of file + - auth + +networks: + default: + driver: bridge + name: flightservice \ No newline at end of file From ba597d5efacc1874a4286f3201bff8a8eacb7fc7 Mon Sep 17 00:00:00 2001 From: Mikaeel Date: Fri, 17 Feb 2023 01:39:54 +0330 Subject: [PATCH 6/8] fix: revert reverse-proxy port --- backend/docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/docker-compose.yml b/backend/docker-compose.yml index 8efcbb4..f22d8ca 100755 --- a/backend/docker-compose.yml +++ b/backend/docker-compose.yml @@ -94,7 +94,7 @@ services: context: ./reverse-proxy dockerfile: Dockerfile ports: - - "9000:80" + - "90:80" depends_on: - postgres - ticket From 88f7f9ca3685f5b07a59ec509e17ba2c2a4f2279 Mon Sep 17 00:00:00 2001 From: Mikaeel Date: Fri, 17 Feb 2023 01:40:27 +0330 Subject: [PATCH 7/8] fix: wrong log --- backend/server/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/server/main.js b/backend/server/main.js index 1e9a24c..8955d88 100644 --- a/backend/server/main.js +++ b/backend/server/main.js @@ -20,6 +20,6 @@ app.use(validatorMiddleware); app.listen(8081, () => { const blue = chalk.blue - const target = blue(`http://localhost:8081`) + const target = blue(`http://server:8081`) console.log(`🚀 Server ready at ${target}`) }) From bdfeafdf9ecd3f675e9c8585309198c3f958ae54 Mon Sep 17 00:00:00 2001 From: Mikaeel Date: Fri, 17 Feb 2023 02:07:46 +0330 Subject: [PATCH 8/8] feat: add locustfile --- locust/locustfile.py | 101 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 locust/locustfile.py diff --git a/locust/locustfile.py b/locust/locustfile.py new file mode 100644 index 0000000..65181ee --- /dev/null +++ b/locust/locustfile.py @@ -0,0 +1,101 @@ +import random +import string + +from locust import HttpUser, task, between + + +def get_random_string(length): + letters = string.ascii_lowercase + return ''.join(random.choice(letters) for _ in range(length)) + + +def get_correct_time(): + correct_time_start = random.randint(946684800000, 2524508000000) + correct_time_end = random.randint(2523508000000, 2524508000000) + return correct_time_start, correct_time_end + + +def get_wrong_time(): + time_start = random.randint(2524508010000, 2524508020000) + return time_start, random.randint(10000, 300000) + + +def select_airport(): + if random.randint(1, 10) < 7: + return random.choice( + ["ALG", "AAE", "BLJ", "BJA", "BSK", "CFK", "CZL", "HME", "GJL", "ORN", "QSF", "TMR", "TLM", "HBE", "ALY", + "ATZ", "ASW", "CAI", "AAC", "DBB", "HRG", "LXR", "RMF", "MUH", "SKV", "SSH", "HMB", "TCP", "BEN", "SEB", + "TIP", "MJI", "AGA", "CMN", "FEZ", "RAK", "NDR", "OUD", "RBA", "TNG", "TTU", "VIL", "EUN", "KRT", "PZU", + "DJE", "NBE", "MIR", "SFA", "TBJ", "TOE", "TUN", "COO", "BOY", "OUA", "BVC", "SID", "RAI", "VXE", "ABJ", + "BJL", "ACC", "KMS", "TKD", "NYI", "HZO", "WZA", "TML", "CKY", "OXB", "BQE", "ROB", "BKO", "NKC", "NDB", + "ATR", "NIM"]) + return 'ALL' + + +class UserWithoutSearchFlight(HttpUser): + wait_time = between(1, 2) + + def on_start(self): + gmail = "user" + get_random_string(12) + "@gmail.com" + password = get_random_string(16) + phone = "09" + get_random_string(9) + self.client.post("/auth/signup/", json={ + "email": gmail, + "first_name": get_random_string(8), + "last_name": get_random_string(8), + "gender": "female", + "phone_number": phone, + "password": password + }) + + self.client.post("/auth/signin", json={ + "email": gmail, + "phone_number": phone, + "password": password + }) + + @task + def signin(self): + self.client.post("/auth/signin", json={ + "email": "user@gmail.com", + "phone_number": "09919700535", + "password": "password" + }) + + @task(3) + def suggest_origin_destination(self): + self.client.get(f"/suggest_origin_destination", params={'name': get_random_string(2)}) + + @task(5) + def get_news(self): + self.client.get(f"/news") + + @task(1) + def but_ticket(self): + self.client.get(f"/ticket/buy", json={ + "flight_id": "AD3E48", + "class_name": random.choice(['First Class', 'Economy', 'Business']), + "passengers": [{ + "name": get_random_string(8), + "family": get_random_string(8), + "passport": get_random_string(8) + }] + }) + + @task(2) + def get_ticket_list(self): + self.client.get(f"/ticket/list") + + +class NormalUser(UserWithoutSearchFlight): + @task(2) + def get_flights(self): + if random.randint(1, 10) < 11: + start, end = get_correct_time() + else: + start, end = get_wrong_time() + origin = select_airport() + destination = select_airport() + self.client.get(f"/flights", params={'origin': origin, 'destination': destination, + 'number_of_passengers': random.randint(1, 10), + 'departure_time': start, 'return_time': end})