main.js 1.4 KB

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