mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: init new self-hosting method
This commit is contained in:
parent
d5791d99d5
commit
d1fde17220
11 changed files with 663 additions and 492 deletions
|
|
@ -1,9 +1,10 @@
|
|||
import random
|
||||
import string
|
||||
from typing import Annotated
|
||||
from pydantic import EmailStr
|
||||
from sqlalchemy import create_engine
|
||||
from sqlmodel import SQLModel, Session
|
||||
import typer
|
||||
|
||||
from config.config import get_learnhouse_config
|
||||
from src.db.organizations import OrganizationCreate
|
||||
from src.db.users import UserCreate
|
||||
|
|
@ -15,6 +16,10 @@ from src.services.install.install import (
|
|||
|
||||
cli = typer.Typer()
|
||||
|
||||
def generate_password(length):
|
||||
characters = string.ascii_uppercase + string.ascii_lowercase + string.digits
|
||||
password = ''.join(random.choice(characters) for _ in range(length))
|
||||
return password
|
||||
|
||||
@cli.command()
|
||||
def install(
|
||||
|
|
@ -49,8 +54,10 @@ def install(
|
|||
|
||||
# Create Organization User
|
||||
print("Creating default organization user...")
|
||||
# Generate random 6 digit password
|
||||
password = generate_password(8)
|
||||
user = UserCreate(
|
||||
username="admin", email=EmailStr("admin@school.io"), password="adminsecret"
|
||||
username="admin", email=EmailStr("admin@school.dev"), password=password
|
||||
)
|
||||
install_create_organization_user(user, "default", db_session)
|
||||
print("Default organization user created ✅")
|
||||
|
|
@ -60,7 +67,8 @@ def install(
|
|||
print("")
|
||||
print("Login with the following credentials:")
|
||||
print("email: admin@school.io")
|
||||
print("password: adminsecret")
|
||||
print("password: " + password)
|
||||
print("⚠️ Remember to change the password after logging in ⚠️")
|
||||
|
||||
else:
|
||||
# Install the default elements
|
||||
|
|
|
|||
948
apps/api/poetry.lock
generated
948
apps/api/poetry.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -12,7 +12,6 @@ version = "0.1.0"
|
|||
[tool.poetry.dependencies]
|
||||
boto3 = "^1.34.79"
|
||||
botocore = "^1.34.84"
|
||||
chromadb = "^0.4.22"
|
||||
faker = "^24.9.0"
|
||||
fastapi = "^0.110.1"
|
||||
fastapi-jwt-auth = "^0.5.0"
|
||||
|
|
@ -38,6 +37,7 @@ sqlmodel = "^0.0.16"
|
|||
tiktoken = "^0.6.0"
|
||||
uvicorn = "0.29.0"
|
||||
typer = "^0.12.3"
|
||||
chromadb = "^0.4.24"
|
||||
|
||||
[build-system]
|
||||
build-backend = "poetry.core.masonry.api"
|
||||
|
|
|
|||
37
apps/api/src/core/events/autoinstall.py
Normal file
37
apps/api/src/core/events/autoinstall.py
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
from sqlalchemy import create_engine
|
||||
from sqlmodel import SQLModel, Session, select
|
||||
|
||||
from cli import install
|
||||
from config.config import get_learnhouse_config
|
||||
from src.db.organizations import Organization
|
||||
|
||||
|
||||
def auto_install():
|
||||
# Get the database session
|
||||
learnhouse_config = get_learnhouse_config()
|
||||
engine = create_engine(
|
||||
learnhouse_config.database_config.sql_connection_string, echo=False, pool_pre_ping=True # type: ignore
|
||||
)
|
||||
SQLModel.metadata.create_all(engine)
|
||||
|
||||
db_session = Session(engine)
|
||||
|
||||
orgs = db_session.exec(select(Organization)).all()
|
||||
|
||||
if len(orgs) == 0:
|
||||
print("No organizations found. Starting auto-installation 🏗️")
|
||||
install(short=True)
|
||||
|
||||
if orgs:
|
||||
for org in orgs:
|
||||
default_org = db_session.exec(select(Organization).where(Organization.slug == 'default')).first()
|
||||
|
||||
if not default_org:
|
||||
print("No default organization found. Starting auto-installation 🏗️")
|
||||
install(short=True)
|
||||
|
||||
else:
|
||||
print("Organizations found. Skipping auto-installation 🚀")
|
||||
|
||||
|
||||
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
from typing import Callable
|
||||
from fastapi import FastAPI
|
||||
from config.config import LearnHouseConfig, get_learnhouse_config
|
||||
from src.core.events.autoinstall import auto_install
|
||||
from src.core.events.content import check_content_directory
|
||||
from src.core.events.database import close_database, connect_to_db
|
||||
from src.core.events.logs import create_logs_dir
|
||||
|
|
@ -25,6 +26,9 @@ def startup_app(app: FastAPI) -> Callable:
|
|||
# Create content directory
|
||||
await check_content_directory()
|
||||
|
||||
# Check if auto-installation is needed
|
||||
auto_install()
|
||||
|
||||
return start_app
|
||||
|
||||
|
||||
|
|
|
|||
19
apps/web/app/api/revalidate/route.ts
Normal file
19
apps/web/app/api/revalidate/route.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { revalidateTag } from 'next/cache'
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const tag: any = request.nextUrl.searchParams.get('tag')
|
||||
revalidateTag(tag)
|
||||
|
||||
return NextResponse.json(
|
||||
{ revalidated: true, now: Date.now(), tag },
|
||||
{
|
||||
status: 200,
|
||||
headers: {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
|
||||
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
||||
},
|
||||
}
|
||||
)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue