|
| 1 | +/** |
| 2 | + * parse后台脚手架 |
| 3 | + * www.yourCompany.com |
| 4 | + */ |
| 5 | + |
| 6 | +var express = require('express'); |
| 7 | +var https = require('https'); |
| 8 | +var { |
| 9 | + default: ParseServer, |
| 10 | + ParseGraphQLServer |
| 11 | +} = require('parse-server'); |
| 12 | +var path = require('path'); |
| 13 | +var ParseDashboard = require('parse-dashboard'); |
| 14 | +var ParseSwagger = require('./parse-swagger'); |
| 15 | +var swaggerUi = require('swagger-ui-express'); |
| 16 | +var fs = require('fs'); |
| 17 | +var appConfig = require('./app.json'); |
| 18 | + |
| 19 | +let { |
| 20 | + parse_port = 1337, |
| 21 | + dashboard_port = 4040, |
| 22 | + parse_host = { |
| 23 | + value: 'testapi.yourCompany.com' |
| 24 | + }, |
| 25 | + applications, |
| 26 | + dashboardUsers = [{ |
| 27 | + "user": "user", |
| 28 | + "pass": "user_pass" |
| 29 | + }] |
| 30 | +} = appConfig; |
| 31 | + |
| 32 | +let host = parse_host.value; |
| 33 | + |
| 34 | +//TODO: RELEASE_HOST与DEV_HOST的配置,需要与app.json中的parse_host.value配置结合起来 |
| 35 | +// domain对应parse_host.value,dbIp为相应的数据库ip |
| 36 | +const RELEASE_HOST = { |
| 37 | + domain: 'api.yourCompany.com', |
| 38 | + dbIp: '127.0.0.1' |
| 39 | +}; |
| 40 | +const DEV_HOST = { |
| 41 | + domain: 'testapi.yourCompany.com', |
| 42 | + dbIp: '127.0.0.2' |
| 43 | +}; |
| 44 | + |
| 45 | +// 数据库IP地址配置 |
| 46 | +let db_ip = (Object.is(host, RELEASE_HOST.domain)) ? RELEASE_HOST.dbIp : DEV_HOST.dbIp; |
| 47 | + |
| 48 | +let parse_baseUrl = `https://${host}:${parse_port}`; |
| 49 | +let dashboard_baseUrl = `https://${host}:${dashboard_port}`; |
| 50 | + |
| 51 | +let apps = applications.map(item => { |
| 52 | + let { |
| 53 | + dbConfig = {}, graphQLServerURL = {}, appId, appName, masterKey, cloud = {} |
| 54 | + } = item; |
| 55 | + let { |
| 56 | + user, |
| 57 | + pass, |
| 58 | + dbName, |
| 59 | + liveQuery |
| 60 | + } = dbConfig; |
| 61 | + |
| 62 | + let isDbConfigInValid = (user === undefined || pass === undefined || dbName === undefined); |
| 63 | + if (isDbConfigInValid) { |
| 64 | + Console.error("数据库连接信息缺失!"); |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + let graphQLPath = graphQLServerURL.value; |
| 69 | + let graphQLServerURL_fullPath = graphQLPath === undefined ? undefined : `${parse_baseUrl}${graphQLPath}`; |
| 70 | + let cloudDir = cloud.value; |
| 71 | + let cloudFullDir = cloudDir === undefined ? undefined : path.resolve(__dirname, cloudDir); |
| 72 | + |
| 73 | + return { |
| 74 | + parseBaseURL: parse_baseUrl, |
| 75 | + dashboardBaseUrl: dashboard_baseUrl, |
| 76 | + appId: appId.value, |
| 77 | + appName: appName.value, |
| 78 | + databaseURI: `mongodb://${user}:${pass}@${db_ip}:27017/${dbName}`, |
| 79 | + cloud: cloudFullDir, |
| 80 | + masterKey: masterKey.value, |
| 81 | + serverURL: `${parse_baseUrl}/${appId.value}`, |
| 82 | + graphQLServerURL: graphQLServerURL_fullPath, |
| 83 | + graphQLPath: graphQLPath, |
| 84 | + liveQuery: liveQuery, |
| 85 | + maxUploadSize: "100mb" |
| 86 | + }; |
| 87 | +}); |
| 88 | + |
| 89 | +/***************ParseServer部分配置********************/ |
| 90 | + |
| 91 | +// Client-keys like the javascript key or the .NET key are not necessary with |
| 92 | +// parse-server If you wish you require them, you can set them as options in the |
| 93 | +// initialization above: javascriptKey, restAPIKey, dotNetKey, clientKey |
| 94 | +let parseApp = express(); |
| 95 | +let dashboardApp = express(); |
| 96 | + |
| 97 | +apps.map(item => { |
| 98 | + let { |
| 99 | + appId, |
| 100 | + graphQLServerURL, |
| 101 | + graphQLPath |
| 102 | + } = item; |
| 103 | + |
| 104 | + let api = new ParseServer(item); |
| 105 | + //router |
| 106 | + parseApp.use(`/${appId}`, api.app); |
| 107 | + |
| 108 | + /***************parse的swagger配置********************/ |
| 109 | + let parseSwagger = new ParseSwagger(item); |
| 110 | + parseApp.use(parseSwagger);// 挂载swagger docs,路径为“/docs-${appId}” |
| 111 | + // swagger UI |
| 112 | + let swaggerOpt = { swaggerUrl: parse_baseUrl + `/docs-${appId}` }; |
| 113 | + parseApp.use(`/swagger-${appId}`, swaggerUi.serve, swaggerUi.setup(null, swaggerOpt)); |
| 114 | + |
| 115 | + /***************parse的GraphQL配置*********************/ |
| 116 | + if (!(Object.is(graphQLServerURL, undefined))) { |
| 117 | + var parseGraphQLServer_WP = new ParseGraphQLServer( |
| 118 | + api, { |
| 119 | + graphQLPath: graphQLPath, |
| 120 | + playgroundPath: '/playground' |
| 121 | + } |
| 122 | + ); |
| 123 | + |
| 124 | + // Mounts the GraphQL API on parse-server, NOT on dashboard. |
| 125 | + /* |
| 126 | + In fact,you can mount it on dashboardApp too. |
| 127 | + (with 'graphQLServerURL' change to `${dashboard_baseUrl}${graphQLPath}` ) |
| 128 | + */ |
| 129 | + parseGraphQLServer_WP.applyGraphQL(parseApp); |
| 130 | + |
| 131 | + //仅在非生产环境开启graphQL playground, |
| 132 | + if (!Object.is(host, RELEASE_HOST.domain)) { |
| 133 | + // (Optional) Mounts the GraphQL Playground - do NOT use in Production |
| 134 | + parseGraphQLServer_WP.applyPlayground(parseApp); |
| 135 | + } |
| 136 | + } |
| 137 | +}); |
| 138 | + |
| 139 | +/***************Dashboard部分配置********************/ |
| 140 | +//配置dashboard仅支持https协议访问 |
| 141 | +let dashboardOptions = { |
| 142 | + allowInsecureHTTP: false |
| 143 | +}; |
| 144 | + |
| 145 | +let dashboard = new ParseDashboard({ |
| 146 | + apps: apps, |
| 147 | + //设置dashboard的登录用户名密码,格式如下: |
| 148 | + /* |
| 149 | + 'users': [{ |
| 150 | + 'user': 'user', |
| 151 | + 'pass': 'user_pass', |
| 152 | + // "readOnly": false, |
| 153 | + // "apps": [{ "appId": "yourApp" }] |
| 154 | + }] */ |
| 155 | + users: dashboardUsers |
| 156 | +}, |
| 157 | + dashboardOptions); |
| 158 | + |
| 159 | +// Parse Server plays nicely with the rest of your web routes |
| 160 | +//默认访问时的提示信息 |
| 161 | +parseApp.get('/', function (req, res) { |
| 162 | + res.status(200).send( |
| 163 | + '欢迎访问parse-scaffold(parse后台脚手架)!'); |
| 164 | +}); |
| 165 | + |
| 166 | +//TODO:仅在非生产环境开启dashboard服务 |
| 167 | +//if (!Object.is(host,'api.yourCompany.com')) { |
| 168 | +dashboardApp.use('/dashboard', dashboard); |
| 169 | + |
| 170 | +// /public 静态资源目录,以后可公开发布的信息 |
| 171 | +//dashboardApp.use('/public', express.static(path.join(__dirname, '/public'))); |
| 172 | + |
| 173 | +//} |
| 174 | + |
| 175 | +//TODO: 此项需要配置服务器支持https访问(一般在nginx中配置) |
| 176 | +let httpsOption = { |
| 177 | + key: fs.readFileSync('/etc/nginx/cert/1234567_testapi.yourCompany.com.key'), |
| 178 | + cert: fs.readFileSync('/etc/nginx/cert/1234567_testapi.yourCompany.com.pem') |
| 179 | +}; |
| 180 | + |
| 181 | +let httpsServer = https.createServer(httpsOption, parseApp); |
| 182 | +httpsServer.listen(parse_port, () => { |
| 183 | + console.info(new Date().toLocaleString() + `: parse-server 已通过https在端口 ${parse_port} 启动`); |
| 184 | +}); |
| 185 | +ParseServer.createLiveQueryServer(httpsServer); |
| 186 | + |
| 187 | +https.createServer(httpsOption, dashboardApp).listen(dashboard_port, () => { |
| 188 | + console.info(new Date().toLocaleString() + `: parse-dashboard 已通过https在端口 ${dashboard_port} 启动`); |
| 189 | +}); |
0 commit comments