main.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { Elysia } from 'elysia'
  2. import { Eta } from "eta"
  3. const eta = new Eta({ views: "./templates" })
  4. import Meteostanica from '../../utils/meteostanica'
  5. import editEnvVariable from '../../utils/editEnvVariable'
  6. export default (langName, lang) => new Elysia()
  7. .get('/', ({ set }) => {
  8. const data = Meteostanica.getData()
  9. set.headers['content-type'] = 'text/html; charset=utf8'
  10. return eta.render(`${langName}/index`, { data })
  11. })
  12. .get('/settings', ({ set }) => {
  13. set.headers['content-type'] = 'text/html; charset=utf8'
  14. return eta.render(`${langName}/settings`, { postDataEnabled: process.env.POST_DATA_ENABLED, cloudURL: process.env.BACKEND_URL })
  15. })
  16. .post('/settings', async ({ body, set, redirect }) => {
  17. const postDataEnabled = body?.postDataEnabled
  18. const cloudURL = body?.cloudURL
  19. postDataEnabled?.replaceAll(`"`, ``)
  20. cloudURL?.replaceAll(`"`, ``)
  21. if (!cloudURL) {
  22. set.headers['content-type'] = 'text/html; charset=utf8'
  23. return eta.render(`${langName}/settings`, { lang, error: `noCloudURL` })
  24. }
  25. await editEnvVariable(`POST_DATA_ENABLED`, postDataEnabled ?? "false")
  26. await editEnvVariable(`BACKEND_URL`, cloudURL)
  27. return redirect(`${langName === `sk` ? `/` : `/en`}`)
  28. })
  29. .get('/currentData', () => {
  30. const data = Meteostanica.getData()
  31. return data?.[0]
  32. })