forked from Ecoland-C624-PS008/ecoland-backend-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
108 lines (89 loc) · 2.91 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import express from "express";
import cors from "cors";
import session from "express-session";
import dotenv from "dotenv";
import { Storage } from '@google-cloud/storage';
import multer from 'multer';
import path from 'path';
import { fileURLToPath } from 'url';
import db from "./config/db.js";
import SequelizeStore from "connect-session-sequelize";
import UserRoute from "./routes/UserRoute.js";
import LandRoute from "./routes/LandRoute.js";
import AuthRoute from "./routes/AuthRoute.js";
import TransactionRoute from "./routes/TransactionsRoute.js";
import HistoryRoute from "./routes/HistoryRoute.js";
dotenv.config();
const app = express();
// Konfigurasi ES Module untuk mendapatkan __dirname
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Multer setup
const storage = multer.memoryStorage();
const upload = multer({ storage: storage });
const sessionStore = SequelizeStore(session.Store);
const store = new sessionStore({
db: db
});
(async()=>{
await db.sync();
})();
app.use(session({
secret: 'hxaqfgKv172cvNFvzKmLFkVhYPYWNXQc',
resave: false,
saveUninitialized: true,
store: store,
cookie: {
secure: 'auto'
}
}));
app.use(cors({
credentials: true,
origin: 'https://ecoland-frontend.vercel.app'
}));
app.use(express.json());
app.use(UserRoute);
app.use(LandRoute);
app.use(AuthRoute);
app.use(TransactionRoute);
app.use(HistoryRoute);
store.sync();
// Google Cloud Storage setup
const gc = new Storage({
keyFilename: path.join(__dirname, './keys/submission-mgce-zaenalalfian-0-d30d4045eddc.json'),
projectId: 'submission-mgce-zaenalalfian-0',
});
const bucket = gc.bucket('submission-zaenalalfian');
// Fungsi untuk membuat nama file acak
const generateRandomString = (length) => {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
return result;
};
app.post('/upload', upload.single('image'), (req, res) => {
if (!req.file) {
return res.status(400).send('No file uploaded.');
}
const randomFileName = generateRandomString(8) + path.extname(req.file.originalname);
const blob = bucket.file(randomFileName);
const blobStream = blob.createWriteStream({
resumable: false,
});
blobStream.on('error', (err) => {
console.error(err);
res.status(500).send({ message: 'Error uploading file.', error: err });
});
blobStream.on('finish', () => {
const publicUrl = `https://storage.googleapis.com/${bucket.name}/${blob.name}`;
res.status(200).send({ message: 'File uploaded successfully.', url: publicUrl });
});
blobStream.end(req.file.buffer);
});
const PORT = 5000;
const HOST = '0.0.0.0';
app.listen(PORT, HOST, ()=> {
console.log('Server up and running...');
});