Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
alessioC42 committed Feb 3, 2024
1 parent 3751fef commit 4952195
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# http://editorconfig.org
root = true

[*]
indent_style = tab
tab_width = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.yml]
indent_style = space
6 changes: 6 additions & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
/node_modules
*-lock.*
*.lock
*.log
.wrangler
19 changes: 19 additions & 0 deletions src/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"semi": true,
"printWidth": 100,
"singleQuote": true,
"bracketSpacing": true,
"insertPragma": false,
"requirePragma": false,
"jsxSingleQuote": false,
"bracketSameLine": false,
"embeddedLanguageFormatting": "auto",
"htmlWhitespaceSensitivity": "css",
"vueIndentScriptAndStyle": true,
"quoteProps": "consistent",
"proseWrap": "preserve",
"trailingComma": "es5",
"arrowParens": "avoid",
"useTabs": true,
"tabWidth": 2
}
31 changes: 31 additions & 0 deletions src/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## Template: worker-router

[![Deploy to Cloudflare Workers](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/templates/tree/main/worker-router)

This template demonstrates using the [`itty-router`](https://github.com/kwhitley/itty-router) package to add routing to your Cloudflare Workers.

[`index.js`](https://github.com/cloudflare/worker-template-router/blob/master/index.js) is the content of the Workers script.

## Setup

To create a `my-project` directory using this template, run:

```sh
$ npx wrangler generate my-project worker
# or
$ yarn wrangler generate my-project worker
# or
$ pnpm wrangler generate my-project worker
```

Before publishing your code you need to edit `wrangler.toml` file and add your Cloudflare `account_id` - more information about configuring and publishing your code can be found [in the documentation](https://developers.cloudflare.com/workers/learning/getting-started).

Once you are ready, you can publish your code by running the following command:

```sh
$ npm run deploy
# or
$ yarn run deploy
# or
$ pnpm run deploy
```
36 changes: 36 additions & 0 deletions src/defaultparser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

/*
* @param {string} stufe - The grade of the student.
* @param {string} klasse - The class of the student.
* @param {boolean} isStudent - Whether the user is a student or Teacher/Parent.
*
* @returns {Object} - The extracted data from the input.
* */
export function defaultSubstitutionsFilterExtractor(stufe, klasse, isStudent) {
klasse = klasse.toLowerCase();

//Oberstufenklassen haben oft sehr seltsame Bezeichnungen. Diese sollen primär schulspezifisch gehandhabt werden.
if (klasse.includes("q") || klasse.includes("e")) {
//get first digit from string
let klasseMatch = klasse.match(/\d+/);
return {
"stufe": klasse.includes("q") ? "Q" : "E",
"klasse": klasseMatch ? klasseMatch[0] : "",
"lehrer": "",
}
}

//Klassenbezeichnungen wie 5a, 5b, 5c, 5d, 10a, 10b, 13c etc.
//lösung mit regex
let klasseMatch = klasse.match(/(\d+)([a-zA-Z]+)/);
if (klasseMatch) {
return {
"stufe": parseInt(klasseMatch[1]),
"klasse": klasseMatch[2],
"lehrer": "",
}
}


return {klasse: "", stufe: "", lehrer: ""};
}
32 changes: 32 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Router } from 'itty-router';
import SCHOOL_CONFIGS from "./school_config";

const router = Router();



router.get('/substitutions/:schoolID', ({ params, query }) => {
const { schoolID } = params;
const { stufe, klasse, isStudent } = query;

let result;

if (!SCHOOL_CONFIGS[schoolID]) {
result = SCHOOL_CONFIGS["default"](stufe, klasse, isStudent);
} else {
result = SCHOOL_CONFIGS[schoolID](stufe, klasse, isStudent);
}

return new Response(JSON.stringify(result), {
headers: {
'Content-Type': "application/json",
},
});
});


router.all('*', () => new Response("",{status: 302, headers: {"location": "https://github.com/alessioc42/lanis-mobile-autoconfig"} }));

export default {
fetch: router.handle,
};
16 changes: 16 additions & 0 deletions src/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "template-worker-router",
"version": "0.0.0",
"private": true,
"scripts": {
"deploy": "wrangler deploy index.js",
"dev": "wrangler dev index.js",
"test": "vitest"
},
"dependencies": {
"itty-router": "^2.6.1"
},
"devDependencies": {
"wrangler": "^3.0.0"
}
}
27 changes: 27 additions & 0 deletions src/school_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {defaultSubstitutionsFilterExtractor} from "./defaultparser";


let SCHOOL_CONFIGS = {
"default": (stufe, klasse, isStudent) => {
return defaultSubstitutionsFilterExtractor(stufe, klasse, isStudent)
},
//Configs for Max-Planck-Schule Rüsselsheim
"5182": (stufe, klasse, isStudent) => {
let result = defaultSubstitutionsFilterExtractor(stufe, klasse, isStudent);

if (result.stufe === "Q" || result.stufe === "E") {
switch (result.klasse) {
case "1" || "2":
result.klasse = "1/2";
break;
case "3" || "4":
result.klasse = "3/4";
break;
}
}

return result;
}
}

export default SCHOOL_CONFIGS;
3 changes: 3 additions & 0 deletions src/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name = "lanis-mobile-autoconfig"
main = "index.js"
compatibility_date = "2022-05-03"

0 comments on commit 4952195

Please sign in to comment.