Skip to content

Commit

Permalink
Add docker instances
Browse files Browse the repository at this point in the history
  • Loading branch information
rabarbar15 committed Jun 27, 2024
1 parent a6b2de8 commit 193aff3
Show file tree
Hide file tree
Showing 57 changed files with 8,731 additions and 0 deletions.
1 change: 1 addition & 0 deletions Zad10 - Chmura:CI/back-cloud/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/node_modules
132 changes: 132 additions & 0 deletions Zad10 - Chmura:CI/back-cloud/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

.DS_Store
17 changes: 17 additions & 0 deletions Zad10 - Chmura:CI/back-cloud/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM alpine:3.19.1

RUN apk update && apk upgrade
RUN apk add --update nodejs npm sqlite

WORKDIR /backend
COPY ./package.json .
COPY ./package-lock.json .
RUN npm install

COPY . .

VOLUME /backend/node_modules

EXPOSE 3003

CMD ["sh", "-c", "npm run devStart"]
122 changes: 122 additions & 0 deletions Zad10 - Chmura:CI/back-cloud/controllers/authController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
const { User } = require('../model')
const jwt = require('jsonwebtoken')
// const bcrypt = require('bcrypt')

const handleErrors = (err) => {
let errors = { email: '', password: '' }

if (err.message.includes('Validation error')) {
Object.values(err.errors).forEach(error => {
errors[error.path] = error.message
console.log(errors);
})
}

if (err.message.includes('Incorrect password')) {
errors.password = 'Incorrect password'
}

if (err.message.includes('User not found')) {
errors.email = 'User not found'
}

return errors
}

const maxAge = 3 * 24 * 60 * 60

const createToken = (id) => {
return jwt.sign({ id }, 'my big secret', {
expiresIn: maxAge
})
}

exports.signup = async (req, res) => {

try {
const { email, password } = req.body;

if ( !email || !password) {
return res.status(400).json({ error: 'Wszystkie pola są wymagane' });
}

console.log(email, password);

const user = await User.create({
email: email,
password: password
})
// console.log(user.id);
const token = createToken(user.id)
res.cookie('jwt', token, { httpOnly: true, maxAge: maxAge * 1000})

res.status(201).json({ user: user.id })

} catch (error) {
console.log(error);
const errors = handleErrors(error)
console.error("Błąd przy rejestracji uzytkownika", errors)
res.status(400).json({errors})
}
}


exports.signin = async (req, res) => {

try {
const { email, password} = req.body;

if (!email || !password) {
return res.status(400).json({ error: 'Wszystkie pola są wymagane' });
}

try {
const user = await User.login(email, password)
console.log(user)

const token = createToken(user.id)
res.cookie('jwt', token, { httpOnly: true, maxAge: maxAge * 1000})

res.status(201).json({ user: user.id })
} catch (error) {
console.log(error);
const errors = handleErrors(error)
res.status(400).json({errors})
}

} catch (error) {
const errors = handleErrors(error)
console.error("Błąd przy logowaniu uzytkownika", errors)
res.status(400).json({errors})
}
}

exports.authConfirm = (req, res) => {
res.status(201).json({ auth: 'Signed in' })
}

exports.getUser = (req, res) => {

const token = req.cookies.jwt

if (token) {
jwt.verify(token, 'my big secret', async (err, decodedToken) => {
if (err) {
res.status(401).json({ message: 'Unauthorized' });

} else {
let user = await User.findByPk(decodedToken.id)
console.log('user id:', user.id)
res.send(user)
}
})
} else {
res.status(401).json({ message: 'Unauthorized' });
}
}

exports.logout = (req, res) => {
// replace jwt with a blank cookie
res.cookie('jwt', '', { maxAge: 1 })
res.send('Logged out')
}
54 changes: 54 additions & 0 deletions Zad10 - Chmura:CI/back-cloud/controllers/bookController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

const path = require('path');


const books = [
{ id: 0, title: "Drakula", author: "Bram Stoker", price: 10.99, imgUrl: '../imgs/drakula.jpg' },
{ id: 1, title: "Behawiorysta", author: "Remigiusz Mróz", price: 8.99, imgUrl: '../imgs/behawiorysta.jpg' },
{ id: 2, title: "Dwie wieże", author: "J.R.R. Tolkien", price: 11.99, imgUrl: '../imgs/dwie-wieze.jpg' },
{ id: 3, title: "Jobs", author: "Walter Isaacson", price: 11.99, imgUrl: '../imgs/jobs.jpg' },
{ id: 4, title: "Out", author: "Natsu Kirimo", price: 11.99, imgUrl: '../imgs/out.jpg' },
{ id: 5, title: "Solaris", author: "Stanisław Lem", price: 11.99, imgUrl: '../imgs/solaris.jpg' },
{ id: 6, title: "Szklany Klosz", author: "Sylvia Plath", price: 11.99, imgUrl: '../imgs/szklany-klosz.jpg' },
{ id: 7, title: "Ziemiomorze", author: "Ursula K. Le Guin", price: 11.99, imgUrl: '../imgs/ziemiomorze.jpg' },
// { id: 8, title: "Miasteczko Salem", author: "Stephen King", price: 11.99, imgUrl: '../imgs/miasteczko-salem.jpg' }
];


exports.getAllBooks = async (req, res) => {
try {
res.send(books)

} catch (error) {
console.error(error);
res.status(500).send('Internal Server Error');
}
};

exports.getBookById = async (req, res) => {
try {
const bookId = req.params.id;
const book = books[bookId]

if (!book) {
res.status(404).send('Item not found');
} else {
res.json(book)
}
} catch (error) {
console.error(error);
res.status(500).send('Internal Server Error');
}
};

exports.getBookImg = async (req, res) => {
const id = req.params.id
// console.log(id);

try {
const imgPath = path.join(__dirname, books[id].imgUrl);
res.sendFile(imgPath)
} catch(error) {
res.status(500).send(error)
}
}
25 changes: 25 additions & 0 deletions Zad10 - Chmura:CI/back-cloud/controllers/cartController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// const { Cart } = require('./model')

exports.createCartItem = async (req, res) => {
try {
res.cookie('session_id', '12345')
res.status(200).json({ msg: 'Cookie' })
} catch (error) {
console.error("Błąd przy dodawaniu ksiąki do koszyka", error.message)
res.status(500).send('Internal Server Error');
}
}


exports.validateCookie = (req, res, next) => {
const { cookies } = req;
if ('session_id' in cookies) {
console.log('session id exists');
if (cookies.session_id === '12345') next()
else res.status(403).send({ msg: 'Not Authenticated' })
} else {
res.status(403).send({ msg: 'Not Authenticated' })
}
// console.log(cookies);

}
Loading

0 comments on commit 193aff3

Please sign in to comment.