deicidus
1 year ago
commit
6148484b4f
9 changed files with 224 additions and 0 deletions
@ -0,0 +1,72 @@
|
||||
const express = require('express'); |
||||
const router = express.Router(); |
||||
const { exec } = require('child_process'); |
||||
|
||||
let dir = process.argv[2] || '.'; |
||||
|
||||
router.get('/', (req, res) => { |
||||
res.send('Welcome to our minimalist web application!') |
||||
}); |
||||
|
||||
router.get('/api/posts', (req, res) => { |
||||
let page = req.query.page || 1; |
||||
let limit = req.query.limit || 10; |
||||
exec(`./routes/listPosts ${dir} ${page} ${limit}`, (error, stdout, stderr) => { |
||||
if (error) { |
||||
console.log(`error: ${error.message}`); |
||||
return; |
||||
} |
||||
if (stderr) { |
||||
console.log(`stderr: ${stderr}`); |
||||
return; |
||||
} |
||||
let posts = stdout.split('\n').filter(Boolean); |
||||
res.json(posts); |
||||
}); |
||||
}); |
||||
|
||||
router.get('/posts', (req, res) => { |
||||
let page = req.query.page || 1; |
||||
let limit = req.query.limit || 10; |
||||
exec(`./routes/listPosts ${dir} ${page} ${limit}`, (error, stdout, stderr) => { |
||||
if (error) { |
||||
console.log(`error: ${error.message}`); |
||||
return; |
||||
} |
||||
if (stderr) { |
||||
console.log(`stderr: ${stderr}`); |
||||
return; |
||||
} |
||||
let posts = stdout.split('\n').filter(Boolean); |
||||
let html = '<h1>Posts List</h1><ul>'; |
||||
posts.forEach(post => { |
||||
html += `<li><a href="/viewPost?filename=${post}">${post.replace('.yaml', '')}</a></li>`; |
||||
}); |
||||
html += '</ul>'; |
||||
|
||||
res.send(html); |
||||
}); |
||||
}); |
||||
|
||||
router.get('/viewPost', (req, res) => { |
||||
let { filename } = req.query; |
||||
exec(`./routes/viewPost --filename=${filename}`, (error, stdout, stderr) => { |
||||
if (error) { |
||||
console.log(`error: ${error.message}`); |
||||
return; |
||||
} |
||||
if (stderr) { |
||||
console.log(`stderr: ${stderr}`); |
||||
return; |
||||
} |
||||
res.send(stdout.split('\n').filter(Boolean)); |
||||
}); |
||||
}); |
||||
|
||||
/*app.listen(3000, () => { |
||||
console.log('Server is running on port 3000'); |
||||
}); |
||||
*/ |
||||
|
||||
module.exports = router |
||||
|
@ -0,0 +1,18 @@
|
||||
{ |
||||
"name": "ao-mini", |
||||
"version": "1.0.0", |
||||
"description": "A simple and flexible web server that serves YAML files as cards or posts.", |
||||
"main": "index.js", |
||||
"scripts": { |
||||
"test": "test" |
||||
}, |
||||
"author": "", |
||||
"license": "AGPL-3.0-or-later", |
||||
"dependencies": { |
||||
"dotenv": "^16.3.1", |
||||
"express": "^4.18.2", |
||||
"gray-matter": "^4.0.3", |
||||
"multer": "^1.4.5-lts.1", |
||||
"yamljs": "^0.3.0" |
||||
} |
||||
} |
@ -0,0 +1,10 @@
|
||||
#!/bin/sh |
||||
# listPosts.sh |
||||
|
||||
dir=$1 |
||||
page=$2 |
||||
limit=$3 |
||||
|
||||
offset=$(( (page - 1) * limit )) |
||||
|
||||
ls -1 "$dir"/*.yaml | awk -F/ '{print $NF}' | sed 's/\.yaml$//' | tail -n "+$((offset+1))" | head -n "$limit" |
@ -0,0 +1,29 @@
|
||||
const express = require('express'); |
||||
const router = express.Router(); |
||||
const { execSync } = require('child_process'); |
||||
|
||||
router.post('/register', (req, res) => { |
||||
const { username, password } = req.body; |
||||
|
||||
if (!username || !password) { |
||||
return res.status(400).json({ error: 'Username and password are required' }); |
||||
} |
||||
|
||||
execSync(`create-user ${username} ${password}`, (error, stdout, stderr) => { |
||||
if (error) { |
||||
console.log('errer') |
||||
if (error.message.includes('already exists')) { |
||||
// User already exists error handling
|
||||
res.status(409).json({ message: 'Username already exists' }); |
||||
} else { |
||||
// Other error handling
|
||||
console.error(error); |
||||
res.status(500).json({ message: 'An error occurred during registration' }); |
||||
} |
||||
} else { |
||||
res.status(200).json({ message: 'Registration successful' }); |
||||
} |
||||
}); |
||||
}); |
||||
|
||||
module.exports = router |
@ -0,0 +1,20 @@
|
||||
#!/bin/sh |
||||
|
||||
# usage: ./viewPost.sh --file=<filename> |
||||
|
||||
# Parse the arguments |
||||
for arg in "$@" |
||||
do |
||||
case $arg in |
||||
--file=*) |
||||
file="${arg#*=}" |
||||
;; |
||||
esac |
||||
done |
||||
|
||||
# Check if the file exists and is a regular file |
||||
if [ -f "$file" ]; then |
||||
cat "$file" |
||||
else |
||||
echo "File does not exist" |
||||
fi |
@ -0,0 +1,54 @@
|
||||
#!/bin/sh |
||||
|
||||
# Warning if run with sudo |
||||
if [ "$EUID" -eq 0 ]; then |
||||
echo "Please do not run this script with sudo. Exiting." |
||||
exit 1 |
||||
fi |
||||
|
||||
# Command or file path provided as an argument |
||||
COMMAND_OR_FILE="$1" |
||||
|
||||
# Get the specified username or default to the current user |
||||
USERNAME="${2:-$(whoami)}" |
||||
|
||||
# Temporary file for sudoers |
||||
TMP_FILE="/tmp/sudoers.tmp" |
||||
|
||||
# Determine if the input is a file or command name |
||||
if [ -f "$COMMAND_OR_FILE" ]; then |
||||
# Convert to absolute path if it's a file |
||||
PATH_TO_ALLOW="$(realpath "$COMMAND_OR_FILE")" |
||||
else |
||||
# Use 'which' to find the command path if it's a command name |
||||
PATH_TO_ALLOW="$(which "$COMMAND_OR_FILE")" |
||||
fi |
||||
|
||||
# Check if the path was determined |
||||
if [ -z "$PATH_TO_ALLOW" ]; then |
||||
echo "File or command not found." |
||||
exit 1 |
||||
fi |
||||
|
||||
# Make a temporary copy of the sudoers file using sudo |
||||
sudo cp /etc/sudoers $TMP_FILE |
||||
|
||||
# Check if the entry already exists |
||||
if ! sudo grep -q "$USERNAME ALL=(ALL) NOPASSWD: $PATH_TO_ALLOW" $TMP_FILE; then |
||||
# Add the new rule if it doesn't exist |
||||
echo "$USERNAME ALL=(ALL) NOPASSWD: $PATH_TO_ALLOW" | sudo tee -a $TMP_FILE > /dev/null |
||||
fi |
||||
|
||||
# Validate the new sudoers file using sudo |
||||
sudo visudo -cf $TMP_FILE |
||||
|
||||
# If validation succeeds, overwrite the sudoers file using sudo |
||||
if [ $? -eq 0 ]; then |
||||
sudo cp $TMP_FILE /etc/sudoers |
||||
echo "Sudoers file updated successfully." |
||||
else |
||||
echo "Error in sudoers file. Not updated." |
||||
fi |
||||
|
||||
# Remove the temporary file using sudo |
||||
sudo rm -f $TMP_FILE |
@ -0,0 +1,2 @@
|
||||
curl -X POST -H "Content-Type: application/json" -d '{"username": "testuser", "password": "testpassword"}' http://localhost:3000/register |
||||
|
@ -0,0 +1,19 @@
|
||||
const express = require('express') |
||||
const app = express() |
||||
const port = process.env.PORT || 3000 |
||||
|
||||
app.use(express.json()) |
||||
|
||||
const os = require('os') |
||||
console.log('Node.js is running as:', os.userInfo().username) |
||||
|
||||
// Import routes here to build our custom web server instance
|
||||
indexRoute = require('./index') |
||||
app.use('/', indexRoute) |
||||
|
||||
registerRoute = require('./routes/register') |
||||
app.use('/', registerRoute) |
||||
|
||||
app.listen(port, () => { |
||||
console.log(`Server is running on http://localhost:${port}`) |
||||
}); |
Loading…
Reference in new issue