Commit d35ffeb6 authored by kavinduchamiran's avatar kavinduchamiran

first commit

parents
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# production
/build
# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.idea
*.cast
*.gif
\ No newline at end of file
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# production
/build
# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.idea
*.cast
*.gif
\ No newline at end of file
stages:
- build
- deploy
variables:
CONTAINER_IMAGE: kavinduchamiran/${CI_COMMIT_REF_NAME}:${CI_COMMIT_SHORT_SHA}
docker_build:
image: docker:stable
services:
- docker:dind
stage: build
only:
- cloudl-server
- cloudl-client
script:
- docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD}
- docker build -t ${CONTAINER_IMAGE} .
- docker tag ${CONTAINER_IMAGE} kavinduchamiran/${CI_COMMIT_REF_NAME}:latest
- docker push ${CONTAINER_IMAGE}
kube_deploy:
image: lwolf/kubectl_deployer:latest
stage: deploy
only:
- cloudl-server
- cloudl-client
script:
- kubectl config set-cluster cloudl --server="${KUBE_URL}" --certificate-authority="${KUBE_CA_PEM}"
- kubectl config set-credentials admin --username="${KUBE_USERNAME}" --password="${KUBE_PASSWORD}"
- kubectl config set-context default --cluster=cloudl --user=${KUBE_USERNAME}
- kubectl config use-context default
- kubectl set image deployment/${CI_COMMIT_REF_NAME}-deployment ${CI_COMMIT_REF_NAME}=kavinduchamiran/${CI_COMMIT_REF_NAME}:latest
\ No newline at end of file
FROM node:10
WORKDIR /cloudl-server
COPY package.json ./
RUN npm install
COPY . .
EXPOSE 5000
CMD ["npm", "start"]
\ No newline at end of file
apiVersion: apps/v1
kind: Deployment
metadata:
name: cloudl-server-deployment
spec:
replicas: 1
template:
metadata:
labels:
app: cloudl-server
spec:
containers:
- name: cloudl-server
image: kavinduchamiran/cloudl-server:4897efb7
ports:
- containerPort: 5000
name: nodejs-port
selector:
matchLabels:
app: cloudl-server
\ No newline at end of file
apiVersion: v1
kind: Service
metadata:
name: cloudl-server-service
spec:
ports:
- port: 5000
protocol: TCP
selector:
app: cloudl-server
type: NodePort
\ No newline at end of file
#!/usr/bin/env bash
gcloud container clusters create cloudl \
--scopes "cloud-platform" \
--num-nodes 1 \
--enable-basic-auth \
--issue-client-certificate \
--enable-ip-alias \
--zone northamerica-northeast1-a
\ No newline at end of file
#!/usr/bin/env bash
kubectl create -f mongodb-pod.yml
kubectl create -f mongodb-service.yml
kubectl create -f cloudl-server-service.yml
kubectl create -f cloudl-server-deployment.yml
kubectl get pods
kubectl get svc
apiVersion: v1
kind: Pod
metadata:
name: mongodb
labels:
app: mongodb
spec:
containers:
- name: mongodb
image: mongo:4.0.4
ports:
- containerPort: 27017
name: mongodb-port
\ No newline at end of file
apiVersion: v1
kind: Service
metadata:
name: mongodb-service
spec:
ports:
- port: 27017
protocol: TCP
selector:
app: mongodb
type: NodePort
\ No newline at end of file
#!/usr/bin/env bash
kubectl delete pod/mongodb
kubectl delete svc/mongodb-service
kubectl delete deploy/cloudl-server-deployment
kubectl delete svc/cloudl-server-service
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
// Create Schema
const JobSchema = new Schema({
link: {
type: String,
required: true
},
userId: {
type: String,
required: true
},
status: {
type: String,
default: 'NEW'
},
progress: {
type: Number,
default: 0
},
date: {
type: Date,
default: Date.now
}
});
module.exports = User = mongoose.model("jobs", JobSchema);
\ No newline at end of file
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
// Create Schema
const UserSchema = new Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
}
});
module.exports = User = mongoose.model("users", UserSchema);
\ No newline at end of file
This diff is collapsed.
{
"name": "cloudl-server",
"version": "1.0.0",
"description": "cloudl node server",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"is-empty": "^1.2.0",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.6.10",
"passport": "^0.4.0",
"passport-jwt": "^4.0.0",
"react-router-dom": "^4.3.1",
"socket.io": "^2.2.0",
"validator": "^11.1.0"
}
}
const express = require("express");
const router = express.Router();
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");
// Load input validation
const validateJobInput = require("../../utils/job");
// Load User model
const Job = require("../../models/Job");
// @route POST api/jobs/add
// @desc Register user
// @access Public
router.post("/add", (req, res) => {
// Form validation
const { errors, isValid } = validateJobInput(req.body);
// Check validation
if (!isValid) {
return res.status(400).json(errors);
}
// Job.findOne({ link: req.body.link }).then(job => {
// if (job) {
// return res.status(400).json({ link: "Link already downloading" });
// } else {
// const newJob = new Job({
// link: req.body.link,
// userId: req.body.id
// });
// newJob
// .save()
// .then(job => res.json(job))
// .catch(err => console.log(err));
// }
// });
});
module.exports = router;
\ No newline at end of file
const express = require("express");
const router = express.Router();
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");
// Load input validation
const validateRegisterInput = require("../../utils/register");
const validateLoginInput = require("../../utils/login");
// Load User model
const User = require("../../models/User");
// @route POST api/users/register
// @desc Register user
// @access Public
router.post("/register", (req, res) => {
// Form validation
const { errors, isValid } = validateRegisterInput(req.body);
// Check validation
if (!isValid) {
return res.status(400).json(errors);
}
User.findOne({ email: req.body.email }).then(user => {
if (user) {
return res.status(400).json({ email: "Email already exists" });
} else {
const newUser = new User({
name: req.body.name,
email: req.body.email,
password: req.body.password
});
// Hash password before saving in database
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(newUser.password, salt, (err, hash) => {
if (err) throw err;
newUser.password = hash;
newUser
.save()
.then(user => res.json(user))
.catch(err => console.log(err));
});
});
}
});
});
router.post("/login", (req, res) => {
// Form validation
const { errors, isValid } = validateLoginInput(req.body);
// Check validation
if (!isValid) {
return res.status(400).json(errors);
}
const email = req.body.email;
const password = req.body.password;
// Find user by email
User.findOne({ email }).then(user => {
// Check if user exists
if (!user) {
return res.status(404).json({ emailnotfound: "Email not found" });
}
// Check password
bcrypt.compare(password, user.password).then(isMatch => {
if (isMatch) {
// User matched
// Create JWT Payload
const payload = {
id: user.id,
name: user.name
};
// Sign token
jwt.sign(
payload,
'secret',
{
expiresIn: 31556926 // 1 year in seconds
},
(err, token) => {
res.json({
success: true,
token: "Bearer " + token
});
}
);
} else {
return res
.status(400)
.json({ passwordincorrect: "Password incorrect" });
}
});
});
});
module.exports = router;
\ No newline at end of file
const express = require('express');
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const passport = require("passport");
const cors = require('cors');
const socketIO = require('socket.io');
const http = require('http');
const users = require('./routes/api/users');
const jobs = require('./routes/api/jobs');
const Job = require("./models/Job");
const app = express();
app.use(
bodyParser.urlencoded({
extended: false
})
);
app.use(bodyParser.json());
app.use(cors());
mongoose
.connect(
`mongodb://mongodb-service/cloudl`, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => console.log("MongoDB successfully connected"))
.catch(err => console.log(err));
const server = http.createServer(app);
const io = socketIO(server);
let sockets = [];
let python_socket;
io.on('connection', socket => {
if(socket.handshake.headers.name === 'python'){
python_socket = socket;
}
// if(socket.handshake.headers.name === 'user')
// python_socket = socket;
socket.on('getJobsByUser', user => {
let {userId} = user;
sockets[userId] = socket;
Job.find({ userId }).then(jobs => {
if (!jobs) {
return res.status(400).json({ error: "No jobs found" });
} else {
socket.emit("jobsByUser", jobs)
}
});
});
if (python_socket) {
socket.on('newJob', job => {
let {id, link} = job;
python_socket.emit('new_job', {id, link})
});
socket.on('getJobs', user => {
python_socket.emit('get_jobs', user)
});
socket.on('deleteJob', job => {
python_socket.emit('remove_job', job)
});
socket.on('resumeJob', job => {
python_socket.emit('resume_job', job)
});
socket.on('pauseJob', job => {
python_socket.emit('pause_job', job)
});
socket.on('downloadJob', job => {
python_socket.emit('download_job', job)
});
python_socket.on('error', error => {
socket.emit('err', error)
});
python_socket.on('job_add_success', () => {
socket.emit('job_add_success');
});
python_socket.on('get_jobs_res', res => {
socket.emit('get_jobs_res', res);
});
python_socket.on('download_ready', res => {
socket.emit('download_ready', res);
});
}
});
app.use(passport.initialize());
require("./utils/passport")(passport);
app.use("/api/users", users);
app.use("/api/jobs", jobs);
app.get('/', (req, res) => {
res.send('ClouDL server up and running.');
});
const port = process.env.PORT || 5000;
server.listen(port, () => console.log(`Server up and running on port ${port} !`));
\ No newline at end of file
const Validator = require("validator");
const isEmpty = require("is-empty");
module.exports = function validateJobInput(data) {
let errors = {};
// Convert empty fields to an empty string so we can use validator functions
data.link = !isEmpty(data.link) ? data.link : "";
data.id = !isEmpty(data.id) ? data.id : "";
// Name checks
if (Validator.isEmpty(data.link)) {
errors.link = "Link is required";
}else{
if (!Validator.isURL(data.link)) {
console.log(1);
errors.linkincorrect = "Link is incorrect";
}else if (!Validator.isURL(data.link) && !Validator.isMagnetURI(data.link) ) {
console.log(2);
errors.magnetincorrect = "Magnet link is not correct";
}
}
if (Validator.isEmpty(data.id)) {
errors.id = "User not found";
}
return {
errors,
isValid: isEmpty(errors)
};
};
\ No newline at end of file
const Validator = require("validator");
const isEmpty = require("is-empty");
module.exports = function validateLoginInput(data) {
let errors = {};
// Convert empty fields to an empty string so we can use validator functions
data.email = !isEmpty(data.email) ? data.email : "";
data.password = !isEmpty(data.password) ? data.password : "";
// Email checks
if (Validator.isEmpty(data.email)) {
errors.email = "Email field is required";
} else if (!Validator.isEmail(data.email)) {
errors.email = "Email is invalid";
}
// Password checks
if (Validator.isEmpty(data.password)) {
errors.password = "Password field is required";
}
return {
errors,
isValid: isEmpty(errors)
};
};
\ No newline at end of file
const JwtStrategy = require("passport-jwt").Strategy;
const ExtractJwt = require("passport-jwt").ExtractJwt;
const mongoose = require("mongoose");
const User = mongoose.model("users");
const opts = {};
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = 'secret';
module.exports = passport => {
passport.use(
new JwtStrategy(opts, (jwt_payload, done) => {
User.findById(jwt_payload.id)
.then(user => {
if (user) {
return done(null, user);
}
return done(null, false);
})
.catch(err => console.log(err));
})
);
};
\ No newline at end of file
const Validator = require("validator");
const isEmpty = require("is-empty");
module.exports = function validateRegisterInput(data) {
let errors = {};
// Convert empty fields to an empty string so we can use validator functions
data.name = !isEmpty(data.name) ? data.name : "";
data.email = !isEmpty(data.email) ? data.email : "";
data.password = !isEmpty(data.password) ? data.password : "";
data.password2 = !isEmpty(data.password2) ? data.password2 : "";
// Name checks
if (Validator.isEmpty(data.name)) {
errors.name = "Name field is required";
}
// Email checks
if (Validator.isEmpty(data.email)) {
errors.email = "Email field is required";
} else if (!Validator.isEmail(data.email)) {
errors.email = "Email is invalid";
}
// Password checks
if (Validator.isEmpty(data.password)) {
errors.password = "Password field is required";
}
if (Validator.isEmpty(data.password2)) {
errors.password2 = "Confirm password field is required";
}
if (!Validator.isLength(data.password, { min: 6, max: 30 })) {
errors.password = "Password must be at least 6 characters";
}
if (!Validator.equals(data.password, data.password2)) {
errors.password2 = "Passwords must match";
}
return {
errors,
isValid: isEmpty(errors)
};
};
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment