Skip to content
This repository was archived by the owner on Mar 2, 2021. It is now read-only.

Commit 4b0409e

Browse files
authored
Merge pull request #28 from internet4000/feature/clearer
WIP clearer attempt
2 parents 2ef0b70 + b000b09 commit 4b0409e

File tree

20 files changed

+1073
-1747
lines changed

20 files changed

+1073
-1747
lines changed

.runtimeconfig.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"stripe": {
3+
"public_key": "pk_test_McG33qKTvlMHHSRz8kVkwo2R",
4+
"private_key": "sk_test_u5iqxnRWtbL7ZcMslcgbA1XY",
5+
"production_public_key": "pk_test_McG33qKTvlMHHSRz8kVkwo2R",
6+
"production_private_key": "sk_test_u5iqxnRWtbL7ZcMslcgbA1XY"
7+
}
8+
}

README.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,11 @@ To install, you'll need node.js and git. Then run:
144144
```
145145
git clone git@github.com:internet4000/radio4000-api.git
146146
cd radio4000-api
147-
yarn; cd src; yarn; cd ..
147+
yarn
148+
yarn start
149+
open http://localhost:4001
148150
```
149151

150-
To start a local development server, run
151-
152-
- `yarn start`
153-
- `firebase serve --only hosting,functions`
154-
155152
You can test it using `yarn test` which runs `ava` on the `test` folder.
156153

157154
## Deployment
@@ -163,9 +160,9 @@ Before you can deploy either the Firebase security rules or the Node.js API, mak
163160

164161
**Deploying rules**
165162

166-
1. Get permissions to the Firebase project `firebase-radio4000`
167-
2. Run `yarn deploy-rules`
168-
3. Now live at https://radio4000.firebaseio.com
163+
-. Get permissions to the Firebase project `firebase-radio4000`
164+
- `yarn deploy-rules` (staging)
165+
- `yarn deploy-rules-production` (production)
169166

170167
**Deploying the Node.js API**
171168

firebase.json

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
{
2-
"database": {
3-
"rules": "database.rules.json"
4-
},
5-
"hosting": {
6-
"public": "src/public",
7-
"rewrites": [
8-
{
9-
"source": "**/**",
10-
"function": "api"
11-
}
12-
]
13-
},
14-
"functions": {
15-
"source": "src"
16-
}
2+
"database": {
3+
"rules": "database.rules.json"
4+
},
5+
"hosting": {
6+
"public": "public",
7+
"ignore": [
8+
"firebase.json",
9+
"**/.*",
10+
"**/node_modules/**"
11+
],
12+
"rewrites": [
13+
{
14+
"source": "**",
15+
"function": "api"
16+
}
17+
]
18+
},
19+
"functions": {
20+
"source": "."
21+
}
1722
}

index.html

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
const functions = require('firebase-functions')
2-
const app = require('./app.js')
2+
const app = require('./src/app')
33

44
exports.api = functions.https.onRequest(app)

package.json

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
{
2-
"name": "radio4000-embed-api",
2+
"name": "radio4000-api",
33
"homepage": "https://github.com/internet4000/radio4000-api",
4-
"main": "src/index.js",
4+
"main": "index.js",
5+
"private": true,
56
"license": "MIT",
67
"scripts": {
7-
"postinstall": "cd src; $npm_execpath install",
8-
"start": "firebase serve -p 4001",
8+
"start": "firebase serve --only hosting,functions --port 4001",
99
"test": "ava",
1010
"deploy-api": "firebase use staging; firebase deploy --only functions",
1111
"deploy-api-production": "firebase use production; firebase deploy --only functions",
1212
"deploy-rules": "firebase use staging; firebase deploy --only database",
1313
"deploy-rules-production": "firebase use production; firebase deploy --only database"
1414
},
1515
"dependencies": {
16-
"nodemon": "^1.11.0"
16+
"body-parser": "^1.17.2",
17+
"cors": "^2.8.4",
18+
"express": "^4.15.3",
19+
"firebase-admin": "^5.2.1",
20+
"firebase-functions": "^0.6.3",
21+
"got": "^6.7.1",
22+
"stripe": "^4.24.0"
1723
},
1824
"devDependencies": {
1925
"ava": "^0.21.0"

public/404.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<meta charset="utf-8">
4+
<title>Page Not Found</title>
5+
<h1>Page Not Found</h1>

src/app.js

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
require('dotenv').config()
2-
31
const express = require('express')
4-
const admin = require('firebase-admin')
5-
const functions = require('firebase-functions')
62
const bodyParser = require('body-parser')
73
const cors = require('cors')
8-
const config = require('./utils/config')
4+
const admin = require('firebase-admin')
5+
const functions = require('firebase-functions')
6+
7+
const config = require('./config')
98
const billings = require('./billings')
109
const embed = require('./embed')
1110
const oembed = require('./oembed')
1211

1312

14-
// Start Express server
13+
/* Start Express server */
1514
const app = express()
1615
app.use(cors())
1716
app.use(bodyParser.json())
@@ -36,29 +35,19 @@ app.use(bodyParser.json())
3635
source: https://firebase.google.com/docs/functions/local-emulator
3736
*/
3837

39-
admin.initializeApp(functions.config().firebase);
38+
admin.initializeApp(functions.config().firebase)
4039

4140

42-
// Routes
41+
/* Routes */
4342
app.get('/', function (req, res) {
4443
res.json({
4544
message: 'Welcome to the Radio4000 api',
4645
documentationUrl: 'https://github.com/internet4000/radio4000-api',
47-
databaseUrl: config.databaseURL,
48-
apiUrl: config.apiURL,
49-
billingsUrl: config.apiURL + '/billings',
50-
embedUrl: config.apiURL + '/embed',
51-
oembedUrl: config.apiURL + '/oembed'
46+
databaseUrl: config.databaseURL
5247
})
5348
})
5449
app.use('/billings', billings)
5550
app.use('/embed', embed)
5651
app.use('/oembed', oembed)
5752

58-
59-
// Run server
60-
app.listen(config.port, function () {
61-
console.log(`Radio4000 API running on port ${config.port}`);
62-
})
63-
6453
module.exports = app

src/billings/index.js

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,76 @@
11
const express = require('express')
2-
const stripe = require('stripe')
32
const admin = require('firebase-admin')
43
const functions = require('firebase-functions')
5-
const config = require('../utils/config')
4+
const stripe = require('stripe')
5+
const config = require('../config')
6+
const noEndpoint = require('../utils/no-endpoint')
67

78
const billings = express.Router()
89

9-
const keyPublishable = config.stripePublicKey;
10-
const keySecret = config.stripePrivateKey;
10+
let stripePrivateKey = functions.config().stripe.private_key
11+
let stripePublicKey = functions.config().stripe.public_key
12+
if (process.env.NODE_ENV === 'production') {
13+
stripePrivateKey = functions.config().stripe.production_private_key
14+
stripePublicKey = functions.config().stripe.production_public_key
15+
}
16+
17+
const keySecret = stripePrivateKey
18+
// const keyPublishable = stripePublicKey
1119

1220
const stripeApp = stripe(keySecret)
1321

14-
billings.get('/', function(req, res) {
15-
res.json({
16-
error: 'this endpoint does not exist, check usage on the documentation'
17-
})
18-
});
22+
billings.get('/', function (req, res) {
23+
return noEndpoint(res)
24+
})
1925

2026
billings.post('/', function (req, res) {
2127
const data = req.body
22-
if (!data || !data.stripeCard) return res.sendStatus(400)
28+
if (!data || !data.stripeCard) return res.sendStatus(400)
2329

24-
const amount = 1400;
25-
const { stripeCard, radio4000ChannelId } = data;
30+
const amount = 1400
31+
const { stripeCard, radio4000ChannelId } = data
2632

2733
const newCustomer = {
28-
email: stripeCard.email,
29-
source: stripeCard.id
30-
}
34+
email: stripeCard.email,
35+
source: stripeCard.id
36+
}
3137

3238
console.log('@billings:data', data)
33-
console.log('@billings:newCustomer', newCustomer);
39+
console.log('@billings:newCustomer', newCustomer)
3440

35-
stripeApp.customers.create(newCustomer).then(customer => {
36-
console.log('@customers.create:customer', customer);
41+
stripeApp.customers.create(newCustomer).then(customer => {
42+
console.log('@customers.create:customer', customer)
3743

3844
const charge = {
3945
customer: customer.id,
4046
source: customer.default_source,
4147
amount: 1400,
42-
currency: "eur",
43-
description: "Radio4000 Premium",
48+
currency: 'eur',
49+
description: 'Radio4000 Premium'
4450
}
4551

4652
stripeApp.charges.create(charge).then(answer => {
4753
console.log('@charges.charge:charge', charge)
4854
console.log('@charges.charge:answer', answer)
4955

50-
if(answer.paid) {
51-
var db = admin.database();
52-
var ref = db.ref(`channels/${radio4000ChannelId}`);
56+
if (answer.paid) {
57+
const db = admin.database()
58+
const ref = db.ref(`channels/${radio4000ChannelId}`)
5359

5460
console.log('radio4000ChannelId', radio4000ChannelId)
5561

5662
ref.child('isPremium')
57-
.set(true)
58-
.then(completion => {
59-
60-
res.status(200).json({
61-
message: 'charge sucess && channel.isPremium = true'
62-
})
63-
64-
}).catch(completionError => {
65-
console.log('@firebase:isPremium-c-error', completionError)
66-
res.status(500).json({
67-
message: 'charge error: card charged, but channel not upgraded to premium'
68-
})
69-
})
63+
.set(true)
64+
.then(() => {
65+
res.status(200).json({
66+
message: 'charge sucess && channel.isPremium = true'
67+
})
68+
}).catch(completionError => {
69+
console.log('@firebase:isPremium-c-error', completionError)
70+
res.status(500).json({
71+
message: 'charge error: card charged, but channel not upgraded to premium'
72+
})
73+
})
7074
} else {
7175
// send error response
7276
console.log('answer.paid', answer.paid)
@@ -75,13 +79,13 @@ billings.post('/', function (req, res) {
7579
})
7680
}
7781
}).catch(error => {
78-
console.log('error charges.create', error);
82+
console.log('error charges.create', error)
7983
res.status(500).json({
8084
message: 'charge create error'
8185
})
82-
});
86+
})
8387
}).catch(error => {
84-
console.log('error customers.create', error);
88+
console.log('error customers.create', error)
8589
res.status(500).json({
8690
message: 'customer create error'
8791
})
@@ -112,6 +116,6 @@ billings.post('/', function (req, res) {
112116
name: 'hu@hu.hu',
113117
tokenization_method: null
114118
}
115-
*/
119+
*/
116120

117121
module.exports = billings

src/config.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Global config that respects the node environment
2+
3+
const {NODE_ENV} = process.env
4+
5+
let apiURL, // URL of this application
6+
databaseURL, // Firebase database URL
7+
stripePrivateKey,
8+
stripePublicKey
9+
10+
// CDN URL to the radio4000-player script
11+
const playerScriptURL = 'https://unpkg.com/radio4000-player'
12+
13+
apiURL = 'http://localhost:4001'
14+
databaseURL = 'https://radio4000-staging.firebaseio.com/'
15+
16+
if (NODE_ENV === 'production') {
17+
apiURL = 'https://api.radio4000.com'
18+
databaseURL = 'https://radio4000.firebaseio.com/'
19+
}
20+
21+
module.exports = {
22+
apiURL,
23+
databaseURL,
24+
playerScriptURL
25+
}

0 commit comments

Comments
 (0)