0
Fork 0
mirror of https://github.com/stonith404/pingvin-share.git synced 2025-01-15 01:14:27 -05:00

feat: configure ports, db url and api url with env variables

This commit is contained in:
Elias Schneider 2023-04-25 23:39:57 +02:00
parent b33c1d7f4b
commit e5071cba12
No known key found for this signature in database
GPG key ID: 07E623B294202B6C
6 changed files with 17 additions and 8 deletions

View file

@ -30,6 +30,6 @@ async function bootstrap() {
SwaggerModule.setup("api/swagger", app, document); SwaggerModule.setup("api/swagger", app, document);
} }
await app.listen(8080); await app.listen(process.env.PORT || 8080);
} }
bootstrap(); bootstrap();

View file

@ -7,7 +7,9 @@ export class PrismaService extends PrismaClient {
super({ super({
datasources: { datasources: {
db: { db: {
url: "file:../data/pingvin-share.db?connection_limit=1", url:
process.env.DATABASE_URL ||
"file:../data/pingvin-share.db?connection_limit=1",
}, },
}, },
}); });

View file

@ -19,4 +19,7 @@ module.exports = withPWA({
output: "standalone", env: { output: "standalone", env: {
VERSION: version, VERSION: version,
}, },
serverRuntimeConfig: {
apiURL: process.env.API_URL ?? 'http://localhost:8080',
},
}); });

View file

@ -22,7 +22,7 @@ export async function middleware(request: NextRequest) {
// Get config from backend // Get config from backend
const config = await ( const config = await (
await fetch("http://localhost:8080/api/configs") await fetch(`${request.nextUrl.origin}/api/configs`)
).json(); ).json();
const getConfig = (key: string) => { const getConfig = (key: string) => {

View file

@ -11,6 +11,7 @@ import axios from "axios";
import { getCookie, setCookie } from "cookies-next"; import { getCookie, setCookie } from "cookies-next";
import { GetServerSidePropsContext } from "next"; import { GetServerSidePropsContext } from "next";
import type { AppProps } from "next/app"; import type { AppProps } from "next/app";
import getConfig from "next/config";
import { useRouter } from "next/router"; import { useRouter } from "next/router";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import Header from "../components/header/Header"; import Header from "../components/header/Header";
@ -117,6 +118,8 @@ function App({ Component, pageProps }: AppProps) {
// Fetch user and config variables on server side when the first request is made // Fetch user and config variables on server side when the first request is made
// These will get passed as a page prop to the App component and stored in the contexts // These will get passed as a page prop to the App component and stored in the contexts
App.getInitialProps = async ({ ctx }: { ctx: GetServerSidePropsContext }) => { App.getInitialProps = async ({ ctx }: { ctx: GetServerSidePropsContext }) => {
const { apiURL } = getConfig().serverRuntimeConfig;
let pageProps: { let pageProps: {
user?: CurrentUser; user?: CurrentUser;
configVariables?: Config[]; configVariables?: Config[];
@ -130,15 +133,13 @@ App.getInitialProps = async ({ ctx }: { ctx: GetServerSidePropsContext }) => {
if (ctx.req) { if (ctx.req) {
const cookieHeader = ctx.req.headers.cookie; const cookieHeader = ctx.req.headers.cookie;
pageProps.user = await axios(`http://localhost:8080/api/users/me`, { pageProps.user = await axios(`${apiURL}/api/users/me`, {
headers: { cookie: cookieHeader }, headers: { cookie: cookieHeader },
}) })
.then((res) => res.data) .then((res) => res.data)
.catch(() => null); .catch(() => null);
pageProps.configVariables = ( pageProps.configVariables = (await axios(`${apiURL}/api/configs`)).data;
await axios(`http://localhost:8080/api/configs`)
).data;
pageProps.route = ctx.req.url; pageProps.route = ctx.req.url;
} }

View file

@ -1,5 +1,6 @@
import { NextApiRequest, NextApiResponse } from "next"; import { NextApiRequest, NextApiResponse } from "next";
import httpProxyMiddleware from "next-http-proxy-middleware"; import httpProxyMiddleware from "next-http-proxy-middleware";
import getConfig from "next/config";
export const config = { export const config = {
api: { api: {
@ -8,11 +9,13 @@ export const config = {
}, },
}; };
const { apiURL } = getConfig().serverRuntimeConfig;
export default (req: NextApiRequest, res: NextApiResponse) => { export default (req: NextApiRequest, res: NextApiResponse) => {
return httpProxyMiddleware(req, res, { return httpProxyMiddleware(req, res, {
headers: { headers: {
"X-Forwarded-For": req.socket?.remoteAddress ?? "", "X-Forwarded-For": req.socket?.remoteAddress ?? "",
}, },
target: "http://localhost:8080", target: apiURL,
}); });
}; };