first commit

This commit is contained in:
n0rdye 2023-02-22 00:49:40 +05:00
commit af41db4b4d
18 changed files with 1957 additions and 0 deletions

2
.dockerignore Normal file
View File

@ -0,0 +1,2 @@
node_modules
npm-debug.log

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/node_modules

19
Dockerfile Normal file
View File

@ -0,0 +1,19 @@
FROM node:16
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./
RUN npm install
# If you are building your code for production
# RUN npm ci --only=production
# Bundle app source
COPY . .
EXPOSE 8080
CMD [ "node", "index.js" ]

7
docker-compose.yaml Normal file
View File

@ -0,0 +1,7 @@
services:
pills:
image: n0rdye/pills
ports:
- 8080:8080
volumes:
- ./:/usr/src/app

133
index.js Normal file
View File

@ -0,0 +1,133 @@
const express = require('express');
const {
response,
request
} = require('express');
const app = express();
const today = new Date()
const day = today.getDate();
const month = today.getMonth()+1;
const year = today.getFullYear();
const { json } = require("body-parser");
const { pathToFileURL } = require("url");
app.set('view engine','ejs');
app.use(express.urlencoded({extended: false}));
app.use(express.static('public'));
const fs = require('fs');
app.get('/user', (req,res) => {
fs.readFile('./pills.json', 'utf8', (err, data) => {
let jdata = JSON.parse(data);
res.render(`user`, { data: dict_reverse(jdata)});
})
})
app.get('/', (req,res) => {
res.redirect("/user");
})
function dict_reverse(obj) {
new_obj= {}
rev_obj = Object.keys(obj).reverse();
rev_obj.forEach(function(i) {
new_obj[i] = obj[i];
})
return new_obj;
}
app.post('/scripts',(req,res) => {
let inp = req.body;
console.log(inp);
if(inp["func"]=="save"){
let pill = `[${inp["pill"]}]`;
let comment = inp["comment"];
fs.readFile('./pills.json','utf8',(err,data)=>{
let jdata = JSON.parse(data);
let num = j_max(jdata)+1;
let name = `pill_${num}`;
let c_date = `${day}.${month}.${year}`;
jwrite_pill(name,pill,c_date,comment);
})
console.log(pill);
}
else if(inp["func"]=="del"){
let pill = inp["pill"];
fs.readFile('./pills.json','utf8',(err,data)=>{
let jdata = JSON.parse(data);
delete jdata[pill];
jwrite(jdata);
})
}
else if(inp["func"]=="edit"&&inp["mode"]=="save"){
let name = inp["name"];
let date = inp["date"];
let comment = inp["comment"];
let pill = inp["pill"];
console.log("ecdit");
jwrite_pill(name,pill,date,comment);
}
res.redirect("/user");
})
function jwrite(jdata){
// console.log(jdata);
let wdata = JSON.stringify(jdata);
fs.writeFile('./pills.json', wdata, 'utf8', err => {
if (err) {
console.log(`Error writing file: ${err}`)
} else {
console.log(`File is written successfully!`)
}
})
}
function jwrite_pill(name,pill,date,comment){
comment = (comment==""||comment==null&&comment==" ")? "-":comment;
fs.readFile('./pills.json','utf8',(err,data)=>{
let jdata = JSON.parse(data);
jdata[name] = {
"date":date,
"pill":pill,
"comment":comment
};
// console.log(jdata);
let wdata = JSON.stringify(jdata);
fs.writeFile('./pills.json', wdata, 'utf8', err => {
if (err) {
console.log(`Error writing file: ${err}`)
} else {
console.log(`File is written successfully!`)
}
})
})
}
app.post("/edit",(req,res) =>{
let inp = req.body;
let name = inp["name"];
let pill = inp["pill"];
let date = inp["date"];
let comment = inp["comment"];
console.log("e");
res.render(`edit`, { name:name,pill:pill,comment:comment,date:date});
})
function j_max(arg){
let max = 0;
for (const key in arg) {
let num = parseInt(key.split("_")[parseInt(key.split("_").length-1)]);
// console.log(key);
max = (num>max)? num:max;
}
console.log(max);
return max;
}
app.all('*', (req, res) => {
res.status(404).send('<h1>404! Page not</h1>');
});
app.listen(process.env.PORT || 8080, () => console.log("started"));

1391
package-lock.json generated Executable file

File diff suppressed because it is too large Load Diff

18
package.json Executable file
View File

@ -0,0 +1,18 @@
{
"name": "pills",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.20.1",
"ejs": "^3.1.8",
"express": "^4.18.2",
"express-session": "^1.17.3"
}
}

1
pills.json Normal file

File diff suppressed because one or more lines are too long

56
sacrap/fx.js Normal file
View File

@ -0,0 +1,56 @@
exports.db_put = {db_put};
function db_put(callback,arg){
pool.query("UPDATE `pills` SET `json`='",'"',arg,'"', function(err, result){
if (err) console.log(err,null);
else callback(null,result[0]);
});
}
function jwrite(jdata){
// console.log(jdata);
let wdata = JSON.stringify(jdata);
fs.writeFile('./mods/pills.json', wdata, 'utf8', err => {
if (err) {
console.log(`Error writing file: ${err}`)
} else {
console.log(`File is written successfully!`)
}
})
}
function jwrite_pill(name,pill,date,comment){
comment = (comment==""||comment==null&&comment==" ")? "-":comment;
fs.readFile('./mods/pills.json','utf8',(err,data)=>{
let jdata = JSON.parse(data);
jdata[name] = {
"date":date,
"pill":pill,
"comment":comment
};
// console.log(jdata);
let wdata = JSON.stringify(jdata);
fs.writeFile('./mods/pills.json', wdata, 'utf8', err => {
if (err) {
console.log(`Error writing file: ${err}`)
} else {
console.log(`File is written successfully!`)
}
})
})
}
function j_max(arg){
let max = 0;
for (const key in arg) {
let num = parseInt(key.split("_")[parseInt(key.split("_").length-1)]);
// console.log(key);
max = (num>max)? num:max;
}
console.log(max);
return max;
}
function dict_reverse(obj) {
new_obj= {}
rev_obj = Object.keys(obj).reverse();
rev_obj.forEach(function(i) {
new_obj[i] = obj[i];
})
return new_obj;
}

15
sacrap/i2.ejs Normal file
View File

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
bye
</body>
</html>

127
sacrap/inde2.js Normal file
View File

@ -0,0 +1,127 @@
const mysql = require("mysql");
const express = require('express');
const {
response,
request
} = require('express');
const app = express();
var vars = require('./vars.js');
const { json } = require("body-parser");
const { pathToFileURL } = require("url");
app.set('view engine','ejs');
app.use(express.urlencoded({extended: false}));
app.use(express.static('public'));
const {
readFile
} = require('fs').promises;
const fs = require('fs');
const pool = mysql.createPool({
connectionLimit: 100,
host: "172.18.0.2",
user: "root",
password: "root",
database: "pills",
debug: false,
});
// database
function db_get(callback){
pool.query('SELECT * FROM pills', function(err, result){
if (err) console.log(err,null);
else callback(null,result[0]);
});
};
function db_put(callback,arg){
pool.query("UPDATE `pills` SET `json`='",'"',arg,'"', function(err, result){
if (err) console.log(err,null);
else callback(null,result[0]);
});
};
// get(function(err,data){
// vars.d = data["id"];
// console.log(vars.d);
// })
// async function s() {
// const promise = await new Promise((resolve, reject) => {pool.query(`SELECT * FROM pills`, (err, result) => {resolve(result[0])})});
// return promise["json"];
// }
// // // s();
// async function l(){
// let json = JSON.parse(await s());
// // console.log(json);
// return json;
// }
// l();
app.get('/', async (req, res) => {
res.render('index');
});
app.get('/user', (req,res) => {
db_get(function(err,data){
let jdata = JSON.parse(data["json"]);
// window.localStorage.setItem("pill/jdata?/","jdata");
let pills = [];
let ids = [];
for (const key in jdata) {
if (jdata.hasOwnProperty.call(jdata, key)) {
const element = jdata[key];
if(element["pill"]!=""&&element["pill"]!=null&&element["pill"]!=" "&&element["pill"]!="undefined"){
pills.push(element["pill"]);
ids.push(key);
// console.log(key);
}
}
}
// console.log(JSON.stringify(jdata));
res.render(`user`, { data:JSON.stringify(jdata)});
})
})
// function name(params) {
// .then(response => response.json())
// .then(responseJson => {
// // set localStorage with your preferred name,..
// // ..say 'my_token', and the value sent by server
// window.localStorage.setItem('my_token', responseJson.my_token)
// // you may also want to redirect after you have saved localStorage:
// // window.location.assign("http://www.example.org")
// // you may even want to return responseJson or something else or assign it to some variable
// // return responseJson;
// })
// }
function get_json() {
fs.readFile('./pills.json', 'utf8', (err, data) => {
if (err) {
console.log(`Error reading file from disk: ${err}`)
} else {
// parse JSON string to JSON object
const pills = JSON.parse(data)
for (const key in pills) {
if (Object.hasOwnProperty.call(pills, key)) {
const element = pills[key];
console.log(key);
}
}
}
})
}
get_json();
// app.post('/scripts',(req,res) => {
// console.log(req.body);
// db_get(function(err,data){
// let jdata = JSON.parse(data["json"]);
// // jdata[]
// })
// })
app.all('*', (req, res) => {
res.status(404).send('<h1>404! Page not</h1>');
});
app.listen(process.env.PORT || 8080, () => console.log("started"));

15
sacrap/index.ejs Normal file
View File

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
hi
</body>
</html>

38
sacrap/index1.js Executable file
View File

@ -0,0 +1,38 @@
const {
response,
request
} = require('express');
const express = require('express');
var vars = require('./vars.js');
const mysql = require("mysql");
const pool = mysql.createPool({
connectionLimit: 100,
host: "localhost",
server: "db",
user: "root",
password: "root",
database: "clicker",
debug: false,
});
pool.query("SELECT * from table_name LIMIT 10", (err, rows) => {
if (err) {
console.log("error occurred during the connection.");
}
// console.log(rows[0]);
});
const {
readFile
} = require('fs').promises;
const app = express();
app.get('/', async (request, response) => {
response.send(await readFile(`html/${dt.page}`, 'utf8'));
});
app.listen(process.env.PORT || 3000, () => console.log("started"));

1
sacrap/pills copy.json Normal file

File diff suppressed because one or more lines are too long

2
sacrap/vars.js Normal file
View File

@ -0,0 +1,2 @@
exports.page = "i2.html";
exports.d = "as";

14
views/edit.ejs Normal file
View File

@ -0,0 +1,14 @@
<style>
*{
text-align: center;
}
</style>
<form action="/scripts" method="post">
<input type="hidden" name="name" value="<%= name %>">
<input type="text" name="pill" value="<%= pill %>">
<input type="text" name="date" value="<%= date %>">
<input type="text" name="comment" value="<%= comment %>">
<input type="hidden" name="func" value="edit">
<input type="submit" name="mode" value="save">
<input type="submit" name="mode" value="cancel">
</form>

44
views/inputs.ejs Normal file
View File

@ -0,0 +1,44 @@
<style>
.pill{
border: 1px black solid;
display: flex;
padding-inline: 3px;
}
.pill input{
margin: 0px;
/* padding: 20px; */
}
.pill label{
padding-inline: 4px;
padding-block: 1px;
}
.pills{
list-style-type: none;
display: flex;
}
.f1{
display: flex;
justify-content: space-evenly;
margin-block: 15px;
}
input{
text-align: center;
}
</style>
<form class="f1" action="/scripts" method="post">
<input type="submit" value="add">
<ul class="pills">
<%
let pills = ["4/2","4/1","4","2","2/1","1","0"];
let i=0;
pills.forEach(el => {
%>
<li class="pill"><label for="<%= i %>"><%= el %></label> <input id="<%= i %>" type="radio" name="pill" value="<%= el %>"></li>
<%
i++;
});
%>
</ul>
<input type="text" name="comment" value="-" placeholder="comment">
<input type="hidden" name="func" value="save">
</form>

73
views/user.ejs Normal file
View File

@ -0,0 +1,73 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.pill-el{
display: flex;
justify-content: space-between;
}
.pill-date,.pill-pill{
display: flex;
width: 150px;
justify-content: space-between;
}
body{
margin-inline: 50px;
}
ul{
padding: 0px;
margin: 0px;
}
</style>
</head>
<body>
<%- include("inputs") -%>
<ul>
<% jdata = data %>
<% for (const key in jdata) {
if (jdata.hasOwnProperty.call(jdata, key)) {
const element = jdata[key];
if(element["pill"]!=""&&element["pill"]!=null&&element["pill"]!=" "&&element["pill"]!="undefined"){
// pills.push(element["pill"]);
// ids.push(key);
// console.log(key);
%>
<li class="pill-el" id="pill-<%= key %>">
<div class="pill-date" id="pill-<%= key %>-date">
<form action="/scripts" method="post">
<input type="submit" value="del">
<input type="hidden" name="func" value="del">
<input type="hidden" name="pill" value="<%= key %>">
</form>
<%= element["date"] %>
</div>
<div class="pill-comment" id="pill-<%= key %>-comment">
<%= element["comment"] %>
</div>
<div class="pill-pill" id="pill-<%= key %>-pill">
<%= element["pill"] %>
<form action="/edit" method="post">
<input type="submit" value="edit">
<input type="hidden" name="func" value="edit">
<input type="hidden" name="name" value="<%= key %>">
<input type="hidden" name="date" value="<%= element["date"] %>">
<input type="hidden" name="pill" value="<%= element["pill"] %>">
<input type="hidden" name="comment" value="<%= element["comment"] %>">
</form>
</div>
</li>
<%
}
}
} %>
</ul>
</body>
</html>