Compare commits

...

6 Commits

Author SHA1 Message Date
Ben Phelps
939f5d7c20 fix path.join import 2022-09-06 08:39:25 +03:00
Ben Phelps
cf39395924 add documentation link 2022-09-05 20:20:34 +03:00
Ben Phelps
6061d9ec65 cleanup imports 2022-09-05 20:19:08 +03:00
Ben Phelps
5a8defb478 allow weather apis to use hidden api keys 2022-09-05 20:14:14 +03:00
Ben Phelps
08afa0b747 check that content type exists before setting it 2022-09-05 10:08:02 +03:00
Ben Phelps
bad436b858 fix jellyfin widget api calls 2022-09-05 08:19:50 +03:00
10 changed files with 67 additions and 15 deletions

View File

@@ -4,9 +4,7 @@ import { BiError } from "react-icons/bi";
import Icon from "./icon";
export default function OpenWeatherMap({ options }) {
const { data, error } = useSWR(
`/api/widgets/openweathermap?lat=${options.latitude}&lon=${options.longitude}&apiKey=${options.apiKey}&duration=${options.cache}&units=${options.units}`
);
const { data, error } = useSWR(`/api/widgets/openweathermap?${new URLSearchParams(options).toString()}`);
if (error || data?.cod == 401) {
return (

View File

@@ -4,9 +4,8 @@ import { BiError } from "react-icons/bi";
import Icon from "./icon";
export default function WeatherApi({ options }) {
const { data, error } = useSWR(
`/api/widgets/weather?lat=${options.latitude}&lon=${options.longitude}&apiKey=${options.apiKey}&duration=${options.cache}`
);
console.log(options);
const { data, error } = useSWR(`/api/widgets/weather?${new URLSearchParams(options).toString()}`);
if (error) {
return (

View File

@@ -7,6 +7,7 @@ import npmProxyHandler from "utils/proxies/npm";
const serviceProxyHandlers = {
// uses query param auth
emby: genericProxyHandler,
jellyfin: genericProxyHandler,
pihole: genericProxyHandler,
radarr: genericProxyHandler,
sonarr: genericProxyHandler,

View File

@@ -1,9 +1,28 @@
import cachedFetch from "utils/cached-fetch";
import { getSettings } from "utils/config";
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));
}

View File

@@ -1,9 +1,28 @@
import cachedFetch from "utils/cached-fetch";
import { getSettings } from "utils/config";
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));
}

View File

@@ -0,0 +1,6 @@
# For configuration options and examples, please see:
# https://github.com/benphelps/homepage/wiki/Settings
providers:
openweathermap: openweathermapapikey
weatherapi: weatherapiapikey

View File

@@ -1,5 +1,6 @@
const formats = {
emby: `{url}/emby/{endpoint}?api_key={key}`,
jellyfin: `{url}/emby/{endpoint}?api_key={key}`,
pihole: `{url}/admin/{endpoint}`,
radarr: `{url}/api/v3/{endpoint}?apikey={key}`,
sonarr: `{url}/api/v3/{endpoint}?apikey={key}`,

View File

@@ -1,5 +1,6 @@
import { join } from "path";
import { existsSync, copyFile } from "fs";
import { existsSync, copyFile, promises as fs } from "fs";
import yaml from "js-yaml";
export default function checkAndCopyConfig(config) {
const configYaml = join(process.cwd(), "config", config);
@@ -14,3 +15,11 @@ export default function checkAndCopyConfig(config) {
});
}
}
export async function getSettings() {
checkAndCopyConfig("settings.yaml");
const settingsYaml = join(process.cwd(), "config", "settings.yaml");
const fileContents = await fs.readFile(settingsYaml, "utf8");
return yaml.load(fileContents);
}

View File

@@ -19,7 +19,7 @@ export default async function credentialedProxyHandler(req, res) {
},
});
res.setHeader("Content-Type", contentType);
if (contentType) res.setHeader("Content-Type", contentType);
return res.status(status).send(data);
}
}

View File

@@ -12,7 +12,7 @@ export default async function genericProxyHandler(req, res) {
const url = new URL(formatApiCall(widget.type, { endpoint, ...widget }));
const [status, contentType, data] = await httpProxy(url);
res.setHeader("Content-Type", contentType);
if (contentType) res.setHeader("Content-Type", contentType);
return res.status(status).send(data);
}
}