Create stuff
This commit is contained in:
commit
7cf3bc2b54
140
.gitignore
vendored
Normal file
140
.gitignore
vendored
Normal file
|
@ -0,0 +1,140 @@
|
||||||
|
# Logs
|
||||||
|
logs
|
||||||
|
*.log
|
||||||
|
npm-debug.log*
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
|
lerna-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
|
||||||
|
|
||||||
|
# Optional REPL history
|
||||||
|
.node_repl_history
|
||||||
|
|
||||||
|
# Output of 'npm pack'
|
||||||
|
*.tgz
|
||||||
|
|
||||||
|
# Yarn Integrity file
|
||||||
|
.yarn-integrity
|
||||||
|
|
||||||
|
# dotenv environment variable files
|
||||||
|
.env
|
||||||
|
.env.*
|
||||||
|
!.env.example
|
||||||
|
|
||||||
|
# parcel-bundler cache (https://parceljs.org/)
|
||||||
|
.cache
|
||||||
|
.parcel-cache
|
||||||
|
|
||||||
|
# Next.js build output
|
||||||
|
.next
|
||||||
|
out
|
||||||
|
|
||||||
|
# Nuxt.js build / generate output
|
||||||
|
.nuxt
|
||||||
|
dist
|
||||||
|
.output
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
# Sveltekit cache directory
|
||||||
|
.svelte-kit/
|
||||||
|
|
||||||
|
# vitepress build output
|
||||||
|
**/.vitepress/dist
|
||||||
|
|
||||||
|
# vitepress cache directory
|
||||||
|
**/.vitepress/cache
|
||||||
|
|
||||||
|
# Docusaurus cache and generated files
|
||||||
|
.docusaurus
|
||||||
|
|
||||||
|
# Serverless directories
|
||||||
|
.serverless/
|
||||||
|
|
||||||
|
# FuseBox cache
|
||||||
|
.fusebox/
|
||||||
|
|
||||||
|
# DynamoDB Local files
|
||||||
|
.dynamodb/
|
||||||
|
|
||||||
|
# Firebase cache directory
|
||||||
|
.firebase/
|
||||||
|
|
||||||
|
# TernJS port file
|
||||||
|
.tern-port
|
||||||
|
|
||||||
|
# Stores VSCode versions used for testing VSCode extensions
|
||||||
|
.vscode-test
|
||||||
|
|
||||||
|
# yarn v3
|
||||||
|
.pnp.*
|
||||||
|
.yarn/*
|
||||||
|
!.yarn/patches
|
||||||
|
!.yarn/plugins
|
||||||
|
!.yarn/releases
|
||||||
|
!.yarn/sdks
|
||||||
|
!.yarn/versions
|
||||||
|
|
||||||
|
# Vite logs files
|
||||||
|
vite.config.js.timestamp-*
|
||||||
|
vite.config.ts.timestamp-*
|
85
index.js
Normal file
85
index.js
Normal file
|
@ -0,0 +1,85 @@
|
||||||
|
require("dotenv").config({quiet:true});
|
||||||
|
const express = require("express");
|
||||||
|
const session = require("express-session");
|
||||||
|
const SQLiteStore = require('connect-sqlite3')(session);
|
||||||
|
const cors = require("cors");
|
||||||
|
const bcrypt = require('bcrypt');
|
||||||
|
|
||||||
|
const DSN = `mysql://${process.env.DB_USER || "root"}:${process.env.DB_PASSWORD || ""}@tcp(${process.env.DB_HOST || "127.0.0.1"}:${process.env.DB_PORT || "3306"})/${process.env.DB_NAME}`;
|
||||||
|
const mariadb = require("mariadb");
|
||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
const db = mariadb.createPool({
|
||||||
|
host: process.env.DB_HOST || "127.0.0.1",
|
||||||
|
port: process.env.DB_PORT || 3306,
|
||||||
|
user: process.env.DB_USER || "root",
|
||||||
|
password: process.env.DB_PASSWORD || null,
|
||||||
|
database: process.env.DB_NAME || "uhppoted",
|
||||||
|
});
|
||||||
|
global.db = db;
|
||||||
|
global.DSN = DSN;
|
||||||
|
global.log = require("./logger.js")
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
const port = process.env.APP_PORT || 8080;
|
||||||
|
|
||||||
|
|
||||||
|
app.use(session({
|
||||||
|
secret: process.env.SESSION_SECRET || "uhppoted-secret-change-me",
|
||||||
|
cookie: { maxAge: 60000 },
|
||||||
|
resave: false,
|
||||||
|
saveUninitialized: false,
|
||||||
|
store: new SQLiteStore({ db: 'sessions.db', dir: './' })
|
||||||
|
}));
|
||||||
|
|
||||||
|
global.hashPassword = async function(password) {
|
||||||
|
const saltRounds = 12;
|
||||||
|
return await bcrypt.hash(password, saltRounds);
|
||||||
|
};
|
||||||
|
|
||||||
|
global.comparePassword = async function(password, hash) {
|
||||||
|
return await bcrypt.compare(password, hash);
|
||||||
|
};
|
||||||
|
|
||||||
|
app.use(cors());
|
||||||
|
app.use(express.json());
|
||||||
|
app.use(express.urlencoded({ extended: true }));
|
||||||
|
app.use(express.static("public"));
|
||||||
|
app.set("view engine", "ejs");
|
||||||
|
app.set("views", "./views");
|
||||||
|
|
||||||
|
const routersDir = path.join(__dirname, 'routes');
|
||||||
|
fs.readdirSync(routersDir).forEach(file => {
|
||||||
|
const router = require(path.join(routersDir, file));
|
||||||
|
if (file.endsWith('.js')) {
|
||||||
|
const route = '/' + file.replace('.js', '');
|
||||||
|
app.use(route, router);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.listen(port, (err) => {
|
||||||
|
if (err) {
|
||||||
|
global.log.error(`Cannot start server: ${err}`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
global.log.info(`Listening on port ${port}`);
|
||||||
|
global.log.debug(`DSN: ${DSN}`);
|
||||||
|
db.getConnection()
|
||||||
|
.then(conn => {
|
||||||
|
return conn.query('SELECT 1 FROM ACL LIMIT 1')
|
||||||
|
.then(() => {
|
||||||
|
global.log.info('Database connection validated with ACL table.');
|
||||||
|
conn.release();
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
global.log.error(`Database ACL validation failed: ${err}`);
|
||||||
|
conn.release();
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
global.log.error(`Database connection failed: ${err}`);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
|
})
|
12
logger.js
Normal file
12
logger.js
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
const colors = require('colors')
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
info: (msg) => console.log(`${colors.cyan(`[${new Date().toISOString()}] INFO:`)} ${msg}`),
|
||||||
|
warn: (msg) => console.log(`${colors.yellow(`[${new Date().toISOString()}] WARN:`)} ${msg}`),
|
||||||
|
error: (msg) => console.log(`${colors.red(`[${new Date().toISOString()}] ERROR:`)} ${msg}`),
|
||||||
|
debug: (msg) => {
|
||||||
|
if (process.env.DEBUG && process.env.DEBUG.toLowerCase() === "true") {
|
||||||
|
console.log(`${colors.magenta(`[${new Date().toISOString()}] DEBUG:`)} ${msg}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
2481
package-lock.json
generated
Normal file
2481
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
24
package.json
Normal file
24
package.json
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
{
|
||||||
|
"name": "uhppoted-db-web",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"keywords": [],
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC",
|
||||||
|
"type": "commonjs",
|
||||||
|
"dependencies": {
|
||||||
|
"bcrypt": "^6.0.0",
|
||||||
|
"colors": "^1.4.0",
|
||||||
|
"connect-sqlite3": "^0.9.16",
|
||||||
|
"cors": "^2.8.5",
|
||||||
|
"dotenv": "^17.2.1",
|
||||||
|
"ejs": "^3.1.10",
|
||||||
|
"express": "^5.1.0",
|
||||||
|
"express-session": "^1.18.2",
|
||||||
|
"mariadb": "^3.4.5"
|
||||||
|
}
|
||||||
|
}
|
28
routes/login.js
Normal file
28
routes/login.js
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
const express = require('express');
|
||||||
|
const db = global.db;
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
// GET /login
|
||||||
|
router.get('/', (req, res) => {
|
||||||
|
if (req.session.user) return res.redirect('/dashboard');
|
||||||
|
res.render('login'); // Assumes you have a 'login' view/template
|
||||||
|
});
|
||||||
|
|
||||||
|
// POST /login
|
||||||
|
router.post('/', (req, res) => {
|
||||||
|
if (req.session.user) return res.redirect('/dashboard');
|
||||||
|
const { username, password } = req.body;
|
||||||
|
if (!username || !password) return res.status(400).render('login', { error: 'Username and password are required.' });
|
||||||
|
db.query('SELECT * FROM users WHERE username = ?', [username]).then(async (user) => {
|
||||||
|
if (!user) return res.status(401).render('login', { error: 'Invalid username or password.' });
|
||||||
|
const match = await global.comparePassword(password, user.passwordHash);
|
||||||
|
if (!match) return res.status(401).render('login', { error: 'Invalid username or password.' });
|
||||||
|
req.session.user = { user };
|
||||||
|
res.redirect('/dashboard');
|
||||||
|
}).catch(err => {
|
||||||
|
global.log.error(`Database error during login: ${err}`);
|
||||||
|
res.status(500).render('login', { error: 'Internal server error.' });
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = router;
|
BIN
sessions.db
Normal file
BIN
sessions.db
Normal file
Binary file not shown.
53
views/login.ejs
Normal file
53
views/login.ejs
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Login</title>
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<style>
|
||||||
|
body { font-family: Arial, sans-serif; background: #f4f4f4; }
|
||||||
|
.login-container {
|
||||||
|
max-width: 350px;
|
||||||
|
margin: 80px auto;
|
||||||
|
padding: 30px;
|
||||||
|
background: #fff;
|
||||||
|
border-radius: 8px;
|
||||||
|
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
|
||||||
|
}
|
||||||
|
.login-container h2 { text-align: center; margin-bottom: 24px; }
|
||||||
|
.form-group { margin-bottom: 18px; }
|
||||||
|
label { display: block; margin-bottom: 6px; }
|
||||||
|
input[type="text"], input[type="password"] {
|
||||||
|
width: 100%; padding: 8px; box-sizing: border-box;
|
||||||
|
border: 1px solid #ccc; border-radius: 4px;
|
||||||
|
}
|
||||||
|
button {
|
||||||
|
width: 100%; padding: 10px;
|
||||||
|
background: #007bff; color: #fff;
|
||||||
|
border: none; border-radius: 4px;
|
||||||
|
font-size: 16px; cursor: pointer;
|
||||||
|
}
|
||||||
|
button:hover { background: #0056b3; }
|
||||||
|
.error { color: #d00; text-align: center; margin-bottom: 12px; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="login-container">
|
||||||
|
<h2>Login</h2>
|
||||||
|
<% if (typeof error !== 'undefined' && error) { %>
|
||||||
|
<div class="error"><%= error %></div>
|
||||||
|
<% } %>
|
||||||
|
<form method="POST" action="/login">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="username">Username</label>
|
||||||
|
<input id="username" name="username" type="text" required autofocus>
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="password">Password</label>
|
||||||
|
<input id="password" name="password" type="password" required>
|
||||||
|
</div>
|
||||||
|
<button type="submit">Login</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in a new issue