Michael Lando | 451a340 | 2017-02-19 10:28:42 +0200 | [diff] [blame^] | 1 | /*- |
| 2 | * ============LICENSE_START======================================================= |
| 3 | * SDC |
| 4 | * ================================================================================ |
| 5 | * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. |
| 6 | * ================================================================================ |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | * ============LICENSE_END========================================================= |
| 19 | */ |
| 20 | |
| 21 | var express = require('express'); |
| 22 | var mockApis = require('../configurations/mock.json').sdcConfig; |
| 23 | //var mockUris = require('../configurations/mock.json'); |
| 24 | var cors = require('cors'); |
| 25 | var multer = require('multer') |
| 26 | var basePathToMockData = './mock-data/'; |
| 27 | var app = express(); |
| 28 | |
| 29 | var allowedHeaders = 'Content-Type,Authorization,If-Modified-Since,'; |
| 30 | allowedHeaders += mockApis.cookie.userIdSuffix; |
| 31 | allowedHeaders += ','+mockApis.cookie.userEmail; |
| 32 | allowedHeaders += ','+mockApis.cookie.userFirstName; |
| 33 | allowedHeaders += ','+mockApis.cookie.userLastName; |
| 34 | allowedHeaders += ','+mockApis.cookie.xEcompRequestId; |
| 35 | |
| 36 | |
| 37 | |
| 38 | app.use(cors({ |
| 39 | // origin: '*', |
| 40 | origin: function(origin, callback) { |
| 41 | callback(null, true); |
| 42 | }, |
| 43 | methods: 'GET, POST, PUT, DELETE', |
| 44 | allowedHeaders: allowedHeaders, |
| 45 | credentials: true |
| 46 | })); |
| 47 | |
| 48 | //set cookie middleware |
| 49 | app.use(function(req, res, next) { |
| 50 | |
| 51 | res.cookie(mockApis.cookie.userIdSuffix, req.headers[mockApis.cookie.userIdSuffix] || mockApis.userTypes.designer.userId ); |
| 52 | res.cookie(mockApis.cookie.userEmail, req.headers[mockApis.cookie.userEmail] || mockApis.userTypes.designer.email); |
| 53 | res.cookie(mockApis.cookie.userFirstName, req.headers[mockApis.cookie.userFirstName] || mockApis.userTypes.designer.firstName); |
| 54 | res.cookie(mockApis.cookie.userLastName, req.headers[mockApis.cookie.userLastName] || mockApis.userTypes.designer.lastName); |
| 55 | res.cookie(mockApis.cookie.xEcompRequestId, req.headers[mockApis.cookie.xEcompRequestId] || mockApis.userTypes.designer.lastName); |
| 56 | next(); |
| 57 | }); |
| 58 | |
| 59 | var userRoutes = require('./routes/user'); |
| 60 | app.use('/v1/user', userRoutes); |
| 61 | var resourceRoutes = require('./routes/resource'); |
| 62 | app.use('/v1/resource', resourceRoutes); |
| 63 | var templateRoutes = require('./routes/template'); |
| 64 | app.use('/v1/template', templateRoutes); |
| 65 | var propertyRoutes = require('./routes/property'); |
| 66 | app.use('/v1/resource/:resourceId/property', propertyRoutes); |
| 67 | var resourcesRoutes = require('./routes/resources'); |
| 68 | app.use('/v1/catalog/resources', resourcesRoutes); |
| 69 | |
| 70 | /******************************************* MOCKS ENPOINTS *************************************************/ |
| 71 | /* get user details */ |
| 72 | // app.get(mockApis.api.GET_user, function (req, res) { |
| 73 | // var user = require(basePathToMockData+'user/user.json'); |
| 74 | // res.send(user); |
| 75 | // }); |
| 76 | |
| 77 | /* get elements */ |
| 78 | app.get(mockApis.api.GET_element, function (req, res) { |
| 79 | |
| 80 | var element = require(basePathToMockData+'element/element.json'); |
| 81 | res.send(element); |
| 82 | }); |
| 83 | |
| 84 | /* get elements */ |
| 85 | app.get(mockApis.api.GET_catalog, function (req, res) { |
| 86 | |
| 87 | var element = require(basePathToMockData+'element/element.json'); |
| 88 | res.send(element); |
| 89 | }); |
| 90 | |
| 91 | /* get categories */ |
| 92 | app.get(mockApis.api.GET_category, function (req, res) { |
| 93 | |
| 94 | var categories = require(basePathToMockData+'category/category.json'); |
| 95 | res.send(categories); |
| 96 | }); |
| 97 | |
| 98 | |
| 99 | /* get categories */ |
| 100 | app.get(mockApis.api.GET_configuration_ui, function (req, res) { |
| 101 | |
| 102 | var categories = require(basePathToMockData+'artifact/artifact-types.json'); |
| 103 | res.send(categories); |
| 104 | }); |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | //upload artifact file |
| 114 | app.use(multer({ dest: './uploads/', |
| 115 | rename: function (fieldname, filename) { |
| 116 | return filename+Date.now(); |
| 117 | }, |
| 118 | onFileUploadStart: function (file) { |
| 119 | console.log(file.originalname + ' is starting ...') |
| 120 | }, |
| 121 | onFileUploadComplete: function (file) { |
| 122 | console.log(file.fieldname + ' uploaded to ' + file.path) |
| 123 | done=true; |
| 124 | } |
| 125 | })); |
| 126 | |
| 127 | var done=false; |
| 128 | app.post(mockApis.api.GET_resource_artifact,function(req,res){ |
| 129 | if(done==true){ |
| 130 | console.log(req.files); |
| 131 | res.end("File uploaded."); |
| 132 | } |
| 133 | }); |
| 134 | |
| 135 | |
| 136 | /**************************************************** *******************************************************/ |
| 137 | |
| 138 | var server = app.listen(9999, function () { |
| 139 | console.log('mock server listening on port %d', server.address().port); |
| 140 | }); |