diff --git a/.gitignore b/.gitignore index 8fb962b..43cb739 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +.env +package-lock.json + # ---> CodeIgniter */config/development */logs/log-*.php @@ -18,3 +21,5 @@ application/logs/* !application/logs/.htaccess /vendor/ +#node_modules +/node_modules \ No newline at end of file diff --git a/README.md b/README.md index 6c05800..7277f09 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,7 @@ # vps +#실행 전 설치사항 +npm install express ejs body-parser express-session passport mysql2 uuid bcrypt path express-ejs-layouts + +#실행 +npm run app.js \ No newline at end of file diff --git a/app.js b/app.js new file mode 100644 index 0000000..b3c4c75 --- /dev/null +++ b/app.js @@ -0,0 +1,38 @@ +const path = require('path'); // path 모듈 불러오기 +const dotenv = require('dotenv'); +const passport = require('passport'); +const express = require('express'); +const session = require('express-session'); +const bodyParser = require('body-parser'); + +dotenv.config(); +const app = express(); +// 기본페이지 +app.set(path.join(__dirname, 'public')); +app.set('layout', 'common/layout'); +app.set('layout extractScripts', true); +app.set('view engine', 'ejs'); +app.set('views', path.join(__dirname, 'views')); + +//Layout 설정 +app.use(require('express-ejs-layouts')); +// ... Passport 설정 및 기타 설정 +app.use(session({ secret: process.env.SESSION_SECRET, resave: false, saveUninitialized: false })) + .use(passport.initialize()) + .use(passport.session()); +// Body Parser 미들웨어 설정 +app.use(bodyParser.urlencoded({ extended: false })) + .use(bodyParser.json()); +// 정적 파일 미들웨어 설정 +app.use(express.static(path.join(__dirname, 'public'))); +// 라우팅 설정 +app.use('/', require('./routes/index')) + .use('/user', require('./routes/users')) + .use('/auth', require('./routes/auths')) + .use('/proxmox/node', require('./routes/proxmox/nodes')); + +// 서버 실행 +const PORT = process.env.PORT || 3000; +app.listen(PORT, () => { + console.log(`Server is running on port ${PORT}`); +}); \ No newline at end of file diff --git a/config/db.js b/config/db.js new file mode 100644 index 0000000..cfb4da5 --- /dev/null +++ b/config/db.js @@ -0,0 +1,31 @@ +// config/db.js +const mysql = require('mysql2/promise'); +require('dotenv').config(); + +class Database { + constructor() { + this.pool = mysql.createPool({ + host: process.env.DB_HOST, + user: process.env.DB_USER, + password: process.env.DB_PASSWORD, + database: process.env.DB_DATABASE, + waitForConnections: true, + connectionLimit: 10, + queueLimit: 0, + }); + } + + async execute(query, values) { + const connection = await this.pool.getConnection(); + try { + const [rows] = await connection.execute(query, values); + return rows; + } catch (error) { + throw error; + } finally { + connection.release(); + } + } +} + +module.exports = Database; \ No newline at end of file diff --git a/controllers/authController.js b/controllers/authController.js new file mode 100644 index 0000000..5f5c561 --- /dev/null +++ b/controllers/authController.js @@ -0,0 +1,21 @@ +// controllers/authController.js +const express = require('express'); +const AuthModel = require('../models/authModel'); +const router = express.Router(); + +class AuthController { + async register(req, res) { + const { username, password } = req.body; + await AuthModel.addAuth(username, password); + res.send('사용자 등록 완료'); + } + + // 로그인 및 로그아웃 등 다른 인증 관련 로직 추가 가능 +} + +const authController = new AuthController(); + +router.post('/register', authController.register.bind(authController)); +// 다른 인증 관련 라우팅 추가 가능 + +module.exports = router; diff --git a/controllers/proxmoxController.js b/controllers/proxmoxController.js new file mode 100644 index 0000000..5201862 --- /dev/null +++ b/controllers/proxmoxController.js @@ -0,0 +1,21 @@ +// controllers/proxmoxController.js +const express = require('express'); +const router = express.Router(); + +class ProxmoxController { + constructor() { + // Constructor logic if needed + } + + getProxmoxData(req, res) { + // Proxmox 관련 컨트롤러 로직 + } + + // 다른 Proxmox API 관련 라우팅 및 로직 추가 가능 +} + +const proxmoxController = new ProxmoxController(); + +router.get('/', proxmoxController.getProxmoxData.bind(proxmoxController)); + +module.exports = router; diff --git a/controllers/userController.js b/controllers/userController.js new file mode 100644 index 0000000..a47b686 --- /dev/null +++ b/controllers/userController.js @@ -0,0 +1,16 @@ +// controllers/userController.js +const Model = require('../models/userModel'); + +class UserController { + async getUsers() { + var users = []; + try { + users = await Model.getUsers(); + } catch (error) { + console.error(error); + } + return users; + } + + // 다른 CURD 기능에 대한 컨트롤러 메소드 구현 +} diff --git a/entities/authEntity.js b/entities/authEntity.js new file mode 100644 index 0000000..a48149f --- /dev/null +++ b/entities/authEntity.js @@ -0,0 +1,12 @@ +// entities/Auth.js +class AuthEntity { + constructor(uid, id, password, role, status) { + this.uid = uid; + this.id = id; + this.password = password; + this.role = role; + this.status = status; + } +} + +module.exports = AuthEntity; diff --git a/entities/userEntity.js b/entities/userEntity.js new file mode 100644 index 0000000..a8051d7 --- /dev/null +++ b/entities/userEntity.js @@ -0,0 +1,13 @@ +// entities/User.js +class UserEntity { + constructor(uid,auth_uid, name, email, address, phone) { + this.uid = uid; + this.auth_uid = auth_uid; + this.name = name; + this.email = email; + this.address = address; + this.phone = phone; + } +} + +module.exports = UserEntity; diff --git a/models/authModel.js b/models/authModel.js new file mode 100644 index 0000000..ec7c84b --- /dev/null +++ b/models/authModel.js @@ -0,0 +1,30 @@ +// models/authModel.js +const Database = require('../config/db'); +const AuthEntity = require('../entities/authEntity'); +const { v4: uuidv4 } = require('uuid'); +const bcrypt = require('bcrypt'); + +class AuthModel { + constructor() { + this.db = new Database(); + } + + async addAuth(id, password, role, status) { + const uid = uuidv4(); + const hashedPassword = await bcrypt.hash(password, 10); + const auth = new AuthEntity(uid, id, hashedPassword, role, status); + const query = 'INSERT INTO auth (uid, id, password, role, status) VALUES (?, ?, ?, ?, ?)'; + return await this.db.execute(query, [auth.uid, auth.id, auth.password, auth.role, auth.status]); + } + + async getAuthById(id, password) { + const hashedPassword = await bcrypt.hash(password, 10); + const query = 'SELECT * FROM auth WHERE id = ? AND password = ? AND statu = ? '; + const auths = await this.db.execute(query, [id,hashedPassword,'use']); + return auths[0]; + } + + // 다른 인증 관련 메소드 추가 가능 +} + +module.exports = new AuthModel(); diff --git a/models/userModel.js b/models/userModel.js new file mode 100644 index 0000000..a326d7d --- /dev/null +++ b/models/userModel.js @@ -0,0 +1,37 @@ +// models/userModel.js +const Database = require('../config/db'); +const UserEntity = require('../entities/userEntity'); + +class UserModel { + constructor() { + this.db = new Database(); + } + + async getUsers() { + const query = 'SELECT * FROM users'; + return await this.db.execute(query); + } + + async getUserById(uid) { + const query = 'SELECT * FROM users WHERE uid = ?'; + const users = await this.db.execute(query, [uid]); + return users[0]; + } + + async addUser(name, email) { + const query = 'INSERT INTO users (name, email) VALUES (?, ?)'; + return await this.db.execute(query, [name, email]); + } + + async updateUser(uid, name, email) { + const query = 'UPDATE users SET name = ?, email = ? WHERE id = ?'; + return await this.db.execute(query, [name, email, uid]); + } + + async deleteUser(uid) { + const query = 'DELETE FROM users WHERE id = ?'; + return await this.db.execute(query, [uid]); + } +} + +module.exports = new UserModel(); diff --git a/package.json b/package.json new file mode 100644 index 0000000..b07dfb9 --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "dependencies": { + "bcrypt": "^5.1.1", + "body-parser": "^1.20.2", + "dotenv": "^16.3.1", + "ejs": "^3.1.9", + "express": "^4.18.2", + "express-ejs-layouts": "^2.5.1", + "express-session": "^1.17.3", + "mysql2": "^3.6.5", + "passport": "^0.7.0", + "path": "^0.12.7", + "uuid": "^9.0.1" + } +} diff --git a/public/css/user.css b/public/css/user.css new file mode 100644 index 0000000..c0cd55d --- /dev/null +++ b/public/css/user.css @@ -0,0 +1,12 @@ +body { + font-family: Arial, sans-serif; +} + +.container { + max-width: 800px; + margin: 0 auto; +} + +.btn-margin { + margin-right: 5px; +} \ No newline at end of file diff --git a/routes/auths.js b/routes/auths.js new file mode 100644 index 0000000..d8d82d1 --- /dev/null +++ b/routes/auths.js @@ -0,0 +1,20 @@ +// routes/Auths.js + +const express = require('express'); +const router = express.Router(); + +router.get('/', (req, res) => { + res.render('index', { title: 'Auth List', body: '...' }); // 유저 리스트 페이지 +}); + +router.get('/add', (req, res) => { + res.render('addAuth', { title: 'Add Auth', body: '...' }); // 사용자 추가 페이지 +}); + +router.get('/edit/:id', (req, res) => { + res.render('editAuth', { title: 'Edit Auth', body: '...', Auth: { id: req.params.id } }); // 사용자 수정 페이지 +}); + +// 나머지 CRUD 라우팅... + +module.exports = router; diff --git a/routes/index.js b/routes/index.js new file mode 100644 index 0000000..6d22dae --- /dev/null +++ b/routes/index.js @@ -0,0 +1,10 @@ +// routes/index.js + +const express = require('express'); +const router = express.Router(); + +router.get('/', (req, res) => { + res.render('index',{title: "HOME"}); +}); + +module.exports = router; diff --git a/routes/proxmox/nodes.js b/routes/proxmox/nodes.js new file mode 100644 index 0000000..23a95b8 --- /dev/null +++ b/routes/proxmox/nodes.js @@ -0,0 +1,13 @@ +// routes/Nodes.js + +const express = require('express'); +const router = express.Router(); + +router.get('/proxmox/node', (req, res) => { + res.render('index', { title: 'Node List', body: '...' }); // 유저 리스트 페이지 +}); + + +// 나머지 CRUD 라우팅... + +module.exports = router; diff --git a/routes/users.js b/routes/users.js new file mode 100644 index 0000000..ad3495c --- /dev/null +++ b/routes/users.js @@ -0,0 +1,27 @@ +// routes/users.js + +const express = require('express'); +const router = express.Router(); +// 컨트롤러를 받아오는 부분 +const Control = require("../controllers/userController"); + +// 유저 리스트 페이지 +router.get('/', (req, res, next) => { + res.render('user/index', { title: 'User List', users: Control.getUsers}); +}); + +// 사용자 추가 페이지 +router.get('/add', (req, res, next) => { + res.render('addUser', { title: 'Add User', user: '...' }); +}); +router.post('/add', (req, res, next) => { + res.render('addUser', { title: 'Add User', user: '...' }); +}); + +// 사용자 수정 페이지 +router.get('/edit/:id', (req, res, next) => { + res.render('editUser', { title: 'Edit User', user: { id: req.params.id } }); +}); + +// 나머지 CRUD 라우팅... +module.exports = router; diff --git a/views/common/layout.ejs b/views/common/layout.ejs new file mode 100644 index 0000000..57b5978 --- /dev/null +++ b/views/common/layout.ejs @@ -0,0 +1,18 @@ + + + + + + + VPS Site + + + + + +
+ <%- body %> +
+ + + \ No newline at end of file diff --git a/views/index.ejs b/views/index.ejs new file mode 100644 index 0000000..33789d7 --- /dev/null +++ b/views/index.ejs @@ -0,0 +1,5 @@ + +

Action List

+Auth List +User List +Node List \ No newline at end of file diff --git a/views/user/add.ejs b/views/user/add.ejs new file mode 100644 index 0000000..7c261a7 --- /dev/null +++ b/views/user/add.ejs @@ -0,0 +1,21 @@ + +

Add User

+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
\ No newline at end of file diff --git a/views/user/edit.ejs b/views/user/edit.ejs new file mode 100644 index 0000000..d069acb --- /dev/null +++ b/views/user/edit.ejs @@ -0,0 +1,21 @@ + +

Edit User

+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
diff --git a/views/user/index.ejs b/views/user/index.ejs new file mode 100644 index 0000000..d08010d --- /dev/null +++ b/views/user/index.ejs @@ -0,0 +1,32 @@ + +

User List

+ + + + + + + + + + + + + + <% users.forEach(user => { %> + + + + + + + + + + <% }); %> + +
UIDAuthNameEmailAddressPhoneAction
<%= user.uid %><%= user.auth_uid %><%= user.name %><%= user.email %><%= user.address %><%= user.phone %> + Edit + Delete +
+Add User