From f2fdd0cd6164f94e87c4d42562ee8fa0dac08c94 Mon Sep 17 00:00:00 2001 From: Zelytra <72125876+zelytra@users.noreply.github.com> Date: Thu, 14 Mar 2024 21:54:00 +0100 Subject: [PATCH] Patch CORS GitHub problem release (#129) --- backend/pom.xml | 5 ++ .../java/fr/zelytra/github/GithubApi.java | 58 +++++++++++++++++++ .../java/fr/zelytra/github/GithubRelease.java | 9 +++ .../java/fr/zelytra/github/GithubRest.java | 21 +++++++ .../java/fr/zelytra/github/TauriRelease.java | 15 +++++ deployment/docker-compose.yml | 2 +- website/src/objects/Github.ts | 12 ---- website/src/objects/HTTPAxios.ts | 3 +- website/src/objects/stores/appStore.ts | 22 ++----- 9 files changed, 115 insertions(+), 32 deletions(-) create mode 100644 backend/src/main/java/fr/zelytra/github/GithubApi.java create mode 100644 backend/src/main/java/fr/zelytra/github/GithubRelease.java create mode 100644 backend/src/main/java/fr/zelytra/github/GithubRest.java create mode 100644 backend/src/main/java/fr/zelytra/github/TauriRelease.java diff --git a/backend/pom.xml b/backend/pom.xml index 57b42860..6e884ef3 100644 --- a/backend/pom.xml +++ b/backend/pom.xml @@ -82,6 +82,11 @@ json 20240303 + + com.google.code.gson + gson + 2.10.1 + diff --git a/backend/src/main/java/fr/zelytra/github/GithubApi.java b/backend/src/main/java/fr/zelytra/github/GithubApi.java new file mode 100644 index 00000000..833a664c --- /dev/null +++ b/backend/src/main/java/fr/zelytra/github/GithubApi.java @@ -0,0 +1,58 @@ +package fr.zelytra.github; + +import com.google.gson.Gson; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; + +public class GithubApi { + + private static final String releaseUrl = "https://github.com/zelytra/BetterFleet/releases/latest/download/latest.json"; + + private final GithubRelease githubRelease; + + public GithubApi() throws IOException { + // Create a neat value object to hold the URL + URL url = new URL("https://github.com/zelytra/BetterFleet/releases/latest/download/latest.json"); + + // Open a connection(?) on the URL(??) and cast the response(???) + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + + // Now it's "open", we can set the request method, headers etc. + connection.setRequestProperty("Content-Type", "application/json"); + connection.setRequestMethod("GET"); + + // This line makes the request + BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); + String inputLine; + StringBuffer content = new StringBuffer(); + while ((inputLine = in.readLine()) != null) { + content.append(inputLine); + } + + // Close the connections + in.close(); + connection.disconnect(); + + // Convert the StringBuffer to a string + String jsonString = content.toString(); + + // Parse JSON response + Gson gson = new Gson(); + TauriRelease tauriRelease = gson.fromJson(jsonString, TauriRelease.class); + + // Process the data as needed + this.githubRelease = new GithubRelease(); + githubRelease.version = tauriRelease.version; + //githubRelease.publicationDate = new Date(tauriRelease.pub_date); + githubRelease.url = tauriRelease.platforms.get("windows-x86_64").url.replace("nsis.zip", "exe"); + + } + + public GithubRelease getGithubRelease() { + return githubRelease; + } +} diff --git a/backend/src/main/java/fr/zelytra/github/GithubRelease.java b/backend/src/main/java/fr/zelytra/github/GithubRelease.java new file mode 100644 index 00000000..15e941fb --- /dev/null +++ b/backend/src/main/java/fr/zelytra/github/GithubRelease.java @@ -0,0 +1,9 @@ +package fr.zelytra.github; + +import java.util.Date; + +public class GithubRelease { + public String version; + public Date publicationDate; + public String url; +} diff --git a/backend/src/main/java/fr/zelytra/github/GithubRest.java b/backend/src/main/java/fr/zelytra/github/GithubRest.java new file mode 100644 index 00000000..bb247501 --- /dev/null +++ b/backend/src/main/java/fr/zelytra/github/GithubRest.java @@ -0,0 +1,21 @@ +package fr.zelytra.github; + +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; + +import java.io.IOException; + +@Path("/github") +public class GithubRest { + + @GET + @Path("/release/download") + @Produces(MediaType.APPLICATION_JSON) + public Response getDownloadLink() throws IOException { + GithubApi githubApi = new GithubApi(); + return Response.ok(githubApi.getGithubRelease()).build(); + } +} diff --git a/backend/src/main/java/fr/zelytra/github/TauriRelease.java b/backend/src/main/java/fr/zelytra/github/TauriRelease.java new file mode 100644 index 00000000..fd78f6d1 --- /dev/null +++ b/backend/src/main/java/fr/zelytra/github/TauriRelease.java @@ -0,0 +1,15 @@ +package fr.zelytra.github; + +import java.util.Map; + +public class TauriRelease { + public String version; + public String notes; + public String pub_date; + public Map platforms; +} + +class Platform { + public String signature; + public String url; +} \ No newline at end of file diff --git a/deployment/docker-compose.yml b/deployment/docker-compose.yml index d11de730..d0209ec5 100644 --- a/deployment/docker-compose.yml +++ b/deployment/docker-compose.yml @@ -8,7 +8,7 @@ services: - "2600:5432" volumes: - ./init.sql:/docker-entrypoint-initdb.d/init.sql - - /psql-data/app:/var/lib/postgresql/betterfleet/data/pgdata + - ./psql-data/app:/var/lib/postgresql/betterfleet/data/pgdata - /etc/localtime:/etc/localtime:ro environment: POSTGRES_USER: ${POSTGRES_USER} diff --git a/website/src/objects/Github.ts b/website/src/objects/Github.ts index 4e0b4f04..0408012b 100644 --- a/website/src/objects/Github.ts +++ b/website/src/objects/Github.ts @@ -1,15 +1,3 @@ -export interface TauriRelease { - version: string - notes: string - pub_date: string - platforms: { - "windows-x86_64": { - signature: string - url: string - } - } -} - export interface GithubRelease { version: string publicationDate: Date diff --git a/website/src/objects/HTTPAxios.ts b/website/src/objects/HTTPAxios.ts index e0067f03..cc15d883 100644 --- a/website/src/objects/HTTPAxios.ts +++ b/website/src/objects/HTTPAxios.ts @@ -7,8 +7,7 @@ export class HTTPAxios { private readonly header = { "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Methods": "GET, POST, DELETE", - Authorization: "", + "Access-Control-Allow-Methods": "GET, POST, DELETE" }; private readonly url = import.meta.env.VITE_BACKEND_HOST + "/"; public isAuth = false; diff --git a/website/src/objects/stores/appStore.ts b/website/src/objects/stores/appStore.ts index d194bc91..b76c43ac 100644 --- a/website/src/objects/stores/appStore.ts +++ b/website/src/objects/stores/appStore.ts @@ -1,26 +1,14 @@ import {reactive} from "vue"; -import axios, {AxiosResponse} from "axios"; -import {GithubRelease, TauriRelease} from "@/objects/Github.ts"; +import {AxiosResponse} from "axios"; +import {GithubRelease} from "@/objects/Github.ts"; +import {HTTPAxios} from "@/objects/HTTPAxios.ts"; export const AppStore = reactive({ - tauriRelease: {} as TauriRelease, githubRelease: {} as GithubRelease, init() { - const httpRequest = axios.create({ - headers: { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Methods": "GET, POST, DELETE", - Authorization: "", - } - }) - httpRequest.get("https://github.com/zelytra/BetterFleet/releases/latest/download/latest.json").then((resonse: AxiosResponse) => { - this.tauriRelease = resonse.data as TauriRelease; - this.githubRelease = { - url: this.tauriRelease.platforms["windows-x86_64"].url.replace("nsis.zip", "exe"), - publicationDate: new Date(this.tauriRelease.pub_date), - version: this.tauriRelease.version - } + new HTTPAxios("github/release/download").get().then((response: AxiosResponse) => { + this.githubRelease = response.data as GithubRelease }) }, });