fix: env names, something else

This commit is contained in:
Mateusz Pstrucha committed 2021-10-05 11:54:13 +02:00
1 parent 304757862a
commit 744c6af92c
11 files changed
+119 -72

No files matched your search

+13 -13
View File
@@ -220,37 +220,37 @@ Enviroment variables required
### Ports
```ini
portHttp=3010
portWs=3020
HTTP_PORT=3010
WS_PORT=3020
```
### Spotify API
```ini
spotifyClientId=ID
spotifyClientSecret=SECRET
SPOTIFY_CLIENT_ID=ID
SPOTIFY_CLIENT_SECRET=SECRET
```
### Facebok API
```ini
fbAppId=ID
fbSecret=SECRET
FB_APPID=ID
FB_SECRET=SECRET
```
### Mongo DB
```ini
dbHost=IP
dbPort=PORT
dbName=DB
dbAuth=COLL
dbUser=USER
dbPass=PASSWORD
DB_HOST=IP
DB_PORT=PORT
DB_NAME=DB
DB_AUTH=COLL
DB_USER=USER
DB_PASS=PASSWORD
```
### Other
```ini
timeValid=monday
TOP_TIME_VALID=monday
```
+16 -14
View File
@@ -1,5 +1,4 @@
import Jwt from 'jsonwebtoken'
import cache from './cache.js'
import {
getTrack,
@@ -13,6 +12,7 @@ import {
getQuery,
setQuery,
} from './mongo.js'
import redis from './redis.js'
import { byId, byQuery } from './spotify.js'
import Track from './Track.js'
@@ -36,19 +36,16 @@ function broadcast(cats) {
}
const current = {
next: { tid: '6BfbSHE9ytCTF910g3wNdj' },
next: { tid: null },
status: {
tid: '3OcyTN8Nz3bdne5aq9mMR5',
tid: null,
progress: 0,
duration: 29598,
paused: false,
duration: 0,
paused: true,
timestamp: new Date(),
},
previous: {
combo: [
['0VdSlJ7owUK1MuS8Kp7LdE', '2021-09-09T21:41:44.528Z'],
['5svu5mLA4U2kdxPj1tLJ2I', '2021-09-09T21:41:44.528Z'],
],
combo: [],
},
}
@@ -71,21 +68,21 @@ const db = {
get: (id) => {
return new Promise((resolve, reject) => {
// try to find in redis
cache.get(id).then((data) => {
redis.get(id).then((data) => {
if (data) {
resolve(JSON.parse(data))
} else {
// if not found, query db
getTrack(id)
.then((tdata) => {
cache.set(id, JSON.stringify(tdata))
redis.set(id, JSON.stringify(tdata))
resolve(new Track(tdata))
})
.catch((dbErr) => {
// track not found in DB, fetch online
byId(id)
.then((track) => {
cache.set(id, JSON.stringify(track))
redis.set(id, JSON.stringify(track))
putTrack(track)
resolve(track)
})
@@ -128,7 +125,7 @@ const db = {
*/
verifyJwt: (jwt) => {
return new Promise((resolve, reject) => {
Jwt.verify(jwt ?? '', process.env.fbSecret, (err, decoded) => {
Jwt.verify(jwt ?? '', process.env.FB_SECRET, (err, decoded) => {
if (err) return reject(err)
resolve(decoded)
})
@@ -212,7 +209,12 @@ const db = {
},
set status(chunk) {
if (current.status.tid != chunk?.tid) {
console.log(
current.status.tid,
chunk?.tid,
current.status.tid != chunk?.tid && current.status.tid !== null
)
if (current.status.tid != chunk?.tid && current.status.tid !== null) {
// add new [tid, date]
current.previous.combo.unshift([
current.status.tid,
+26 -17
View File
@@ -4,27 +4,27 @@ services:
api:
build: .
restart: always
container_name: api
environment:
portHttp: 3010
dbHost: db
dbPort: 27017
dbName: radio-rolnik
dbAuth: admin
dbUser: root
dbPass: supersecurepassword123
portWs: 3020
CACHE_HOST: cache
HTTP_PORT: 3010
# WS_PORT: 3020
DB_HOST: mongo
DB_PORT: 27017
DB_NAME: radio-rolnik
DB_USER: root
DB_PASS: supersecurepassword123
CACHE_HOST: redis
CACHE_PORT: 6379
ports:
- 3010:3010
- 3020:3020
depends_on:
- db
- mongo
links:
- db
db:
image: mongo:4.4
container_name: db
- mongo
mongo:
image: mongo:5.0
container_name: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
@@ -33,11 +33,20 @@ services:
- ./database:/data/db
ports:
- 27017:27017
cache:
command: --quiet
redis:
image: redis:6.2-alpine
container_name: cache
container_name: redis
restart: always
environment:
ALLOW_EMPTY_PASSWORD: yes
ALLOW_EMPTY_PASSWORD: 'yes'
ports:
- 6379:6379
nginx:
image: nginx:1.21-alpine
container_name: nginx
restart: always
volumes:
- ./nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- 80:80
+7 -7
View File
@@ -15,12 +15,12 @@ import player from './routes/player.js'
dotenv.config()
var app = express(),
www = createServer(app)
http = createServer(app)
app.use(json())
app.use(urlencoded({ extended: false }))
app.use(cookieParser())
app.set('port', process.env.portHttp)
app.set('port', process.env.HTTP_PORT)
app.use('/track', track)
app.use('/search', search)
@@ -28,18 +28,18 @@ app.use('/vote', vote)
app.use('/login', login)
app.use('/player', player)
www.listen(process.env.portHttp)
http.listen(process.env.HTTP_PORT)
www.on('error', function (error) {
http.on('error', function (error) {
if (error.syscall !== 'listen') {
throw error
}
console.info(`Error: port ${process.env.portHttp}: ${error.code}`)
console.info(`Error: port ${process.env.HTTP_PORT}: ${error.code}`)
})
www.on('listening', function () {
console.info(`www: listening on ${process.env.portHttp}`)
http.on('listening', function () {
console.info(`http: listening on ${process.env.HTTP_PORT}`)
})
var i = 0
+13 -6
View File
@@ -8,11 +8,18 @@ import Vote from './models/Vote.js'
import User from './models/User.js'
import Search from './models/Search.js'
const { dbHost, dbPort, dbName, dbAuth, dbUser, dbPass } = process.env
const {
DB_HOST,
DB_PORT,
DB_NAME,
dbAuth = 'admin',
DB_USER,
DB_PASS,
} = process.env
mongoose.connect(`mongodb://${dbHost}:${dbPort}/${dbName}`, {
user: dbUser,
pass: dbPass,
mongoose.connect(`mongodb://${DB_HOST}:${DB_PORT}/${DB_NAME}`, {
user: DB_USER,
pass: DB_PASS,
authSource: dbAuth,
useNewUrlParser: true,
useUnifiedTopology: true,
@@ -26,7 +33,7 @@ database.on('error', (err) => {
})
database.once('open', () => {
console.info(`db: connected`)
console.info(`mongo: connected`)
})
// #### Tracks #### #### #### #### #### #### #### ####
@@ -72,7 +79,7 @@ export { getTrack, putTrack }
* @type Date
*/
const timeValid = (() => {
switch (process.env.timeValid) {
switch (process.env.TOP_TIME_VALID) {
default:
case 'monday':
var monday = new Date()
+28
View File
@@ -0,0 +1,28 @@
server {
listen 80;
location / {
proxy_pass http://host.docker.internal:3000/;
}
location /api/ {
proxy_pass http://api:3010/;
}
location /ws/ {
proxy_pass http://api:3020/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy false;
proxy_read_timeout 86400s;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
View File
File renamed without changes.
+2 -2
View File
@@ -8,8 +8,8 @@ dotenv.config()
const router = express.Router()
const appId = process.env.fbAppId,
secret = process.env.fbSecret
const appId = process.env.FB_APPID,
secret = process.env.FB_SECRET
router.get('/token', ({ query: { code } }, res) => {
if (code === undefined) return res.status(400).end('Facebook code required')
+10 -9
View File
@@ -15,14 +15,14 @@ function update(data) {
db.current.update(data)
}
const playerSecret = 'Bearer ' + process.env.playerSecret
const playerSecret = 'Bearer ' + process.env.PLAYER_SECRET
// set current track
router.put(
'/set/status',
(
{
body: { tid, progress = null, duration = null, paused = null },
body: { tid = null, progress = null, duration = null, paused = null },
headers: { authorization },
},
res
@@ -33,9 +33,9 @@ router.put(
if (authorization != playerSecret)
return res.status(403).end('Token invalid')
if (tid === undefined) return res.status(400).end('"tid" field required')
if (tid.length < Track.length) return res.status(400).end('Invalid tid')
if (tid !== null)
if (typeof tid !== 'string')
return res.status(400).end('tid value invalid')
if (progress !== null)
if (typeof progress !== 'number')
@@ -52,7 +52,7 @@ router.put(
update({
cat: 'status',
content: {
tid,
...(tid !== null && { tid }),
...(progress !== null && { progress }),
...(duration !== null && { duration }),
...(paused !== null && { paused }),
@@ -75,8 +75,6 @@ router.put(
if (tid === undefined) return res.status(400).end('"tid" field required')
if (tid.length < Track.length) return res.status(400).end('Invalid tid')
update({
cat: 'next',
content: { tid },
@@ -96,7 +94,10 @@ router.get('/get/top/', ({ headers: { authorization } }, res) => {
Promise.all(
top.map((tid) => spotifyToYt.trackGet(`spotify:track:${tid}`))
).then((yts) => {
top.map((tid, i) => (yts[i].tid = tid))
top.map((tid, i) => {
yts[i].tid = tid
yts[i].info[0].duration = 10e3
})
res.json(yts)
})
})
+1 -1
View File
@@ -4,7 +4,7 @@ import db from './db.js'
dotenv.config()
const port = process.env.portWs,
const port = process.env.WS_PORT,
wss = new WebSocketServer({ port: port })
console.info('ws: listening on ' + port)
+3 -3
View File
@@ -6,8 +6,8 @@ import Track from './Track.js'
dotenv.config()
const spotify = new SpotifyWebApi({
clientId: process.env.spotifyClientId,
clientSecret: process.env.spotifyClientSecret,
clientId: process.env.SPOTIFY_CLIENT_ID,
clientSecret: process.env.SPOTIFY_CLIENT_SECRET,
})
function grantToken() {
@@ -15,7 +15,7 @@ function grantToken() {
.clientCredentialsGrant()
.then(
function ({ body: { access_token, expires_in } }) {
console.info('spotify: access token granted ' + expires_in)
console.info('spotify: access token granted')
spotify.setAccessToken(access_token)