mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: add support for selfHosted client-side
This commit is contained in:
parent
316ab4025d
commit
27d2bc08f2
4 changed files with 18 additions and 14 deletions
|
|
@ -7,6 +7,7 @@ hosting_config:
|
||||||
port: 443
|
port: 443
|
||||||
ssl: true
|
ssl: true
|
||||||
use_default_org: false
|
use_default_org: false
|
||||||
|
default_org: learnhouse
|
||||||
allowed_origins:
|
allowed_origins:
|
||||||
- https://learnhouse.app
|
- https://learnhouse.app
|
||||||
- https://learnhouse.io
|
- https://learnhouse.io
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ import { useRouter } from "next/navigation";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import styled from "styled-components";
|
import styled from "styled-components";
|
||||||
import { Title } from "@components/UI/Elements/Styles/Title";
|
import { Title } from "@components/UI/Elements/Styles/Title";
|
||||||
import { getAPIUrl, getBackendUrl, getUriWithOrg } from "@services/config/config";
|
import { getAPIUrl, getBackendUrl, getSelfHostedOption, getUriWithOrg } from "@services/config/config";
|
||||||
import { deleteCourseFromBackend } from "@services/courses/courses";
|
import { deleteCourseFromBackend } from "@services/courses/courses";
|
||||||
import useSWR, { mutate } from "swr";
|
import useSWR, { mutate } from "swr";
|
||||||
import { swrFetcher } from "@services/utils/requests";
|
import { swrFetcher } from "@services/utils/requests";
|
||||||
|
|
@ -25,7 +25,7 @@ const CoursesIndexPage = (params: any) => {
|
||||||
function removeCoursePrefix(course_id: string) {
|
function removeCoursePrefix(course_id: string) {
|
||||||
return course_id.replace("course_", "");
|
return course_id.replace("course_", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Title>
|
<Title>
|
||||||
|
|
@ -44,8 +44,8 @@ const CoursesIndexPage = (params: any) => {
|
||||||
<button style={{ backgroundColor: "red", border: "none" }} onClick={() => deleteCourses(course.course_id)}>
|
<button style={{ backgroundColor: "red", border: "none" }} onClick={() => deleteCourses(course.course_id)}>
|
||||||
Delete <Trash size={10}></Trash>
|
Delete <Trash size={10}></Trash>
|
||||||
</button>
|
</button>
|
||||||
<Link href={getUriWithOrg(orgslug,"") + "/course/" + removeCoursePrefix(course.course_id)}>
|
<Link href={getUriWithOrg(orgslug, "/course/" + removeCoursePrefix(course.course_id))}>
|
||||||
<Link href={getUriWithOrg(orgslug,"") + "/course/" + removeCoursePrefix(course.course_id) + "/edit"}>
|
<Link href={getUriWithOrg(orgslug, "/course/" + removeCoursePrefix(course.course_id) + "/edit")}>
|
||||||
<button>
|
<button>
|
||||||
Edit <Edit2 size={10}></Edit2>
|
Edit <Edit2 size={10}></Edit2>
|
||||||
</button>
|
</button>
|
||||||
|
|
|
||||||
|
|
@ -20,8 +20,8 @@ export default function middleware(req: NextRequest) {
|
||||||
const isSelfHosted = getSelfHostedOption();
|
const isSelfHosted = getSelfHostedOption();
|
||||||
const hostname = req.headers.get("host") || "learnhouse.app";
|
const hostname = req.headers.get("host") || "learnhouse.app";
|
||||||
let currentHost = hostname.replace(".localhost:3000", "");
|
let currentHost = hostname.replace(".localhost:3000", "");
|
||||||
|
|
||||||
if (!isSelfHosted && currentHost === ("localhost:3000")) {
|
if (!isSelfHosted && currentHost === "localhost:3000" && !url.pathname.startsWith("/organizations")) {
|
||||||
// Redirect to error page if not self-hosted and on localhost
|
// Redirect to error page if not self-hosted and on localhost
|
||||||
const errorUrl = "/error";
|
const errorUrl = "/error";
|
||||||
return NextResponse.redirect(errorUrl, { status: 302 });
|
return NextResponse.redirect(errorUrl, { status: 302 });
|
||||||
|
|
@ -33,7 +33,11 @@ export default function middleware(req: NextRequest) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (url.pathname.startsWith("/organizations")) {
|
if (url.pathname.startsWith("/organizations")) {
|
||||||
|
if (!isSelfHosted) {
|
||||||
|
currentHost = "";
|
||||||
|
}
|
||||||
url.pathname = url.pathname.replace("/organizations", `/organizations${currentHost}`).replace("localhost:3000", "");
|
url.pathname = url.pathname.replace("/organizations", `/organizations${currentHost}`).replace("localhost:3000", "");
|
||||||
|
|
||||||
return NextResponse.rewrite(url);
|
return NextResponse.rewrite(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,12 @@ const LEARNHOUSE_API_URL = "http://localhost:1338/api/";
|
||||||
const LEARNHOUSE_BACKEND_URL = "http://localhost:1338/";
|
const LEARNHOUSE_BACKEND_URL = "http://localhost:1338/";
|
||||||
|
|
||||||
export const getAPIUrl = () => LEARNHOUSE_API_URL;
|
export const getAPIUrl = () => LEARNHOUSE_API_URL;
|
||||||
|
|
||||||
export const getBackendUrl = () => LEARNHOUSE_BACKEND_URL;
|
export const getBackendUrl = () => LEARNHOUSE_BACKEND_URL;
|
||||||
|
export const getSelfHostedOption = () => (process.env.NEXT_PUBLIC_LEARNHOUSE_SELF_HOSTED === "true" ? true : false);
|
||||||
export const getSelfHostedOption = () => false;
|
|
||||||
|
|
||||||
export const getUriWithOrg = (orgslug: string, path: string) => {
|
export const getUriWithOrg = (orgslug: string, path: string) => {
|
||||||
const selfHosted = getSelfHostedOption();
|
const selfHosted = getSelfHostedOption();
|
||||||
|
|
||||||
if (selfHosted) {
|
if (selfHosted) {
|
||||||
return `http://localhost:3000${path}`;
|
return `http://localhost:3000${path}`;
|
||||||
}
|
}
|
||||||
|
|
@ -20,16 +19,16 @@ export const getOrgFromUri = () => {
|
||||||
if (selfHosted) {
|
if (selfHosted) {
|
||||||
getDefaultOrg();
|
getDefaultOrg();
|
||||||
} else {
|
} else {
|
||||||
const hostname = window.location.hostname;
|
if (typeof window !== "undefined") {
|
||||||
// get the orgslug from the hostname
|
const hostname = window.location.hostname;
|
||||||
const orgslug = hostname.split(".")[0];
|
return hostname.replace(".localhost:3000", "");
|
||||||
return orgslug;
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getDefaultOrg = () => {
|
export const getDefaultOrg = () => {
|
||||||
const selfHosted = getSelfHostedOption();
|
const selfHosted = getSelfHostedOption();
|
||||||
if (selfHosted) {
|
if (selfHosted) {
|
||||||
return "test";
|
return process.env.NEXT_PUBLIC_LEARNHOUSE_DEFAULT_ORG;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue