-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteps.txt
140 lines (123 loc) · 5.24 KB
/
steps.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
AUTH SERVICE
Setting up the Base:
1. Create auth directory
2. npm init -y
3. npm install --save express typescript ts-node-dev @types/express
4. tsc --init
1. Create src directory
2. Create index.ts
3. In the package.json replace "test" script with:
"start": "ts-node-dev ./src/index.ts"
Creating Docker and Kubernetis config files:
1. In the root dir create Dockerfile
FROM node:alpine
WORKDIR /app
COPY . .
RUN apk upgrade
RUN npm install
CMD ["npm","start"]
2. Inside of Infra/k8 create a deployment/service configuration file for Kubernetis :
auth-depl.yaml
apiVersion: apps/v1
kind: Deployment
# This "metadata" gives a name for the actual deployment itself
metadata:
name: auth-depl
spec:
replicas: 1
selector:
# matchLabel: tells what pods the deployment will apply to.
matchLabels:
app: auth
template:
# "template" here explaining how each POD inside of the deployment will be created.
# The "template" is actually a pod Template. It describes a pod that is launched.
metadata:
labels:
app: auth
spec:
containers:
- name: auth
image: civilizador/auth_service:0.0.1
---
# AUTH SERVICE DEFINITION
apiVersion: v1
kind: Service
metadata:
name: auth-srv
spec:
selector:
app: auth
ports:
- name: auth
protocol: TCP
port: 3000
targetPort: 3000
3. Create a Skaffold config file inside of the service root directory:
skaffold.yaml
apiVersion: skaffold/v2alpha3
kind: Config
deploy: # will list all config files we want to load into our Cluster
kubectl:
manifests:
- ./infra/k8s/*
build:
local:
push: false
artifacts: # artifacts section indicates what are output files would be. In our case it's Docker image
-
image: civilizador/auth
context: auth
docker:
dockerfile: Dockerfile
sync: # indicates which files to watch for changes
manual:
-
src: 'src/**/*.ts' # all .ts files should be monitored.
dest: .
Handling Errors
1. Under src folder create "middleware" directory
2. Create error-handler.ts file
3. Define errorHandler function that will accept 4 arguments (err, req, res, next)
4. Export types for each argument from "express" and define type annotations for each argument
5. Import your errorHandler to index.ts file
6. Whenever Express catch an error it will send it to error handler
7. Create different Error classes for each error case and store them inside of the error directory.
Note that every error class will be based on generic, skeleton like template class callded CutomError under the same directory
Creating Mongo DB deployment:
1. Under infra/k8s create a new file to describe mongo-db deployment for AUTH service:
auth-mongo-depl.yaml
Creating MongoDB SCHEMA
1. Create a new directory under "src" called "models"
2. Create file called user.ts
3. Describe Schema for User model
Password Hashing
1. Create new directory services
2. Create new file services/password.ts
3. Define a Password class inside of it.
Every time we would need to hash a password we would be calling this class.
4. Define 2 static methods toHash and compare.
Static methods are methods that are callable directly from the class
and do not require to create a new instance of the class to call them.
Installing additional libraries for implementing AUTH:
1. Install "cookie-session" and "@types/cookie-session"
npm install --save "cookie-session" , "@types/cookie-session"
2. Install jasonwebtoken
npm install --save "jasonwebtoken" , "@types/jasonwebtoken"
3. Connect "cookie-session" to our app inside the "index.ts" :
secure: true option indicates that cookie will be used only with https connection.
app.use(cookieSession({
signed: false,
secure: true
}))
4. Generate a jwt tocken and create a new session inside of "#.ts":
// Generate JWT
const userJwt = jwt.sign({
id: user.id,
email: user.email,
name: user.name
}, 'ASaede122!!2sme');
// Store generated JWT inside of the session object
req.session = {
jwt: userJwt
};