Skip to content

Commit

Permalink
implement start session query
Browse files Browse the repository at this point in the history
  • Loading branch information
yukukotani committed Mar 16, 2021
1 parent 4da00b2 commit 7cbb36c
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ PG_DATABASE=twinte_session_service
PG_USER=postgres
PG_PASSWORD=postgres

# 1 year
SESSION_LIFETIME_HOURS=8760

DATABASE_URL="postgresql://${PG_USER}:${PG_PASSWORD}@${PG_HOST}:${PG_PORT}/${PG_DATABASE}?schema=public"
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
"@types/express": "^4.17.11",
"@types/jest": "^26.0.20",
"@types/node": "^14.14.20",
"@types/uuid": "^8.3.0",
"@typescript-eslint/eslint-plugin": "^4.12.0",
"@typescript-eslint/parser": "^4.12.0",
"dotenv": "^8.2.0",
"eslint": "^7.12.1",
"eslint-config-prettier": "^7.1.0",
"eslint-config-standard": "^16.0.2",
Expand All @@ -42,7 +44,9 @@
"@prisma/client": "2.18.0",
"clone": "^2.1.2",
"cross-env": "^7.0.3",
"dayjs": "^1.10.4",
"log4js": "^6.3.0",
"protobufjs": "github:iFwu/protobuf.js"
"protobufjs": "github:iFwu/protobuf.js",
"uuid": "^8.3.2"
}
}
3 changes: 3 additions & 0 deletions src/constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const sessionLifeTimeHours = process.env.SESSION_LIFETIME_HOURS
? parseInt(process.env.SESSION_LIFETIME_HOURS)
: 0
3 changes: 3 additions & 0 deletions src/database/prisma-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { PrismaClient } from '@prisma/client'

export const prismaClient = new PrismaClient()
17 changes: 17 additions & 0 deletions src/database/session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Session } from '@prisma/client'
import { prismaClient } from './prisma-client'
import { v4 as uuid } from 'uuid'
import { Dayjs } from 'dayjs'

export async function createSession({
userId,
expiredAt,
}: {
userId: string
expiredAt: Dayjs
}): Promise<Session> {
const id = uuid()
return await prismaClient.session.create({
data: { id, user_id: userId, expired_at: expiredAt.toDate() },
})
}
12 changes: 0 additions & 12 deletions src/grpc/hello.service.ts

This file was deleted.

10 changes: 5 additions & 5 deletions src/grpc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import * as protoLoader from '@grpc/proto-loader'
import path from 'path'
import { logger } from '../logger'
import { applyLogger } from './logger'
import { helloService } from './hello.service'
import { sessionService } from './session.service'

const protoPath = path.resolve(__dirname, `../../protos/HelloService.proto`)
const serviceName = 'HelloService'
const protoPath = path.resolve(__dirname, `../../protos/SessionService.proto`)
const serviceName = 'SessionService'

let server: grpc.Server | undefined

const def = protoLoader.loadSync(protoPath, { defaults: true })
const helloServiceDef = (grpc.loadPackageDefinition(def)[
const serviceDef = (grpc.loadPackageDefinition(def)[
serviceName
] as ServiceClientConstructor).service

Expand All @@ -23,7 +23,7 @@ export async function startGrpcServer() {
return new Promise<void>((resolve, reject) => {
if (server) reject(new Error('already started'))
server = new grpc.Server()
server.addService(helloServiceDef, applyLogger(helloService))
server.addService(serviceDef, applyLogger(sessionService))
server.bindAsync(
'0.0.0.0:50051',
grpc.ServerCredentials.createInsecure(),
Expand Down
30 changes: 30 additions & 0 deletions src/grpc/session.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import dayjs from 'dayjs'
import { google, SessionService, StartSessionResponse } from '../../generated'
import { sessionLifeTimeHours } from '../constant'
import { createSession } from '../database/session'
import { GrpcServer } from './type'

export const sessionService: GrpcServer<SessionService> = {
async startSession({ request }, callback) {
const session = await createSession({
userId: request.userId,
expiredAt: dayjs().add(sessionLifeTimeHours, 'hour'),
})

callback(
null,
StartSessionResponse.create({
session: {
sessionId: session.id,
userId: session.user_id,
expiredAt: google.protobuf.Timestamp.create({
seconds: session.expired_at.getTime() / 1000,
}),
},
})
)
},
getSession({ request }, callback) {
// not implemented yet
},
}
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import dotenv from 'dotenv'
dotenv.config()

import { startGrpcServer } from './grpc'
import { logger } from './logger'

Expand Down
17 changes: 16 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,11 @@
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.0.tgz#7036640b4e21cc2f259ae826ce843d277dad8cff"
integrity sha512-RJJrrySY7A8havqpGObOB4W92QXKJo63/jFLLgpvOtsGUqbQZ9Sbgl35KMm1DjC6j7AvmmU2bIno+3IyEaemaw==

"@types/uuid@^8.3.0":
version "8.3.0"
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-8.3.0.tgz#215c231dff736d5ba92410e6d602050cce7e273f"
integrity sha512-eQ9qFW/fhfGJF8WKHGEHZEyVWfZxrT+6CLIJGBcZPfxUh/+BnEj+UCGYMlr9qZuX/2AltsvwrGqp0LhEW8D0zQ==

"@types/yargs-parser@*":
version "20.2.0"
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-20.2.0.tgz#dd3e6699ba3237f0348cd085e4698780204842f9"
Expand Down Expand Up @@ -1650,6 +1655,11 @@ date-format@^3.0.0:
resolved "https://registry.yarnpkg.com/date-format/-/date-format-3.0.0.tgz#eb8780365c7d2b1511078fb491e6479780f3ad95"
integrity sha512-eyTcpKOcamdhWJXj56DpQMo1ylSQpcGtGKXcU0Tb97+K56/CF5amAqqqNj0+KvA0iw2ynxtHWFsPDSClCxe48w==

dayjs@^1.10.4:
version "1.10.4"
resolved "https://registry.yarnpkg.com/dayjs/-/dayjs-1.10.4.tgz#8e544a9b8683f61783f570980a8a80eaf54ab1e2"
integrity sha512-RI/Hh4kqRc1UKLOAf/T5zdMMX5DQIlDxwUe3wSyMMnEbGunnpENCdbUgM+dW7kXidZqCttBrmw7BhN4TMddkCw==

debug@4, debug@^4.0.1, debug@^4.1.0, debug@^4.1.1:
version "4.3.1"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.1.tgz#f0d229c505e0c6d8c49ac553d1b13dc183f6b2ee"
Expand Down Expand Up @@ -1774,6 +1784,11 @@ domexception@^2.0.1:
dependencies:
webidl-conversions "^5.0.0"

dotenv@^8.2.0:
version "8.2.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.2.0.tgz#97e619259ada750eea3e4ea3e26bceea5424b16a"
integrity sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==

ecc-jsbn@~0.1.1:
version "0.1.2"
resolved "https://registry.yarnpkg.com/ecc-jsbn/-/ecc-jsbn-0.1.2.tgz#3a83a904e54353287874c564b7549386849a98c9"
Expand Down Expand Up @@ -5262,7 +5277,7 @@ uuid@^3.3.2:
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee"
integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==

uuid@^8.3.0:
uuid@^8.3.0, uuid@^8.3.2:
version "8.3.2"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==
Expand Down

0 comments on commit 7cbb36c

Please sign in to comment.