Skip to content

Instantly share code, notes, and snippets.

@fractalliter
Last active April 28, 2024 19:31

Revisions

  1. fractalliter revised this gist Apr 28, 2024. 2 changed files with 11 additions and 3 deletions.
    8 changes: 8 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -71,4 +71,12 @@ We can easily check logs for the service in real-time via [journalctl](https://w
    journalctl -u hello-world.service -f
    ```

    You should see something like bellow if you send `curl localhost:3000/elias?github=fractalliter`:

    ```text
    Apr 28 20:36:44 user-something systemd[1]: Started Hello world server.
    Apr 28 20:36:46 user-something ts-node[21800]: Server running at PORT: 3000
    Apr 28 20:36:57 user-something ts-node[21800]: request received param elias that queries {"github":"fractalliter"}
    ```

    JournalCTL enables us to query the logs between dates and for specific log level like `error` or `info`.
    6 changes: 3 additions & 3 deletions index.ts
    Original file line number Diff line number Diff line change
    @@ -4,12 +4,12 @@ import express, { Request, Response } from "express";

    const app = express();

    app.use(express.json()) // for parsing application/json
    //
    app.use(express.json())

    const PORT = process.env.PORT;

    app.get("/:name", (request: Request, response: Response) => {
    console.log(`request received param ${request.params.name} that queries ${JSON.stringify(request.query)}`);
    console.log(`request received param ${request.params.name} that queries ${JSON.stringify(request.query)}`);
    response.status(200).send("Hello World");
    });

  2. fractalliter revised this gist Apr 28, 2024. No changes.
  3. fractalliter created this gist Apr 28, 2024.
    74 changes: 74 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    # Description

    [Systemd](https://systemd.io/) is a suite of basic building blocks for a Linux system.
    It provides a system and service manager that runs as PID 1 and starts the rest of the system.
    There are cases that you want to run a script or job on a Linux server and keep track of the logs without an external library
    and also get the benfit of seamless control over the application lifecycle on the server. Systemd provides all of this out of the box.
    In this simple example I tried to demonstrate the use case with a simple [Express](https://expressjs.com/en/starter/hello-world.html)
    hello world server that is developed in [Typescript](https://www.typescriptlang.org/) and [TS-Node](https://github.com/TypeStrong/ts-node).

    ## Prerequisites

    - Nodejs
    - NPM

    ## How to create systemd service

    You need to install Node in your Linux server and then install [ts-node](https://github.com/TypeStrong/ts-node) package:

    ```bash
    # Or globally with TypeScript.
    npm install -g typescript
    npm install -g ts-node
    ```

    Create a directory at `/usr/local/` for your app. you may need admin permission:

    ```bash
    mkdir /usr/local/hello-world
    ```
    Then change your direcotry to:

    ```bash
    cd /usr/local/hello-world
    ```

    You need to initialize your node project and install express library:

    ```bash
    npm init -y

    npm i express
    npm i -D @types/express
    ```

    you need to create `index.ts` file and copy/paste the content of the file into it. we are set with the script but
    we also need to create systemd service:

    ```bash
    touch /etc/systemd/system/hello-world.service
    ```

    and then copy/paste the content of `hello-server.service` file into it.

    We are also set with systemd service configruation.

    ## How to run

    You are now able to execute the server under systemd services and control the status with systemd commands.

    ```bash
    systemctl daemon-reload
    systemctl enable hello-world.service
    systemctl start hello-world.service
    ```

    ## How to see logs

    We can easily check logs for the service in real-time via [journalctl](https://www.freedesktop.org/software/systemd/man/latest/journalctl.html):

    ```bash
    journalctl -u hello-world.service -f
    ```

    JournalCTL enables us to query the logs between dates and for specific log level like `error` or `info`.
    14 changes: 14 additions & 0 deletions hello-server.service
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    [Unit]
    Description=Hello world server
    After=network.target
    StartLimitIntervalSec=0

    [Service]
    Type=simple
    Restart=on-failure
    RestartSec=1
    ExecStart=/usr/bin/ts-node /usr/local/hello-world/index.ts
    Environment=PORT=3000

    [Install]
    WantedBy=multi-user.target
    18 changes: 18 additions & 0 deletions index.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    #!/usr/bin/ts-node

    import express, { Request, Response } from "express";

    const app = express();

    app.use(express.json()) // for parsing application/json
    //
    const PORT = process.env.PORT;

    app.get("/:name", (request: Request, response: Response) => {
    console.log(`request received param ${request.params.name} that queries ${JSON.stringify(request.query)}`);
    response.status(200).send("Hello World");
    });

    app.listen(PORT, () => {
    console.log("Server running at PORT: ", PORT);
    })