@adpt/cloud
Version:
AdaptJS cloud component library
140 lines (138 loc) • 5.26 kB
JavaScript
"use strict";
/*
* Copyright 2019 Unbounded Systems, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = tslib_1.__importStar(require("@adpt/core"));
const utils_1 = require("@adpt/utils");
const Container_1 = require("../Container");
const docker_1 = require("../docker");
const http_1 = require("../http");
const NetworkService_1 = require("../NetworkService");
const Service_1 = require("../Service");
const nginxImg = "nginx:latest";
/*
* Match
*/
const matchWriters = new utils_1.Dispatcher("Match");
const matchConfig = (m) => matchWriters.dispatch(m);
matchWriters.add("path", (m) => m.path);
matchWriters.add("regex", (m) => `~ ${m.regex}`);
/*
* Dest
*/
const destWriters = new utils_1.Dispatcher("Destination");
const destConfig = (d) => destWriters.dispatch(d);
destWriters.add("files", (d) => d.filesRoot ? `root ${d.filesRoot};` : "");
/*
* Location
*/
const locationConfig = (loc) => `
location ${matchConfig(loc.match)} {
${destConfig(loc.dest)}
}
`;
function useMakeNginxConf(props) {
const servers = props.servers || [];
if (servers.length === 0) {
throw new Error(`Nginx configuration must contain at least one virtual server`);
}
if (servers.length > 1) {
throw new Error(`Multiple servers not implemented yet`);
}
const serverConf = servers.map((s) => {
const locations = s.locations.map(locationConfig);
const root = s.filesRoot ? `root ${s.filesRoot};` : "";
return `
server {
${root}
listen ${props.port};
${locations.join("\n")}
}
`;
});
return `
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
${serverConf.join("\n")}
}
`;
}
const defaultProps = Object.assign({}, http_1.HttpServer.defaultProps, { servers: [{
filesRoot: "/www/static",
locations: [{
match: { type: "path", path: "/" },
dest: { type: "files" },
}],
}] });
/**
* {@link http.HttpServer} implementation based on {@link https://nginx.org | nginx}
*
* @public
*/
function HttpServer(propsIn) {
const props = propsIn;
const netSvc = core_1.handle();
const nginx = core_1.handle();
const img = core_1.handle();
const deps = http_1.fileHandles(props.add);
const nginxConf = useMakeNginxConf(props);
const fileInfo = http_1.useFilesInfo(props.add) || [];
const commands = fileInfo.map((f) => f.dockerCommands).join("\n");
const stages = fileInfo.map((f) => f.stage).filter(utils_1.notNull);
//FIXME(manishv) nginx config check will only pass if all hostnames can be resolved locally, how to fix?
//if (false) useAsync(async () => checkNginxConf(nginxConf), undefined);
const dockerfile = `
FROM ${nginxImg}
RUN apt-get update && \
apt-get install --no-install-recommends --no-install-suggests -y inotify-tools && \
apt-get clean
WORKDIR /nginx
COPY --from=files / .
${commands}
CMD [ "/bin/sh", "/nginx/start_nginx.sh" ]
`;
core_1.useMethodFrom(netSvc, "hostname");
core_1.useMethodFrom(netSvc, "port");
const ret = core_1.default.createElement(core_1.Sequence, { key: props.key },
deps,
core_1.default.createElement(docker_1.LocalDockerImage, { key: props.key + "-img", handle: img, dockerfile: dockerfile, files: [{
path: "start_nginx.sh",
contents: `#!/bin/sh
exec nginx -g "daemon off;" -c /nginx/nginx.conf
`
},
{
path: "nginx.conf",
contents: nginxConf
}], contextDir: props.localAddRoot, options: {
imageName: "nginx-static",
uniqueTag: true
}, stages: stages }),
core_1.default.createElement(Service_1.Service, { key: props.key },
core_1.default.createElement(NetworkService_1.NetworkService, { key: props.key + "-netsvc", handle: netSvc, endpoint: nginx, port: props.port, targetPort: props.port, scope: props.scope }),
core_1.default.createElement(Container_1.Container, { key: props.key, handle: nginx, name: "nginx-static", image: img, ports: [props.port], imagePullPolicy: "Never" })));
return ret;
}
exports.HttpServer = HttpServer;
// TODO: The "as any" is a workaround for an api-extractor bug. See issue #185.
HttpServer.defaultProps = defaultProps;
exports.default = HttpServer;
//# sourceMappingURL=HttpServer.js.map