commit 6148484b4f6c5a52727b94fbd73128ad24656383 Author: deicidus Date: Tue Aug 15 20:47:46 2023 -0700 First commit diff --git a/index.js b/index.js new file mode 100644 index 0000000..ccfac13 --- /dev/null +++ b/index.js @@ -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 = '

Posts List

'; + + 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 + diff --git a/package.json b/package.json new file mode 100644 index 0000000..2335f6a --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/routes/listPosts b/routes/listPosts new file mode 100755 index 0000000..8bf95bf --- /dev/null +++ b/routes/listPosts @@ -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" \ No newline at end of file diff --git a/routes/post.js b/routes/post.js new file mode 100755 index 0000000..e69de29 diff --git a/routes/register.js b/routes/register.js new file mode 100644 index 0000000..4c12a2b --- /dev/null +++ b/routes/register.js @@ -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 \ No newline at end of file diff --git a/routes/viewPost b/routes/viewPost new file mode 100755 index 0000000..fb10ab1 --- /dev/null +++ b/routes/viewPost @@ -0,0 +1,20 @@ +#!/bin/sh + +# usage: ./viewPost.sh --file= + +# 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 diff --git a/scripts/allow-sudo b/scripts/allow-sudo new file mode 100755 index 0000000..9602647 --- /dev/null +++ b/scripts/allow-sudo @@ -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 \ No newline at end of file diff --git a/scripts/test-register b/scripts/test-register new file mode 100755 index 0000000..cd9e1e4 --- /dev/null +++ b/scripts/test-register @@ -0,0 +1,2 @@ +curl -X POST -H "Content-Type: application/json" -d '{"username": "testuser", "password": "testpassword"}' http://localhost:3000/register + diff --git a/server.js b/server.js new file mode 100644 index 0000000..f53d523 --- /dev/null +++ b/server.js @@ -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}`) +}); \ No newline at end of file