1
1
const express = require ( 'express' ) ;
2
2
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' )
4
5
const { log, error } = require ( "firebase-functions/lib/logger" ) ;
5
6
6
7
const {
@@ -10,42 +11,26 @@ const {
10
11
realiseSentence
11
12
} = require ( './methods.js' ) ;
12
13
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
-
27
14
const app = express ( )
28
15
16
+ app . use ( cors ( ) )
29
17
app . use ( express . json ( ) ) ;
30
18
app . use ( express . urlencoded ( { extended : true } ) ) ;
31
19
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
+
45
30
46
31
app . get ( '/' , ( req , res ) => res . send ( 'Welcome to the coolest Realiser!' ) )
47
32
48
- app . post ( '/prepare' , allowCors , ( req , res ) => {
33
+ app . post ( '/prepare' , ( req , res ) => {
49
34
const body = req . body ;
50
35
if ( ! body . words || ! body . types ) {
51
36
res . status ( 400 ) . send ( { err : 'Wrong request body, missing properties words and/or types' } )
@@ -59,7 +44,7 @@ app.post('/prepare', allowCors, (req, res) => {
59
44
} )
60
45
} )
61
46
62
- app . post ( '/parse' , allowCors , ( req , res ) => {
47
+ app . post ( '/parse' , ( req , res ) => {
63
48
const body = req . body ;
64
49
if ( ! body . words || ! body . types ) {
65
50
res . status ( 400 ) . send ( { err : 'Wrong request body, missing properties words and/or types' } )
@@ -75,7 +60,7 @@ app.post('/parse', allowCors, (req, res) => {
75
60
} )
76
61
} )
77
62
78
- app . post ( '/process' , allowCors , ( req , res ) => {
63
+ app . post ( '/process' , ( req , res ) => {
79
64
const body = req . body ;
80
65
if ( ! body . words || ! body . types ) {
81
66
res . status ( 400 ) . send ( { err : 'Wrong request body, missing properties words and/or types' } )
@@ -92,7 +77,7 @@ app.post('/process', allowCors, (req, res) => {
92
77
} )
93
78
} )
94
79
95
- app . post ( '/realise' , allowCors , ( req , res ) => {
80
+ app . post ( '/realise' , ( req , res ) => {
96
81
const body = req . body ;
97
82
if ( ! body . words || ! body . types ) {
98
83
res . status ( 400 ) . send ( { err : 'Wrong request body, missing properties words and/or types' } )
@@ -110,7 +95,7 @@ app.post('/realise', allowCors, (req, res) => {
110
95
} )
111
96
} )
112
97
113
- app . post ( '/replicate' , allowCors , ( req , res ) => {
98
+ app . post ( '/replicate' , ( req , res ) => {
114
99
res . status ( 200 ) . json ( req . body )
115
100
} )
116
101
0 commit comments