mirror of
https://github.com/Radiquum/YAMPD.git
synced 2025-05-20 07:39:35 +05:00
refactor: api routes
This commit is contained in:
parent
c74170a14d
commit
802b755d29
6 changed files with 95 additions and 55 deletions
|
@ -1,22 +1,45 @@
|
||||||
const API = process.env.NEXT_PUBLIC_API_URL || "/api";
|
const API = process.env.NEXT_PUBLIC_API_URL || "/api";
|
||||||
|
|
||||||
export const ENDPOINTS = {
|
type _PACK_ENDPOINT = {
|
||||||
createPack: `${API}/pack/new`,
|
getPack: string;
|
||||||
getAllPacks: `${API}/pack/all`,
|
|
||||||
};
|
|
||||||
|
|
||||||
type PACK_IMG_ENDPOINTS = {
|
|
||||||
getPackImage: string;
|
getPackImage: string;
|
||||||
editPackImage: string;
|
editPackImage: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const PACK_IMG_ENDPOINTS = function (
|
type _PACKS_ENDPOINT = {
|
||||||
endpoint: keyof PACK_IMG_ENDPOINTS,
|
getPacks: string;
|
||||||
id: string
|
createPack: string;
|
||||||
) {
|
deletePack: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const PACK_ENDPOINT = (endpoint: keyof _PACK_ENDPOINT, id: string) => {
|
||||||
|
if (!id) {
|
||||||
|
console.error(`ENDPOINT "${endpoint}" REQUIRES A PACK ID`);
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
const _endpoints = {
|
const _endpoints = {
|
||||||
|
getPack: `${API}/pack/${id}`,
|
||||||
getPackImage: `${API}/pack/${id}/image`,
|
getPackImage: `${API}/pack/${id}/image`,
|
||||||
editPackImage: `${API}/pack/${id}/image/edit`,
|
editPackImage: `${API}/pack/${id}/image/edit`,
|
||||||
};
|
};
|
||||||
return _endpoints[endpoint];
|
return _endpoints[endpoint];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const PACKS_ENDPOINT = (
|
||||||
|
endpoint: keyof _PACKS_ENDPOINT,
|
||||||
|
id?: string | null
|
||||||
|
) => {
|
||||||
|
const requireID: string[] = ["deletePack"];
|
||||||
|
if (requireID.includes(endpoint) && !id) {
|
||||||
|
console.error(`ENDPOINT "${endpoint}" REQUIRES A PACK ID`);
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
const _endpoints = {
|
||||||
|
getPacks: `${API}/packs/all`,
|
||||||
|
createPack: `${API}/packs/new`,
|
||||||
|
deletePack: `${API}/packs/${id}/delete`,
|
||||||
|
};
|
||||||
|
return _endpoints[endpoint];
|
||||||
|
};
|
||||||
|
|
|
@ -8,7 +8,7 @@ import { Button } from "flowbite-react";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
|
|
||||||
import mc from "../../../api/mc_version.json";
|
import mc from "../../../api/mc_version.json";
|
||||||
import { ENDPOINTS, PACK_IMG_ENDPOINTS } from "@/api/ENDPOINTS";
|
import { PACKS_ENDPOINT, PACK_ENDPOINT } from "@/api/ENDPOINTS";
|
||||||
import { toast } from "react-toastify";
|
import { toast } from "react-toastify";
|
||||||
const mcr = mc.reverse();
|
const mcr = mc.reverse();
|
||||||
|
|
||||||
|
@ -52,7 +52,7 @@ export default function PackNew() {
|
||||||
async function _submit() {
|
async function _submit() {
|
||||||
const tid = toast.loading(`Creating Pack "${packInfo.title}"`)
|
const tid = toast.loading(`Creating Pack "${packInfo.title}"`)
|
||||||
|
|
||||||
const res = await fetch(`${ENDPOINTS.createPack}`, {
|
const res = await fetch(PACKS_ENDPOINT("createPack"), {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify(packInfo),
|
body: JSON.stringify(packInfo),
|
||||||
headers: {
|
headers: {
|
||||||
|
@ -68,7 +68,7 @@ export default function PackNew() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (image) {
|
if (image) {
|
||||||
await fetch(`${PACK_IMG_ENDPOINTS("editPackImage", data.id)}`, {
|
await fetch(`${PACK_ENDPOINT("editPackImage", data.id)}`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
image: image,
|
image: image,
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
from flask import Blueprint
|
from flask import Blueprint
|
||||||
|
|
||||||
api = Blueprint("api", __name__, url_prefix="/api/pack")
|
apiPack = Blueprint("pack", __name__, url_prefix="/api/pack")
|
||||||
|
apiPacks = Blueprint("packs", __name__, url_prefix="/api/packs")
|
||||||
|
|
||||||
from . import pack
|
from . import pack
|
||||||
|
from . import packs
|
||||||
|
|
|
@ -1,53 +1,23 @@
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
from . import api
|
from . import apiPack
|
||||||
from flask import request, jsonify
|
from flask import request, jsonify, send_file, redirect
|
||||||
from config import PACKS_FOLDER, IMG_ALLOWED_MIME
|
from config import PACKS_FOLDER, IMG_ALLOWED_MIME
|
||||||
import json
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
import base64
|
import base64
|
||||||
|
|
||||||
|
|
||||||
@api.route("/new", methods=["POST"])
|
@apiPack.route("/<id>/image", methods=["GET"])
|
||||||
def APIPackNew():
|
def getPackImage(id):
|
||||||
pack = {
|
if not os.path.exists(f"{PACKS_FOLDER}/{id}/packicon.png"):
|
||||||
"formatVersion": 0,
|
return redirect("/favicon.ico")
|
||||||
"modpackVersion": 0,
|
|
||||||
"title": request.json.get("title"),
|
|
||||||
"author": request.json.get("author"),
|
|
||||||
"version": request.json.get("version"),
|
|
||||||
"modloader": request.json.get("modloader"),
|
|
||||||
"updateURL": "",
|
|
||||||
"mods": [],
|
|
||||||
}
|
|
||||||
title = pack.get("title").replace(" ", "_")
|
|
||||||
|
|
||||||
if os.path.exists(f"{PACKS_FOLDER}/{title}"):
|
return send_file(f"{PACKS_FOLDER}/{id}/packicon.png")
|
||||||
return jsonify({"status": "error", "message": "pack already exists"})
|
|
||||||
|
|
||||||
os.makedirs(f"{PACKS_FOLDER}/{title}", exist_ok=True)
|
|
||||||
|
|
||||||
with open(
|
|
||||||
os.path.abspath(f"{PACKS_FOLDER}/{title}/packfile.json"),
|
|
||||||
mode="w",
|
|
||||||
encoding="utf-8",
|
|
||||||
) as fp:
|
|
||||||
json.dump(pack, fp)
|
|
||||||
fp.close()
|
|
||||||
|
|
||||||
return jsonify(
|
|
||||||
{
|
|
||||||
"status": "ok",
|
|
||||||
"message": f"pack {pack.get('title')} created",
|
|
||||||
"id": title,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@api.route("/<id>/image/edit", methods=["POST"])
|
@apiPack.route("/<id>/image/edit", methods=["POST"])
|
||||||
def APIPackImageEdit(id):
|
def editPackImage(id):
|
||||||
|
|
||||||
image_string = request.json.get("image")
|
image_string = request.json.get("image")
|
||||||
image_mime = request.json.get("mimetype")
|
image_mime = request.json.get("mimetype")
|
||||||
if image_string == None:
|
if image_string == None:
|
||||||
|
|
41
src/api/packs.py
Normal file
41
src/api/packs.py
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
import os
|
||||||
|
from . import apiPacks
|
||||||
|
from flask import request, jsonify
|
||||||
|
from config import PACKS_FOLDER
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
|
@apiPacks.route("/new", methods=["POST"])
|
||||||
|
def createPack():
|
||||||
|
pack = {
|
||||||
|
"formatVersion": 0,
|
||||||
|
"modpackVersion": 0,
|
||||||
|
"title": request.json.get("title"),
|
||||||
|
"author": request.json.get("author"),
|
||||||
|
"version": request.json.get("version"),
|
||||||
|
"modloader": request.json.get("modloader"),
|
||||||
|
"updateURL": "",
|
||||||
|
"mods": [],
|
||||||
|
}
|
||||||
|
title = pack.get("title").replace(" ", "_")
|
||||||
|
|
||||||
|
if os.path.exists(f"{PACKS_FOLDER}/{title}"):
|
||||||
|
return jsonify({"status": "error", "message": "pack already exists"})
|
||||||
|
|
||||||
|
os.makedirs(f"{PACKS_FOLDER}/{title}", exist_ok=True)
|
||||||
|
|
||||||
|
with open(
|
||||||
|
os.path.abspath(f"{PACKS_FOLDER}/{title}/packfile.json"),
|
||||||
|
mode="w",
|
||||||
|
encoding="utf-8",
|
||||||
|
) as fp:
|
||||||
|
json.dump(pack, fp)
|
||||||
|
fp.close()
|
||||||
|
|
||||||
|
return jsonify(
|
||||||
|
{
|
||||||
|
"status": "ok",
|
||||||
|
"message": f"pack {pack.get('title')} created",
|
||||||
|
"id": title,
|
||||||
|
}
|
||||||
|
)
|
|
@ -5,7 +5,8 @@ import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
from api import api
|
from api import apiPack
|
||||||
|
from api import apiPacks
|
||||||
|
|
||||||
|
|
||||||
def resource_path(relative_path):
|
def resource_path(relative_path):
|
||||||
|
@ -21,7 +22,10 @@ app = Flask(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
app.register_blueprint(api)
|
app.register_blueprint(apiPack)
|
||||||
|
app.register_blueprint(apiPacks)
|
||||||
|
|
||||||
|
|
||||||
if os.getenv("is_dev") == "True":
|
if os.getenv("is_dev") == "True":
|
||||||
from flask_cors import CORS
|
from flask_cors import CORS
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue