Skip to content

Commit cae9b2f

Browse files
committed
corrected access to docs #19
added npm cors package changed location of swagger config ro differ from docs url improve access to swagger css added localhost as one of the urls for local testing
1 parent be35d94 commit cae9b2f

File tree

5 files changed

+24
-36
lines changed

5 files changed

+24
-36
lines changed

functions/getter.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ function getApp () {
99
} catch (e){
1010
admin.initializeApp({
1111
credential: admin.credential.cert(serviceAccount),
12-
databaseURL: "https://ottaaproject-realiser-lexicons.firebaseio.com/",
13-
databaseURL: `${process.env.DATABASE_URL}`,
12+
databaseURL: `${process.env.DATABASE_URL}`
1413
}, 'realiser');
1514
return admin.app('realiser')
1615
}

functions/handlers.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const { dbGetter } = require("./getter.js");
22

3-
43
/*
54
async function findART(obj, langRef){
65
const addART = []

functions/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
},
1818
"main": "index.js",
1919
"dependencies": {
20+
"cors": "^2.8.5",
2021
"dotenv": "^16.0.3",
2122
"express": "^4.18.2",
2223
"firebase-admin": "^10.0.2",

functions/routing.js

+18-33
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const express = require('express');
22
const swaggerUi = require('swagger-ui-express');
3-
const swaggerDoc = require('./docs/swagger.config.json')
3+
const swaggerDoc = require('./swagger/swagger.config.json');
4+
const cors = require('cors')
45
const { log, error } = require("firebase-functions/lib/logger");
56

67
const {
@@ -10,42 +11,26 @@ const {
1011
realiseSentence
1112
} = require('./methods.js');
1213

13-
function allowCors(req, res, next){
14-
res.set('Access-Control-Allow-Origin', '*'); //this should be configured for only the apps we want to be able to access the API
15-
16-
if (req.method === 'OPTIONS') {
17-
res.set('Access-Control-Allow-Methods', 'POST');
18-
res.set('Access-Control-Allow-Methods', 'GET');
19-
res.set('Access-Control-Allow-Headers', 'Content-Type');
20-
res.set('Access-Control-Max-Age', '3600');
21-
res.status(204).send('');
22-
} else {
23-
next();
24-
}
25-
}
26-
2714
const app = express()
2815

16+
app.use(cors())
2917
app.use(express.json());
3018
app.use(express.urlencoded({extended: true}));
3119
app.use(express.static(__dirname)); //swagger customCssUrl requires defining an static path on functions (root)
32-
app.use(
33-
'/docs',
34-
allowCors,
35-
swaggerUi.serve,
36-
swaggerUi.setup(swaggerDoc, {
37-
customCssUrl: '/assets/swagger.css',
38-
customSiteTitle: 'OTTAA Realiser Docs',
39-
customfavIcon: '/assets/logo.ico',
40-
swaggerOptions: {
41-
supportedSubmitMethods: [] //to disable the "Try it out" button
42-
}
43-
})
44-
)
20+
app.use('/docs', swaggerUi.serve)
21+
app.get('/docs', swaggerUi.setup(swaggerDoc, {
22+
customCssUrl: '../assets/swagger.css',
23+
customSiteTitle: 'OTTAA Realiser Docs',
24+
customfavIcon: '../assets/logo.ico',
25+
swaggerOptions: {
26+
supportedSubmitMethods: [] //to disable the "Try it out" button
27+
}
28+
}))
29+
4530

4631
app.get('/', (req, res) => res.send('Welcome to the coolest Realiser!'))
4732

48-
app.post('/prepare', allowCors, (req, res) => {
33+
app.post('/prepare', (req, res) => {
4934
const body = req.body;
5035
if (!body.words || !body.types) {
5136
res.status(400).send({err: 'Wrong request body, missing properties words and/or types'})
@@ -59,7 +44,7 @@ app.post('/prepare', allowCors, (req, res) => {
5944
})
6045
})
6146

62-
app.post('/parse', allowCors, (req, res) => {
47+
app.post('/parse', (req, res) => {
6348
const body = req.body;
6449
if (!body.words || !body.types) {
6550
res.status(400).send({err: 'Wrong request body, missing properties words and/or types'})
@@ -75,7 +60,7 @@ app.post('/parse', allowCors, (req, res) => {
7560
})
7661
})
7762

78-
app.post('/process', allowCors, (req, res) => {
63+
app.post('/process', (req, res) => {
7964
const body = req.body;
8065
if (!body.words || !body.types) {
8166
res.status(400).send({err: 'Wrong request body, missing properties words and/or types'})
@@ -92,7 +77,7 @@ app.post('/process', allowCors, (req, res) => {
9277
})
9378
})
9479

95-
app.post('/realise', allowCors, (req, res) => {
80+
app.post('/realise', (req, res) => {
9681
const body = req.body;
9782
if (!body.words || !body.types) {
9883
res.status(400).send({err: 'Wrong request body, missing properties words and/or types'})
@@ -110,7 +95,7 @@ app.post('/realise', allowCors, (req, res) => {
11095
})
11196
})
11297

113-
app.post('/replicate', allowCors, (req, res) => {
98+
app.post('/replicate', (req, res) => {
11499
res.status(200).json(req.body)
115100
})
116101

functions/docs/swagger.config.json functions/swagger/swagger.config.json

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
{
1010
"url": "https://us-central1-ottaaproject-flutter.cloudfunctions.net/realiser",
1111
"description": "deploy"
12+
},
13+
{
14+
"url": "http://localhost:9090",
15+
"description": "local"
1216
}
1317
],
1418
"paths": {

0 commit comments

Comments
 (0)