diff --git a/README.md b/README.md index 710077f..b0338ab 100644 --- a/README.md +++ b/README.md @@ -32,11 +32,22 @@ A simple blogging platform built with Next.js, shadcn/ui and Tailwind CSS. **Copy example .env:** ```bash + cd .. # if you are currently in the server/ directory cp .env.example .env nano .env # edit if you desire ``` -3. **Start server** +3. **Setup backend** + + Next, you must perform the initial setup of the database, which can be done with our easy setup tool. To start, ensure you `cd` into the `server/` folder. + + Then, simply execute the below command and follow the prompts: + + ```bash + node setup.js # bun is not suggested + ``` + +4. **Start server** Starting a dev server with Node or Bun requires two terminals. One will output the log files for the backend, and the other will output the log files for the frontend. @@ -86,3 +97,5 @@ A simple blogging platform built with Next.js, shadcn/ui and Tailwind CSS. - [ ] Add a post list w/ management options on `/admin/posts` - [ ] Add a user list w/ management options in `/admin/users` - [ ] Better error handling in `server/index.js` +- [ ] Setup strings file for `server/setup.js` +- [ ] @lucmsilva - Implement `pt-BR` Translation diff --git a/server/setup.js b/server/setup.js new file mode 100644 index 0000000..7c74ddc --- /dev/null +++ b/server/setup.js @@ -0,0 +1,122 @@ +import { Database } from 'sqlite3'; +import readline from 'readline'; +import fs from 'fs'; +import figlet from 'figlet'; + +const version = "v1.0.0"; + +const tables = [ + { + name: 'users', + schema: ` + CREATE TABLE IF NOT EXISTS users ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + username TEXT, + email TEXT, + password TEXT, + key TEXT + ); + ` + }, + { + name: 'posts', + schema: ` + CREATE TABLE IF NOT EXISTS posts ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + title TEXT, + description TEXT, + category TEXT, + slug TEXT, + content TEXT, + date TEXT + ); + ` + }, + { + name: 'categories', + schema: ` + CREATE TABLE IF NOT EXISTS categories ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT, + description TEXT, + slug TEXT + ); + ` + } +]; + +figlet('BlogPop', (err, data) => { + if (err) { + console.log('BLOGPOP SETUP | ' + version + "\n"); + console.dir(err); + return; + } + console.log(data); + console.log("SETUP | VERSION: " + version + "\n"); + + if (fs.existsSync('./db.sqlite')) { + console.log("[!] You've already run the setup before!"); + process.exit(0); + } + + const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout + }); + + console.log("┌───────────────────────────────────────────────────────────────────────────────────────────┐"); + console.log("│ Let's start by setting up an admin user for you. Please enter your desired details below. │"); + console.log("├────────────────┬──────────────────────────────────────────────────────────────────────────┘"); + + rl.question('│ ADMIN USERNAME │ ', (username) => { + if (!username) { + console.log("└────────────────┴──────────────────────────────────────────────────────────────────────────┘\n"); + console.log('[!] The username field cannot be blank.'); + rl.close(); + process.exit(1); + } + console.log("├────────────────┼──────────────────────────────────────────────────────────────────────────┘"); + rl.question('│ ADMIN EMAIL │ ', (email) => { + if (!email) { + console.log("└────────────────┴──────────────────────────────────────────────────────────────────────────┘\n"); + console.log('[!] The email field cannot be blank.'); + rl.close(); + process.exit(1); + } + console.log("├────────────────┼──────────────────────────────────────────────────────────────────────────┘"); + rl.question('│ ADMIN PASSWORD │ ', (password) => { + if (!password) { + console.log("└────────────────┴──────────────────────────────────────────────────────────────────────────┘\n"); + console.log('[!] The password field cannot be blank.'); + rl.close(); + process.exit(1); + } + console.log("└────────────────┴──────────────────────────────────────────────────────────────────────────┘\n"); + console.log("[TASK] Creating tables...\n"); + const db = new Database('./db.sqlite'); + + for (const table of tables) { + db.exec(table.schema); + console.log(`[✓] Created table ${table.name} successfully.`); + } + console.log('\n[✓ TASK] Tables created.\n'); + + console.log("[TASK] Creating admin user..."); + const insertStmt = db.prepare(`INSERT INTO users (username, email, password) VALUES (?, ?, ?)`); + insertStmt.run(username, email, password); + insertStmt.finalize(); + console.log('[✓ TASK] Admin user created.\n'); + + console.log("┌────────────────────────────────────────────────────┐"); + console.log("│ ✓ SETUP COMPLETE │"); + console.log("├──────────────┬─────────────────────────────────────┤"); + console.log("│ START SERVER │ $ node index.js │"); + console.log("└──────────────┴─────────────────────────────────────┘") + + db.close(); + rl.close(); + }); + }); + }); +}); +