Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ab1b49a

Browse files
committed
load default stream from config table
1 parent 454a096 commit ab1b49a

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed

api/simpleradio/config.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#
2+
3+
from functools import reduce
4+
import json
5+
6+
from fastapi import APIRouter, Depends, HTTPException
7+
from pydantic import BaseModel
8+
9+
from .db import crud
10+
from .util import get_short_hash
11+
12+
router = APIRouter(
13+
prefix="/config",
14+
responses={404: {"description": "Not found"}},
15+
)
16+
17+
18+
@router.get("/")
19+
async def get_config(conn=Depends(crud.db_conn)):
20+
21+
cursor = await conn.execute("""
22+
SELECT * FROM config
23+
""")
24+
25+
rows = await cursor.fetchall()
26+
27+
config = reduce(lambda acc, row: {**acc, **{ row['name']: row['value'] }}, rows, {})
28+
29+
return {
30+
"config": config
31+
}
32+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
import json
3+
4+
import simpleradio.db.crud as crud
5+
import simpleradio.util as util
6+
7+
async def execute(conn):
8+
9+
await conn.execute("""
10+
CREATE TABLE config (
11+
name VARCHAR PRIMARY KEY,
12+
value VARCHAR
13+
)
14+
""")
15+
16+
await conn.execute("""
17+
INSERT INTO config (name, value) VALUES ('default_stream_list_id', ?)
18+
""", ('3939c14eb6',))

api/simpleradio/webapp.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11

22
from fastapi import APIRouter, FastAPI
33
from . import hashed_content
4+
from . import config
45

56
v1 = FastAPI()
67

78
v1.include_router(hashed_content.router)
9+
v1.include_router(config.router)
810

911
app = FastAPI()
1012

src/components/AppRoot.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
1+
import { useEffect, useState } from "react";
12
import { Navigate } from "react-router-dom";
23

34
function AppRoot() {
45

56
console.log("rendering AppRoot")
67

8+
const [ streamListId, setStreamListId ] = useState(null)
9+
const [ error, setError ] = useState(null)
10+
11+
useEffect(() => {
12+
fetch(`/api/v1/config/`)
13+
.then(res => res.json())
14+
.then((result) => {
15+
setStreamListId(result.config.default_stream_list_id)
16+
})
17+
.catch((error) => {
18+
setError("Error loading config: " + error)
19+
})
20+
})
21+
722
return (
8-
<Navigate to="/streamlist/3939c14eb6" />
23+
<>
24+
{streamListId && (
25+
<Navigate to={`/streamlist/${streamListId}`} />
26+
)}
27+
{error && (
28+
<p>Couldn't load application configuration, backend is probably not running</p>
29+
)}
30+
</>
931
)
1032
}
1133

0 commit comments

Comments
 (0)