Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log tail #25

Merged
merged 8 commits into from
Apr 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ your can change the port adding `-e "port=xxx"` param

See the content of `docker` directory. There are Dockerfile and docker-compose template. E.g. run `docker build -f docker/Dockerfile -t abaplint-backend .` to build your image from scratch.

TODO: https docker compose, logging advices
*TODO: https docker compose, logging advices*

### Env variables

The package respects `.env` file (must not be committed to the repo though!). Here are the available variables:

- PORT - port to listen at
- ALB_SUPPRESS_FRONPAGE_LOG - disable frontpage log: set `1` to disable

## Development

Expand All @@ -50,3 +57,5 @@ Useful scripts
- docker
- `bin/docker-build.sh` - build docker container from command line (supposes bash environment)
- `bin/docker-run.sh` - run the built above container

See also: [docker dev notes](./docker/dev-notes.md)
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ COPY ["package*.json", "LICENSE", "./"]
RUN npm ci --quiet --only=production && npm cache clean --force --silent
COPY --from=builder /usr/src/app/build/ ./build

# HEALTHCHECK --interval=30s CMD node healthcheck.js ?
# HEALTHCHECK --interval=30s CMD node healthcheck.js ? or curl localhost/healthz = OK ?

ENTRYPOINT [ "/sbin/tini", "--", "node", "build/server.js" ]
24 changes: 15 additions & 9 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
import * as express from "express";
import {checkObject} from "./check_object";
import { addInfoEx } from "../lib/log-tail";

const router = express.Router();

router.use(express.json({limit: "50mb"}));
router.use(express.urlencoded({limit: "50mb", extended: false}));

router.post("/ping", (_req, res) => {
// addInfo("ping, " + new Date());
addInfoEx("ping");
res.json({ success: true, payload: "abap is forevah!" });
});

router.post("/check_file", (req, res) => {
// TODO validate request
// TODO define response type ?
// TODO capture exceptions, return json with error
const hrstart = process.hrtime();
const result = checkObject(req.body);
// addInfo("check_file, " +
// result.object.objectType + " " +
// result.object.objectName + ", " +
// result.issues.length + " issues, " +
// new Date() + ", " +
// req.socket.bytesRead + " bytes");
const hrend = process.hrtime(hrstart);
addInfoEx([
"check_file",
result.object.objectType,
result.object.objectName,
`${result.issues.length} issues`,
`${req.socket.bytesRead} bytes`,
`${(hrend[0] * 1000 + hrend[1] / 1000000).toFixed()} ms`,
]);
res.json(result);
});

// app.post("/api/v1/check_configuration",
// app.post("/api/v1/get_default_configuration",
// app.post("/api/v1/pretty_print",

router.all("*", (_req, res) => {
return res.status(404).json({ success: false, error: { message: "Wrong API call" } });
router.all("*", (req, res) => {
addInfoEx("unexpected API call: " + req.originalUrl);
res.status(404).json({ success: false, error: { message: "Wrong API call" } });
});

export default router;
14 changes: 0 additions & 14 deletions src/app.smoke.test.ts

This file was deleted.

17 changes: 6 additions & 11 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,22 @@ import * as morgan from "morgan";
import * as helmet from "helmet";
import { frontPage } from "./front_page";
import api from "./api";
import { addInfoEx } from "./lib/log-tail";

const app = express();

// const info: string[] = [];

// function addInfo(s: string): void {
// info.push(s);
// if (info.length > 10) {
// info.shift();
// }
// }

if (process.env.NODE_ENV !== "test") {
// app.use(cors());
app.use(helmet());
app.use(morgan("common"));
}

app.get("/", (_req, res) => res.send(frontPage([])));
app.get("/", (_req, res) => res.send(frontPage()));
app.get("/healthz", (_req, res) => res.send("OK"));
app.use("/api/v1", api);
app.use("*", (_req, res) => res.status(404).send("forbidden") );
app.use("*", (req, res) => {
addInfoEx("unexpected request: " + req.originalUrl);
res.status(404).send("forbidden");
});

export default app;
14 changes: 12 additions & 2 deletions src/front_page.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as abaplint from "@abaplint/core";
import * as os from "os";
import { getLogTail } from "./lib/log-tail";

function osInfo(): string {
return "load: " + os.loadavg() + "<br>" +
Expand All @@ -10,7 +11,16 @@ function osInfo(): string {
"cpus: " + JSON.stringify(os.cpus()) + "<br>";
}

export function frontPage(info: string[]): string {
function renderLogTail(): string {
if (process.env.ALB_SUPPRESS_FRONPAGE_LOG === "1") {
return "";
} else {
const info = getLogTail();
return info.join("<br>");
}
}

export function frontPage(): string {
return `<!DOCTYPE html>
<html>
<head>
Expand All @@ -21,7 +31,7 @@ export function frontPage(info: string[]): string {
<hr>
${osInfo()}
<hr>
${info.join("<br>")}
${renderLogTail()}
</body>
</html>
`;
Expand Down
10 changes: 10 additions & 0 deletions src/lib/log-tail.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { addInfo, getLogTail } from "./log-tail";

test("should add", () => {
addInfo("hello");
addInfo("world");
expect(getLogTail()).toEqual([
"hello",
"world",
]);
});
29 changes: 29 additions & 0 deletions src/lib/log-tail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const logTail: string[] = [];
let tailLength = 10; // default

export function setTailLength(newTailLength: number): void {
if (newTailLength <= 0) {
throw Error("Unexpected tailLength");
}
tailLength = newTailLength;
}

export function addInfo(s: string): void {
logTail.push(s);
if (logTail.length > tailLength) {
logTail.shift();
}
}

export function getLogTail(): string[] {
return logTail;
}

export function formatDate(date: Date): string {
return date.toISOString().substr(0, 19).replace("T", " ");
}

export function addInfoEx(entries: string | string[]): void {
entries = Array.isArray(entries) ? entries : [entries];
addInfo([ formatDate(new Date()), ...entries ].join(", "));
}
3 changes: 0 additions & 3 deletions test/dummy.test.ts

This file was deleted.