Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import * as React from "react" import simpleGit from "simple-git" import express, { NextFunction } from "express" require("express-async-errors") // todo: why the require? import cookieParser from "cookie-parser" import "reflect-metadata" import * as http from "http" import { ADMIN_SERVER_HOST, ADMIN_SERVER_PORT, ENV, SLACK_ERRORS_WEBHOOK_URL, } from "../settings/serverSettings" import * as db from "../db/db" import * as wpdb from "../db/wpdb" import { IndexPage } from "./IndexPage" import { authCloudflareSSOMiddleware, authMiddleware } from "./authentication" import { apiRouter } from "./apiRouter" import { testPageRouter } from "./testPageRouter" import { adminRouter } from "./adminRouter" import { renderToHtmlPage } from "../serverUtils/serverUtil" import { publicApiRouter } from "./publicApiRouter" import { mockSiteRouter } from "./mockSiteRouter" import { GIT_CMS_DIR } from "../gitCms/GitCmsConstants" const expressErrorSlack = require("express-error-slack") // todo: why the require? interface OwidAdminAppOptions { slackErrorsWebHookUrl?: string gitCmsDir: string isDev: boolean quiet?: boolean } export class OwidAdminApp { constructor(options: OwidAdminAppOptions) { this.options = options } app = express() private options: OwidAdminAppOptions private async getGitCmsBranchName() { const git = simpleGit({ baseDir: this.options.gitCmsDir, binary: "git", maxConcurrentProcesses: 1, }) const branches = await git.branchLocal() const gitCmsBranchName = await branches.current return gitCmsBranchName } private gitCmsBranchName = "" server?: http.Server async stopListening() { if (!this.server) return this.server.close() } async startListening(adminServerPort: number, adminServerHost: string) { this.gitCmsBranchName = await this.getGitCmsBranchName() const { app } = this // since the server is running behind a reverse proxy (nginx), we need to "trust" // the X-Forwarded-For header in order to get the real request IP // https://expressjs.com/en/guide/behind-proxies.html app.set("trust proxy", true) // Parse cookies https://github.com/expressjs/cookie-parser app.use(cookieParser()) app.use(express.urlencoded({ extended: true, limit: "50mb" })) app.use("/admin/login", authCloudflareSSOMiddleware) // Require authentication (only for /admin requests) app.use(authMiddleware) app.use("/api", publicApiRouter.router) app.use("/admin/api", apiRouter.router) app.use("/admin/test", testPageRouter) app.use("/admin/assets", express.static("itsJustJavascript/webpack")) app.use("/admin/storybook", express.static(".storybook/build")) app.use("/admin", adminRouter) // Default route: single page admin app app.get("/admin/*", async (req, res) => { res.send( renderToHtmlPage( <IndexPage username={res.locals.user.fullName} isSuperuser={res.locals.user.isSuperuser} gitCmsBranchName={this.gitCmsBranchName} /> ) ) }) // Send errors to Slack // The middleware passes all errors onto the next error-handling middleware if (this.options.slackErrorsWebHookUrl) app.use( expressErrorSlack({ webhookUri: this.options.slackErrorsWebHookUrl, }) ) // todo: we probably always want to have this, and can remove the isDev if (this.options.isDev) app.use("/", mockSiteRouter) // Give full error messages, including in production app.use(this.errorHandler) await this.connectToDatabases() this.server = await this.listenPromise( app, adminServerPort, adminServerHost ) this.server.timeout = 5 * 60 * 1000 // Increase server timeout for long-running uploads if (!this.options.quiet) console.log( `owid-admin server started on http://${adminServerHost}:${adminServerPort}` ) } // Server.listen does not seem to have an async/await form yet. // https://github.com/expressjs/express/pull/3675 // https://github.com/nodejs/node/issues/21482 private listenPromise( app: express.Express, adminServerPort: number, adminServerHost: string ): Promise<http.Server> { return new Promise((resolve) => { const server = app.listen(adminServerPort, adminServerHost, () => { resolve(server) }) }) } errorHandler = async ( err: any, req: express.Request, res: express.Response, // keep `next` because Express only passes errors to handlers with 4 parameters. // eslint-disable-next-line @typescript-eslint/no-unused-vars next: NextFunction ) => { if (!res.headersSent) { res.status(err.status || 500) res.send({ error: { message: err.stack || err, status: err.status || 500, }, }) } else { res.write( JSON.stringify({ error: { message: err.stack || err, status: err.status || 500, }, }) ) res.end() } } connectToDatabases = async () => { try { await db.getConnection() } catch (error) { // grapher database is in fact required, but we will not fail now in case it // comes online later if (!this.options.quiet) { console.error(error) console.warn( "Could not connect to grapher database. Continuing without DB..." ) } } if (wpdb.isWordpressDBEnabled) { try { await wpdb.singleton.connect() } catch (error) { if (!this.options.quiet) { console.error(error) console.warn( "Could not connect to Wordpress database. Continuing without Wordpress..." ) } } } else if (!this.options.quiet) { console.log( "WORDPRESS_DB_NAME is not configured -- continuing without Wordpress DB" ) } if (!wpdb.isWordpressAPIEnabled && !this.options.quiet) { console.log( "WORDPRESS_API_URL is not configured -- continuing without Wordpress API" ) } } } if (!module.parent) new OwidAdminApp({ slackErrorsWebHookUrl: SLACK_ERRORS_WEBHOOK_URL, gitCmsDir: GIT_CMS_DIR, isDev: ENV === "development", }).startListening(ADMIN_SERVER_PORT, ADMIN_SERVER_HOST) |