Code Monkey home page Code Monkey logo

ka-mensa-api's Introduction

ka-mensa-api

CI Test Coverage Maintainability

Introduction

ka-mensa-api is one component in a three-part project whose goal it is to aggregate, process and visualize the Studierendenwerk Karlsruhe's canteen plans in ways superior to the official sources.

Disclaimer: This project is neither affiliated with nor endorsed by the Studierendenwerk Karlsruhe or the Karlsruhe Institute of Technology.

The entire project is written in JavaScript+TypeScript and is composed as follows:

  • ka-mensa-fetch: library package responsible for the fetching of raw plan data and conversion into canonical, easily digestible JSON documents
  • ka-mensa-api: NodeJS server that utilizes the fetcher to continuously collect meal plans and makes them available via REST API
  • ka-mensa-ui: single-page web app that loads meal plans from an API instance and displays them in a modern, responsive interface with filtering and color-coding capabilities

Setup (standard)

Prerequisites

Ensure Node and NPM are available on your system. Then clone this repository somewhere and run npm install to load dependencies.

Configuration

Open up src/config.ts and configure to your liking. Notice that network options are rather limited. If you want HTTPS support or advanced embedding into existing domain structures, you will need to set up a reverse proxy, such as nginx.

You might want to change the plan fetch source. This will not impact the API behavior but only how data is retrieved. There are two options available:

  • 'simplesite' (the default)
  • 'jsonapi'

The simplesite source fetches plans from the Studierendenwerk's website, which offers a view for the visually impaired that is quite easy to parse. It allows for fetching plans far into the future and is relatively stable. That said, this still breaks sometimes, e.g. when the Studierendenwerk renames lines or takes other unpredictable actions.

The JSON API source is more reliable. It will basically never break. The downside: it requires authentication (you will have to figure out how to obtain credentials on your own).

In summary, both sources provide the same information but differ in stability and auth requirements.

Startup

To start the server, run npm start. It will immediately fetch the most recent set of plans, then listen for API requests. Plan polling will continue indefinitely with the interval set in src/config.ts.

Note that npm start is shortcut for npm run build followed by npm run production, where the former compiles the TypeScript code and the latter executes it. To start the server without compiling again, use npm run production only.

A fixup job will run at startup. This fills in missing data that might have been missing at cache time but is now inferrable due to an updated data set. Any plans that are considered complete, or that cannot be inferred further, will not be changed.

Setup with Docker

This project is available as a Docker image! See also https://hub.docker.com/r/meyfa/ka-mensa-api.

Running the Container

If you just want to get going:

docker run --name mensa -p <host-port>:8080 -d meyfa/ka-mensa-api

If you would like to put the cache in a volume (highly recommended!) so it has enough room to grow, is available after restarting the container and/or can be accessed from the host: Mount to /usr/src/app/cache like so:

docker run \
        --name mensa \
        -p <host-port>:8080 \
        -v /path/on/host:/usr/src/app/cache \
        -d meyfa/ka-mensa-api

CORS Headers

Perhaps you need to configure CORS for the API server - to enable displaying the plans via an instance of ka-mensa-ui running on another domain, for example. This can be done via the config file or by setting an environment variable:

docker run \
        --name mensa \
        -p <host-port>:8080 \
        -v /path/on/host:/usr/src/app/cache \
        -e MENSA_CORS_ALLOWORIGIN=https://example.com \
        -d meyfa/ka-mensa-api

Specify a regular URL to only allow that one origin. Use * to allow all origins.

Environment Variables

Some options can be configured via environment variables. They are as follows:

  • MENSA_CACHE_DIRECTORY: The path to a directory where downloaded plans should be stored, and where they should be served from. Defaults to a cache/ directory inside the working directory.
  • MENSA_CORS_ALLOWORIGIN: Set this to a specific URL to allow CORS requests from that URL, or set to * to allow all origins. Defaults to not serving any CORS headers.

Development

Contributions are welcome. Guidelines:

  • By contributing, you agree to make your changes available under the MIT license of this project.
  • Please write unit tests for as much code as possible.
    • To run: npm test
    • With coverage: npm run coverage
  • Make sure to adhere to JS standard style and proper usage of TypeScript.
    • Linter: npm run lint
    • Automatic fixing of most style issues: npm run lint-fix

Documentation

A document outlining the API provided by this software is available: API_DOCUMENTATION.md.

ka-mensa-api's People

Contributors

dependabot[bot] avatar meyfa avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

ka-mensa-api's Issues

Fixup of past plans

Sometimes, ka-mensa-fetch isn't updated quickly enough to reflect changes to the canteen and/or line names. This causes null ids in cached plans. There should be a fixup job that runs at process startup which looks for such null entries and tries to fill them in. That way, when ka-mensa-fetch is updated to be able to resolve the new names, even old plans can potentially be rescued.

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Awaiting Schedule

These updates are awaiting their schedule. Click on a checkbox to get an update now.

  • chore(deps): update dependency c8 to v10
  • chore(deps): lock file maintenance

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

dockerfile
Dockerfile
  • node 20.12.2-alpine
  • node 20.12.2-alpine
github-actions
.github/workflows/docker.yml
  • actions/checkout v4
  • docker/setup-buildx-action v3
  • docker/login-action v3
  • docker/build-push-action v5
.github/workflows/main.yml
  • actions/checkout v4
  • actions/setup-node v4
  • actions/checkout v4
  • actions/setup-node v4
  • actions/checkout v4
  • actions/setup-node v4
  • paambaati/codeclimate-action v5.0.0
npm
package.json
  • @fastify/cors 9.0.1
  • fastify 4.26.0
  • fs-adapters 7.0.2
  • group-items 4.0.0
  • ka-mensa-fetch 4.0.2
  • moment 2.30.1
  • ms 2.1.3
  • omniwheel 0.4.2
  • winston 3.13.0
  • @meyfa/eslint-config 5.1.0
  • @types/mocha 10.0.6
  • @types/ms 0.7.34
  • @types/node 20.12.7
  • c8 9.1.0
  • eslint 8.57.0
  • eslint-plugin-jsdoc 48.2.3
  • mocha 10.4.0
  • ts-node 10.9.2
  • typescript 5.4.5
  • node >=18.16.1

  • Check this box to trigger a request for Renovate to run again on this repository

Eliminate config.ts

Configuration via a source file is bad for multiple reasons:

  • Changes to the config must be recompiled
  • It gets the cloned Git repository out of sync
  • It doesn't really work with Docker

Most of the settings aren't needed anyway. I suggest removing them entirely, and converting others to environment variables, which are a more portable and standardized alternative.

Related issues:

  • #145
  • #146
  • #147
  • Move server options out of config.ts
  • Move fetch options out of config.ts
  • Update README to better explain ways to configure
  • Unify options parsing

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.