mirror of
https://github.com/karl0ss/homepage.git
synced 2025-04-29 12:03:41 +01:00
allow weather apis to use hidden api keys
This commit is contained in:
parent
08afa0b747
commit
5a8defb478
@ -4,9 +4,7 @@ import { BiError } from "react-icons/bi";
|
|||||||
import Icon from "./icon";
|
import Icon from "./icon";
|
||||||
|
|
||||||
export default function OpenWeatherMap({ options }) {
|
export default function OpenWeatherMap({ options }) {
|
||||||
const { data, error } = useSWR(
|
const { data, error } = useSWR(`/api/widgets/openweathermap?${new URLSearchParams(options).toString()}`);
|
||||||
`/api/widgets/openweathermap?lat=${options.latitude}&lon=${options.longitude}&apiKey=${options.apiKey}&duration=${options.cache}&units=${options.units}`
|
|
||||||
);
|
|
||||||
|
|
||||||
if (error || data?.cod == 401) {
|
if (error || data?.cod == 401) {
|
||||||
return (
|
return (
|
||||||
|
@ -4,9 +4,8 @@ import { BiError } from "react-icons/bi";
|
|||||||
import Icon from "./icon";
|
import Icon from "./icon";
|
||||||
|
|
||||||
export default function WeatherApi({ options }) {
|
export default function WeatherApi({ options }) {
|
||||||
const { data, error } = useSWR(
|
console.log(options);
|
||||||
`/api/widgets/weather?lat=${options.latitude}&lon=${options.longitude}&apiKey=${options.apiKey}&duration=${options.cache}`
|
const { data, error } = useSWR(`/api/widgets/weather?${new URLSearchParams(options).toString()}`);
|
||||||
);
|
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
return (
|
return (
|
||||||
|
@ -1,9 +1,28 @@
|
|||||||
import cachedFetch from "utils/cached-fetch";
|
import cachedFetch from "utils/cached-fetch";
|
||||||
|
import { getSettings } from "utils/config";
|
||||||
|
|
||||||
export default async function handler(req, res) {
|
export default async function handler(req, res) {
|
||||||
const { lat, lon, apiKey, duration, units } = req.query;
|
const { latitude, longitude, units, provider, cache } = req.query;
|
||||||
|
let { apiKey } = req.query;
|
||||||
|
|
||||||
const api_url = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${apiKey}&units=${units}`;
|
if (!apiKey && !provider) {
|
||||||
|
return res.status(400).json({ error: "Missing API key or provider" });
|
||||||
|
}
|
||||||
|
|
||||||
res.send(await cachedFetch(api_url, duration));
|
if (!apiKey && provider !== "openweathermap") {
|
||||||
|
return res.status(400).json({ error: "Invalid provider for endpoint" });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!apiKey && provider) {
|
||||||
|
const settings = await getSettings();
|
||||||
|
apiKey = settings?.providers?.openweathermap;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!apiKey) {
|
||||||
|
return res.status(400).json({ error: "Missing API key" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const api_url = `https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=${apiKey}&units=${units}`;
|
||||||
|
|
||||||
|
res.send(await cachedFetch(api_url, cache));
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,28 @@
|
|||||||
import cachedFetch from "utils/cached-fetch";
|
import cachedFetch from "utils/cached-fetch";
|
||||||
|
import { getSettings } from "utils/config";
|
||||||
|
|
||||||
export default async function handler(req, res) {
|
export default async function handler(req, res) {
|
||||||
const { lat, lon, apiKey, duration } = req.query;
|
const { latitude, longitude, provider, cache } = req.query;
|
||||||
|
let { apiKey } = req.query;
|
||||||
|
|
||||||
const api_url = `http://api.weatherapi.com/v1/current.json?q=${lat},${lon}&key=${apiKey}`;
|
if (!apiKey && !provider) {
|
||||||
|
return res.status(400).json({ error: "Missing API key or provider" });
|
||||||
|
}
|
||||||
|
|
||||||
res.send(await cachedFetch(api_url, duration));
|
if (!apiKey && provider !== "weatherapi") {
|
||||||
|
return res.status(400).json({ error: "Invalid provider for endpoint" });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!apiKey && provider) {
|
||||||
|
const settings = await getSettings();
|
||||||
|
apiKey = settings?.providers?.weatherapi;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!apiKey) {
|
||||||
|
return res.status(400).json({ error: "Missing API key" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const api_url = `http://api.weatherapi.com/v1/current.json?q=${latitude},${longitude}&key=${apiKey}`;
|
||||||
|
|
||||||
|
res.send(await cachedFetch(api_url, cache));
|
||||||
}
|
}
|
||||||
|
3
src/skeleton/settings.yaml
Normal file
3
src/skeleton/settings.yaml
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
providers:
|
||||||
|
openweathermap: openweathermapapikey
|
||||||
|
weatherapi: weatherapiapikey
|
@ -1,5 +1,8 @@
|
|||||||
import { join } from "path";
|
import { join } from "path";
|
||||||
import { existsSync, copyFile } from "fs";
|
import { existsSync, copyFile } from "fs";
|
||||||
|
import { promises as fs } from "fs";
|
||||||
|
import path from "path";
|
||||||
|
import yaml from "js-yaml";
|
||||||
|
|
||||||
export default function checkAndCopyConfig(config) {
|
export default function checkAndCopyConfig(config) {
|
||||||
const configYaml = join(process.cwd(), "config", config);
|
const configYaml = join(process.cwd(), "config", config);
|
||||||
@ -14,3 +17,11 @@ export default function checkAndCopyConfig(config) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getSettings() {
|
||||||
|
checkAndCopyConfig("settings.yaml");
|
||||||
|
|
||||||
|
const settingsYaml = path.join(process.cwd(), "config", "settings.yaml");
|
||||||
|
const fileContents = await fs.readFile(settingsYaml, "utf8");
|
||||||
|
return yaml.load(fileContents);
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user