Skip to content

Commit

Permalink
refactor project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
marnym committed Apr 25, 2024
1 parent 36b78e4 commit 8ff81dc
Show file tree
Hide file tree
Showing 18 changed files with 300 additions and 213 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ A static site to be displayed on info-screens around [Teknologföreningen](https

## Environmental variables

Required environmental variables can be found in `main.ts`.
See `.env.example`

# Running

The server can be ran with the following command:
## Development

`deno run --allow-env --allow-read --allow-net main.ts`
The development server can be run with the following command:

`deno task dev`
52 changes: 52 additions & 0 deletions dagsen/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { render } from "../render.ts";
import * as dagsen from "./api.ts";

export async function dagsenHandler(params: URLSearchParams): Promise<Response> {
let lang = params.get("lang") as dagsen.Language | null;

if (lang !== null) {
const renderData = await fetchRenderData(lang);
return render("/dagsen/menu", renderData);
}

lang = lang ?? "sv";
if (!dagsen.LANGUAGES.includes(lang)) {
lang = "sv";
}

const renderData = await fetchRenderData(lang);
return render("/dagsen/index", renderData);
}

type RenderData = {
date: string;
menuItems: string[];
openTime: string;
nextPage: string;
pageTimeout: string;
};

async function fetchRenderData(language: dagsen.Language): Promise<RenderData> {
const now = new Date();

const date = now.toLocaleDateString("en-GB", { year: "2-digit", month: "2-digit", "day": "2-digit" })
.split("/")
.slice(0, 2)
.join("/");

const menuItems = (await dagsen.fetchMenuText(language))?.split("\r\n") ?? [];

return {
date,
menuItems: menuItems,
openTime: dagsen.openTime(now),
nextPage: nextDagsenPage(dagsen.LANGUAGES, language),
pageTimeout: "5s",
};
}

function nextDagsenPage(languages: readonly dagsen.Language[], currentLanguage: dagsen.Language): string {
const languagesLength = languages.length;
const nextIndex = languages.indexOf(currentLanguage) + 1;
return `/dagsen?lang=${languages[nextIndex % languagesLength]}`;
}
23 changes: 14 additions & 9 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
{
"deno.enable": true,
"deno.lint": true,
"deno.unstable": true,
"fmt": {
"lineWidth": 120
},
"tasks": {
"dev": "deno run --watch --allow-read --allow-env --allow-net main.ts"
}
"deno.enable": true,
"deno.lint": true,
"deno.unstable": true,
"fmt": {
"lineWidth": 120
},
"tasks": {
"dev": "deno run --watch --allow-read --allow-env --allow-net main.ts"
},
"imports": {
"@std/http": "https://deno.land/[email protected]/http/mod.ts",
"@std/dotenv": "https://deno.land/[email protected]/dotenv/load.ts",
"eta": "https://deno.land/x/[email protected]/src/index.ts",
}
}
71 changes: 71 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export type Env = {
camUrl: string;
ylonzDate: Date;
votesUrl: string;
pageTimeout: string;
refreshTime: string;
};

const {
CAM_URL,
YLONZ_DATE,
VOTES_URL,
PAGE_TIMEOUT = "10s",
REFRESH_TIME = "04:00",
} = Deno.env.toObject();

export const env: Env = {
camUrl: CAM_URL,
ylonzDate: new Date(YLONZ_DATE),
votesUrl: VOTES_URL,
pageTimeout: PAGE_TIMEOUT,
refreshTime: REFRESH_TIME,
};
Loading

0 comments on commit 8ff81dc

Please sign in to comment.