Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HW3 #2

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*.env
*.idea*
*.vscode*
*auth-data/*
*ticket-data/*
# Test binary, built with `go test -c`
*.test

Expand Down
8 changes: 0 additions & 8 deletions auth-service/.idea/.gitignore

This file was deleted.

9 changes: 0 additions & 9 deletions auth-service/.idea/auth-service.iml

This file was deleted.

13 changes: 0 additions & 13 deletions auth-service/.idea/dataSources.xml

This file was deleted.

8 changes: 0 additions & 8 deletions auth-service/.idea/modules.xml

This file was deleted.

6 changes: 0 additions & 6 deletions auth-service/.idea/vcs.xml

This file was deleted.

12 changes: 12 additions & 0 deletions backend/auth-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ var RedisClient *redis.Client

func ConnectRedis() {
RedisClient = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Addr: "authRedis:6380",
})
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ func checkIfTokenHasBeenExpired(tokenData *TokenData) (error, *SignOutResponse,
Status: SignOutResponse_OK_201,
}, nil, true

}
if err != nil {
_, _, err := utils.CheckTokenIsNotUnAuthorized(tokenData.AccessToken)
if err != nil {
return err, nil, nil, false
}

}
return err, nil, nil, false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
authorization "awesomeProject/grpc_server/auth"
"context"
"google.golang.org/grpc"
"fmt"
"log"
)

Expand All @@ -30,25 +31,5 @@ func main() {
if err != nil {
log.Printf("Could not sign up: %s", err)
}

signInData := authorization.SignInData{
PhoneNumber: "0933041sdf2310360",
Email: "[email protected]",
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")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions backend/auth-service/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

sleep 3
echo "Running auth service..."

exec go run ./grpc_server/server.go
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
58 changes: 56 additions & 2 deletions backend/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ services:
expose:
- "6379"

authRedis:
image: redis:6.2-alpine
command: --port 6380
expose:
- "6380"

postgres:
build:
context: ./postgres
Expand All @@ -18,7 +24,31 @@ 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"
depends_on:
- authdb
- authRedis
links:
- authdb
- authRedis
bank:
build:
context: ./bankService
Expand Down Expand Up @@ -46,13 +76,37 @@ services:
build:
context: ./server
dockerfile: Dockerfile
expose:
- "8081"
depends_on:
- postgres
- ticket
- bank
- auth
links:
- postgres
- bank
- ticket
- auth
reverse-proxy:
restart: always
build:
context: ./reverse-proxy
dockerfile: Dockerfile
ports:
- "8081:8081"
- "90:80"
depends_on:
- postgres
- ticket
- bank
- auth
links:
- postgres
- bank
- ticket
- ticket
- auth

networks:
default:
driver: bridge
name: flightservice
2 changes: 2 additions & 0 deletions backend/reverse-proxy/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM nginx
COPY nginx.conf /etc/nginx/conf.d/default.conf
10 changes: 10 additions & 0 deletions backend/reverse-proxy/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
server {
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;
}
}
1 change: 0 additions & 1 deletion backend/server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ FROM node:14
WORKDIR /

COPY . /
RUN rm -rf node_modules
RUN npm install
RUN chmod 0777 /run.sh

Expand Down
1 change: 1 addition & 0 deletions backend/server/authHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}
Expand Down
5 changes: 3 additions & 2 deletions backend/server/authRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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})
}
Expand Down
2 changes: 1 addition & 1 deletion backend/server/grpc_clients/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
3 changes: 1 addition & 2 deletions backend/server/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ 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);


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}`)
})
2 changes: 2 additions & 0 deletions backend/server/ticketRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion backend/ticketService/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ FROM node:14
WORKDIR /

COPY . /
RUN rm -rf node_modules
RUN npm install
RUN chmod 0777 /run.sh

Expand Down
29 changes: 21 additions & 8 deletions backend/ticketService/data_access/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading