-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackend.js
129 lines (113 loc) · 3.76 KB
/
backend.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const uuid = require('uuid')
const path = require('path')
const express = require('express')
//import firebase things
const initFirebase = require('firebase/app')
const firebaseStorage = require('firebase/storage')
const firebaseFirestore = require('firebase/firestore')
const {getFirestore, Timestamp, FieldValue, GeoPoint, setDoc} = require('firebase/firestore')
const key = require(path.resolve( __dirname, "./key.js"))
const firebaseConfig = {
apiKey: key.MY_KEY,
authDomain: "noisedot-a5ac4.firebaseapp.com",
projectId: "noisedot-a5ac4",
storageBucket: "noisedot-a5ac4.appspot.com",
messagingSenderId: "1090527593409",
appId: "1:1090527593409:web:2ddef0140e9174ce362540",
measurementId: "G-V8W22V84X4"
}
// Initialize Firebase
const firebase = initFirebase.initializeApp(firebaseConfig) // is a firebase app instance
const storage = firebaseStorage.getStorage(firebase) // is a FirebaseStorage Instance
const db = getFirestore(firebase); // should be a FirebaseStorage Instance
const dots = firebaseFirestore.collection(db, 'dots')
// build app
const app = express()
app.use(express.static('public'))
app.use(express.json())
/*
* Load JSON files from firestore on initialization
* this decreases the amount of requests we have to make to the API
* so it should speed up performance client-side at the cost of init speed.
*/
const jsonList = new Map();
async function getJsonList() {
firebaseFirestore.getDocs(dots)
.then(querySnapshot => {
querySnapshot.forEach(doc => {
// doc.data() is never undefined for query doc snapshots
jsonList.set(doc.id, doc.data())
})
})
console.log("initialized!")
}
// initial endpoint. we might change this to the map later.
app.get('/', (_, res) =>
res.sendFile(path.join(__dirname, '/public/index.html'))
)
// map endpoint.
app.get('/map', (_, res) =>
res.sendFile(path.join(__dirname, '/public/map.html'))
)
// audio list endpoint.
app.get('/browse', (_, res) =>
res.sendFile(path.join(__dirname, '/public/browse.html'))
)
// upload page endpoint.
app.get('/upload', (_, res) => {
res.sendFile(path.join(__dirname, '/public/upload.html'))
})
// gets JSON file from firebase db
app.get('/firebaseJSON', (req, res) => {
let id = req.headers.id.replace(/\"/g, "")
res.send(jsonList.get(id))
})
// gets audio file from firebase db
app.get('/firebaseAudio', (req, res) => {
const pathReference = firebaseStorage.ref(storage, `audio/${req.headers.file.replace(/\"/g, "")}`)
firebaseStorage.getDownloadURL(pathReference)
.then(url => {
res.send(url)
})
})
// gets the list of data IDS from firebase db
app.get('/getAudioList', (_, res) => {
const toSend = {}
const ids = []
jsonList.forEach((__, id) => {
ids.push(id)
})
toSend['ids'] = ids
res.send(toSend)
})
//uploads the JSON info to firebase db
app.post('/uploadAudio', (req, res) => {
// get the generated UUID from req
// get the fileType from req
//---- Possible Things for Uploading File ----
/*
var metadata = {
contentType: 'image/jpeg',
};
var uploadTask = storageRef.child('images/mountains.jpg').put(file, metadata);
*/
// OR
/* //'file' comes from the Blob or File API
ref.put(file).then((snapshot) => {
console.log('Uploaded a blob or file!');
});
*/
res.end()
})
//uploads the JSON info to firebase db
app.post('/uploadJSON', (req, res) => {
req.body.timestamp = Timestamp.fromMillis(req.body.timestamp)
console.log(req.body)
let docName = uuid.v4().replace(/-/g, "").substring(0,20)
setDoc(firebaseFirestore.doc(db, "dots", docName), req.body)
res.end()
})
// runs app on both https and http
module.exports = app
getJsonList()
app.listen(3000, () => console.log("listening at port 3000"))