first commit
This commit is contained in:
2
.dockerignore
Normal file
2
.dockerignore
Normal file
@@ -0,0 +1,2 @@
|
||||
node_modules
|
||||
npm-debug.log
|
||||
20
.gitignore
vendored
Normal file
20
.gitignore
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
node_modules/
|
||||
public/src/thumbs
|
||||
public/avatars/
|
||||
# OS generated files #
|
||||
######################
|
||||
.DS_Store
|
||||
.DS_Store?
|
||||
._*
|
||||
.Spotlight-V100
|
||||
.Trashes
|
||||
ehthumbs.db
|
||||
Thumbs.db
|
||||
package-lock.json
|
||||
.env
|
||||
.vscode/
|
||||
settings.json
|
||||
Procfile
|
||||
dump.rdb
|
||||
error.log
|
||||
output.log
|
||||
14
Dockerfile
Normal file
14
Dockerfile
Normal file
@@ -0,0 +1,14 @@
|
||||
FROM node:21.5.0
|
||||
|
||||
RUN mkdir /smarthome
|
||||
WORKDIR /smarthome
|
||||
RUN npm install -g nodemon
|
||||
|
||||
COPY package.json /smarthome
|
||||
RUN npm install
|
||||
|
||||
COPY . /smarthome
|
||||
|
||||
EXPOSE 9095
|
||||
|
||||
CMD ["nodemon", "app"]
|
||||
47
app.js
Normal file
47
app.js
Normal file
@@ -0,0 +1,47 @@
|
||||
const express = require("express");
|
||||
const app = express();
|
||||
const server = require("http").createServer(app);
|
||||
const mongoose = require("mongoose");
|
||||
const configs = require("./configs");
|
||||
const chalk = require("chalk");
|
||||
const helmet = require("helmet");
|
||||
const config = require('./configs');
|
||||
const morgan = require("morgan");
|
||||
|
||||
app.use(express.urlencoded({extended: true}));
|
||||
app.use(express.json());
|
||||
app.use(helmet());
|
||||
|
||||
// Logs
|
||||
app.use(morgan('dev'));
|
||||
|
||||
// Connect Mongoose
|
||||
mongoose.connect(configs.mongodburl, {
|
||||
autoIndex: true
|
||||
}).then(() => {
|
||||
console.log(chalk.cyanBright.bold("Mongodb is live!"));
|
||||
}).catch((error) => {
|
||||
|
||||
console.log("Mongodb error: " + error.message);
|
||||
});
|
||||
|
||||
// Save Route
|
||||
app.use("/api/save", require("./route/save.route"));
|
||||
// Update Route
|
||||
app.use("/api/update", require("./route/update.route"));
|
||||
// Wrong Endpoint
|
||||
app.use("/api/*", (req, res) => {
|
||||
res.status(400).json({error: true, message: "Invalid api end point!"});
|
||||
});
|
||||
// Root public route
|
||||
app.use("*", (req, res) => { // res.sendFile("public/", { root: __dirname });
|
||||
res.sendStatus(400);
|
||||
});
|
||||
// Server Listening
|
||||
server.listen(configs.port, () => {
|
||||
console.log(chalk.yellowBright.bold.red("Listening: " + configs.port));
|
||||
});
|
||||
// Error Handling
|
||||
app.on("eror", (error) => {
|
||||
console.log(error);
|
||||
});
|
||||
38
configs/index.js
Normal file
38
configs/index.js
Normal file
@@ -0,0 +1,38 @@
|
||||
// Settings environments
|
||||
require("dotenv").config();
|
||||
|
||||
const config = {
|
||||
// Site Url
|
||||
baseURL: process.env.BASE_URL,
|
||||
port: process.env.PORT || 3000,
|
||||
// Veridy Token
|
||||
verifyToken: process.env.VERIFY_TOKEN,
|
||||
// If the Remember me option is not selected
|
||||
cookieExpirationTimeRememberTrue: parseInt(process.env.COOKIE_EXP_TIME_REMEMBER_TRUE), // 1 mounth
|
||||
// Remember me option is selected
|
||||
cookieExpirationTimeRememberFalse: parseInt(process.env.COOKIE_EXP_TIME_REMEMBER_FALSE), // 30 minute
|
||||
// Token Settings
|
||||
tookenExpirationTime: parseInt(process.env.TOOKEN_EXP_TIME), // 1 mounth
|
||||
secret: process.env.SECRET,
|
||||
// Crypt Salt Secret
|
||||
cryptoSalt: process.env.CRYPTO_SALT,
|
||||
// Avatar upload settings
|
||||
avatarNameLength: 15,
|
||||
avatarSizeLimit: 250000, // 250 KB
|
||||
avatarFileTypes: /jpeg|jpg|png/,
|
||||
// Mongo Settings
|
||||
//mongodburl: process.env.MONGODB_URI_PRO,
|
||||
mongodburl: process.env.MONGODB_URI,
|
||||
// mongodburl: 'mongodb://mongo:27017/jwt' //server
|
||||
// The Movie DB api key
|
||||
tmdbApiKey: process.env.TMDB_API_KEY,
|
||||
// Flixinfo api default language
|
||||
thmdbDefaultLanguage: "en-US",
|
||||
// Redis Cache değerleri
|
||||
redisCacheLength: parseInt(process.env.REDIS_CACHE_LENGTH) || 100, // server başladığında ilk chachelenecek kayıt miktarı
|
||||
redisTTL: parseInt(process.env.REDIS_TTL) || 86400, // veriler ne kadar süre cachede tutulacak
|
||||
//redisUrl: process.env.REDIS_URL_PRO,
|
||||
redisUrl: process.env.REDIS_URL,
|
||||
};
|
||||
|
||||
module.exports = config;
|
||||
74
controller/save.controller.js
Normal file
74
controller/save.controller.js
Normal file
@@ -0,0 +1,74 @@
|
||||
const configs = require("../configs");
|
||||
const DeviceModel = require("../models/device.model");
|
||||
|
||||
const save = async (
|
||||
token,
|
||||
device_name,
|
||||
device_type,
|
||||
manifactor,
|
||||
serial_number,
|
||||
model,
|
||||
firmware_version,
|
||||
device_ip_address
|
||||
) => {
|
||||
if (token === configs.verifyToken) {
|
||||
try {
|
||||
// Önce kaydı bulalım
|
||||
const existingDevice = await DeviceModel.findOne({
|
||||
device_name,
|
||||
device_type,
|
||||
manifactor,
|
||||
serial_number,
|
||||
model,
|
||||
firmware_version
|
||||
});
|
||||
if (!existingDevice) {
|
||||
// İlk defa kaydet
|
||||
const newDevice = new DeviceModel({
|
||||
device_name,
|
||||
device_type,
|
||||
manifactor,
|
||||
serial_number,
|
||||
model,
|
||||
firmware_version,
|
||||
device_ip_address
|
||||
});
|
||||
const result = await newDevice.save();
|
||||
return "Cihaz kaydedildi.";
|
||||
} else {
|
||||
// Daha önce kaydedilmiş ise sadece ip adresini güncelle
|
||||
const filter = {
|
||||
device_name,
|
||||
device_type,
|
||||
manifactor,
|
||||
serial_number,
|
||||
model,
|
||||
firmware_version
|
||||
};
|
||||
|
||||
const update = {
|
||||
$set: {
|
||||
device_ip_address
|
||||
}
|
||||
};
|
||||
|
||||
const result = await DeviceModel.updateOne(filter, update);
|
||||
|
||||
if (result.nModified === 0) {
|
||||
throw new Error("Güncellenmek istenen kayıt bulunamadı");
|
||||
}
|
||||
|
||||
return "IP Adresi güncellendi";
|
||||
}
|
||||
} catch (error) {
|
||||
// if (error.code === 11000) {
|
||||
// throw new Error("Zaten kaydedilmiş");
|
||||
// }
|
||||
throw new Error("Mongodb'ye kaydedilirken hata oluştu");
|
||||
}
|
||||
} else {
|
||||
throw new Error("Hatalı token");
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = { save };
|
||||
55
docker-compose.yml
Normal file
55
docker-compose.yml
Normal file
@@ -0,0 +1,55 @@
|
||||
version: "3"
|
||||
services:
|
||||
web:
|
||||
build: .
|
||||
volumes:
|
||||
- ./:/smarthome
|
||||
- /smarthome/node_modules
|
||||
ports:
|
||||
- "9095:9095"
|
||||
restart: always
|
||||
links:
|
||||
- redis
|
||||
- mongo
|
||||
tty: true
|
||||
environment:
|
||||
DEBUG_COLORS: "true"
|
||||
TERM: xterm-256color
|
||||
COLORTERM: truecolor
|
||||
networks:
|
||||
- smarthome
|
||||
mongo:
|
||||
container_name: mongo
|
||||
image: mongo:4.2.5
|
||||
restart: always
|
||||
environment:
|
||||
- MONGO_INITDB_ROOT_USERNAME=admin
|
||||
- MONGO_INITDB_ROOT_PASSWORD=anTis**664+..12895
|
||||
ports: ["27017:27017"]
|
||||
networks:
|
||||
- smarthome
|
||||
redis:
|
||||
container_name: redis
|
||||
image: redis:5.0.8
|
||||
restart: always
|
||||
command: ["redis-server", "--bind", "redis", "--port", "6379"]
|
||||
ports: ["6379:6379"]
|
||||
mongo-express:
|
||||
container_name: mongo-express
|
||||
image: mongo-express
|
||||
restart: always
|
||||
environment:
|
||||
- ME_CONFIG_MONGODB_ADMINUSERNAME=admin
|
||||
- ME_CONFIG_MONGODB_ADMINPASSWORD=anTis**664+..12895
|
||||
- ME_CONFIG_MONGODB_SERVER=mongo
|
||||
- ME_CONFIG_MONGODB_AUTH_USERNAME=wisecolt
|
||||
- ME_CONFIG_MONGODB_AUTH_PASSWORD=XQrvBF7y&Jnc&JoAPCVG6%!kM3UY*d*VweW
|
||||
ports:
|
||||
- "8081:8081"
|
||||
depends_on:
|
||||
- mongo
|
||||
networks:
|
||||
- smarthome
|
||||
|
||||
networks:
|
||||
smarthome:
|
||||
34
models/device.model.js
Normal file
34
models/device.model.js
Normal file
@@ -0,0 +1,34 @@
|
||||
const mongoose = require("mongoose");
|
||||
|
||||
const Schema = mongoose.Schema;
|
||||
|
||||
const deviceSchema = new Schema({
|
||||
device_name: {
|
||||
type: String,
|
||||
unique: true
|
||||
},
|
||||
device_type: {
|
||||
type: String
|
||||
},
|
||||
manifactor: {
|
||||
type: String
|
||||
},
|
||||
serial_number: {
|
||||
type: String,
|
||||
unique: true
|
||||
},
|
||||
model: {
|
||||
type: String
|
||||
},
|
||||
firmware_version: {
|
||||
type: String
|
||||
},
|
||||
device_ip_address: {
|
||||
type: String,
|
||||
unique: true
|
||||
}
|
||||
});
|
||||
|
||||
const Device = mongoose.model("Device", deviceSchema);
|
||||
|
||||
module.exports = Device;
|
||||
23
package.json
Normal file
23
package.json
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"create-react-app": "^5.0.1",
|
||||
"dotenv": "^16.4.5",
|
||||
"express": "^4.18.2",
|
||||
"helmet": "^7.1.0",
|
||||
"mongoose": "^8.2.0",
|
||||
"morgan": "^1.10.0",
|
||||
"nodemon": "^3.1.0",
|
||||
"socket.io": "^4.7.4",
|
||||
"chalk": "^4.0.0"
|
||||
},
|
||||
"name": "wisecolt-mancmini",
|
||||
"version": "1.0.0",
|
||||
"main": "app.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"keywords": [],
|
||||
"description": ""
|
||||
}
|
||||
13
route/device.info.route.js
Normal file
13
route/device.info.route.js
Normal file
@@ -0,0 +1,13 @@
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
// const MainController = require("../controller/main.controller");
|
||||
|
||||
router.use(express.urlencoded({extended: false}));
|
||||
router.use(express.json());
|
||||
|
||||
// Save route
|
||||
router.get("/", (req, res) => {
|
||||
res.status(400).json({error: true, message: "Invalid api end point!"});
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
35
route/save.route.js
Normal file
35
route/save.route.js
Normal file
@@ -0,0 +1,35 @@
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
const SaveController = require("../controller/save.controller");
|
||||
|
||||
router.use(express.urlencoded({ extended: false }));
|
||||
router.use(express.json());
|
||||
|
||||
// Save route
|
||||
router.post("/", async (req, res) => {
|
||||
try {
|
||||
// Token Control Middleware
|
||||
const result = await SaveController.save(
|
||||
req.body.token,
|
||||
req.body.device_name,
|
||||
req.body.device_type,
|
||||
req.body.manifactor,
|
||||
req.body.serial_number,
|
||||
req.body.model,
|
||||
req.body.firmware_version,
|
||||
req.body.device_ip_address
|
||||
);
|
||||
|
||||
return res.status(200).json({
|
||||
error: false,
|
||||
result
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(400).json({
|
||||
error: true,
|
||||
result: error.message
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user