Skip to content
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
87 changes: 87 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
name: CI/CD Pipeline

on: push # Triggers the workflow on push to any branch

env:
POSTGRES_PRISMA_URL: postgresql://postgres:postgres@localhost:5432/test_db
POSTGRES_URL_NON_POOLING: postgresql://postgres:postgres@localhost:5432/test_db
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/test_db
JWT_SECRET: ${{ secrets.JWT_SECRET }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}

jobs:
build:
runs-on: ubuntu-latest

services:
postgres:
image: postgres:13
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: test_db
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '20.6.0'

- name: Install Dependencies
run: npm install

- name: Wait for PostgreSQL to start
run: |
for i in {1..10}; do
pg_isready -h localhost -p 5432 -U postgres && echo "PostgreSQL started" && break
echo "Waiting for PostgreSQL to start..."
sleep 5
done

- name: Push Prisma Schema to PostgreSQL
run: npx prisma db push

- name: Run Tests
env:
JWT_SECRET: secret
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
POSTGRES_PRISMA_URL: postgresql://postgres:postgres@localhost:5432/test_db
POSTGRES_URL_NON_POOLING: postgresql://postgres:postgres@localhost:5432/test_db
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/test_db
AWS_REGION: ${{ secrets.AWS_REGION }}
AWS_S3_BUCKET: ${{ secrets.AWS_S3_BUCKET }}
AWS_ACCESS_KEY: ${{ secrets.AWS_ACCESS_KEY }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
UPLOADS_DIR: ${{ secrets.UPLOADS_DIR }}
run: npm run test

deploy:
if: github.ref == 'refs/heads/main'
needs: build
runs-on: ubuntu-latest
steps:
- name: Setup SSH Agent
uses: webfactory/ssh-agent@v0.5.4
with:
ssh-private-key: ${{ secrets.DEPLOY_SSH_KEY }}
- name: Deploy to Web Servers
run: ssh -o StrictHostKeyChecking=no ubuntu@zencodeschool.com "/opt/zencodeschool.com/deploy_production.sh"

deploy_develop:
if: github.ref == 'refs/heads/develop'
needs: build
runs-on: ubuntu-latest

steps:
- name: Setup SSH Agents
uses: webfactory/ssh-agent@v0.5.4
with:
ssh-private-key: ${{ secrets.DEPLOY_SSH_KEY }}
- name: Deploy Develop Branch To Staging Web Server
run: ssh -o StrictHostKeyChecking=no ubuntu@develop.zencodeschool.com "/opt/zencodeschool.com/deploy_develop.sh"
2 changes: 1 addition & 1 deletion __tests__/swagger_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('Swagger Documentation', () => {

const response = await server.inject({
method: 'GET',
url: '/'
url: '/api'
})

expect(response.statusCode).to.be.equal(200)
Expand Down
86 changes: 82 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@hapi/inert": "^6.0.5",
"@hapi/joi": "^17.1.1",
"@hapi/vision": "^6.1.0",
"@prisma/client": "^5.6.0",
"@promster/hapi": "^12.0.0",
"commander": "^9.3.0",
"dotenv": "^10.0.0",
Expand All @@ -48,6 +49,7 @@
"commitizen": "^4.2.4",
"cz-conventional-changelog": "^3.3.0",
"mocha": "^9.1.2",
"prisma": "^5.6.0",
"shebang-trim": "^1.1.0",
"ts-node": "^10.9.1",
"typedoc": "^0.23.24"
Expand Down
51 changes: 51 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

model User {
id Int @id @default(autoincrement())
email String @unique
password String
username String
EmailPasswordAuth EmailPasswordAuth?
Instructor Instructor[]
JwtToken JwtToken[]
SocialMediaAuth SocialMediaAuth[]
}

model EmailPasswordAuth {
id Int @id @default(autoincrement())
userId Int @unique
password String
User User @relation(fields: [userId], references: [id])
}

model Instructor {
id Int @id @default(autoincrement())
userId Int?
name String
position String?
slug String?
image_url String?
User User? @relation(fields: [userId], references: [id])
}

model JwtToken {
id Int @id @default(autoincrement())
userId Int
token String
User User @relation(fields: [userId], references: [id])
}

model SocialMediaAuth {
id Int @id @default(autoincrement())
userId Int
provider String
socialId String
User User @relation(fields: [userId], references: [id])
}
6 changes: 6 additions & 0 deletions src/prisma.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()

export default prisma
28 changes: 27 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ if (config.get('prometheus_enabled')) {
}

server.route({
method: 'GET', path: '/api/v0/status',
method: 'GET',
path: '/api/v0/status',
handler: handlers.Status.index,
options: {
description: 'Simply check to see that the server is online and responding',
Expand All @@ -70,6 +71,31 @@ server.route({
}
})

const Instructor = Joi.object({
id: Joi.number().integer().required(),
name: Joi.string().required(),
position: Joi.string().email().optional(),
slug: Joi.string().email().optional(),
}).label('Instructor')

server.route({
method: 'GET',
path: '/api/instructors',
handler: handlers.Instructors.index,
options: {
description: 'List instructors at the academy',
tags: ['api', 'instructors'],
response: {
failAction: 'log',
schema: Joi.object({
instructors: Joi.array()
.items(Instructor)
.label('Instructors')
})
}
}
})

var started = false

export async function start() {
Expand Down
20 changes: 20 additions & 0 deletions src/server/handlers/instructors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

import prisma from '../../prisma'

export async function index(req, h) {

try {

const instructors = await prisma.instructor.findMany()

return h.response({ instructors }).code(200)

} catch(error) {

console.log(error)
return h.response(error).code(500)

}

}