Skip to content

Commit

Permalink
Patch CORS GitHub problem release (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
zelytra authored Mar 14, 2024
1 parent d81d697 commit f2fdd0c
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 32 deletions.
5 changes: 5 additions & 0 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@
<artifactId>json</artifactId>
<version>20240303</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
58 changes: 58 additions & 0 deletions backend/src/main/java/fr/zelytra/github/GithubApi.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
9 changes: 9 additions & 0 deletions backend/src/main/java/fr/zelytra/github/GithubRelease.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package fr.zelytra.github;

import java.util.Date;

public class GithubRelease {
public String version;
public Date publicationDate;
public String url;
}
21 changes: 21 additions & 0 deletions backend/src/main/java/fr/zelytra/github/GithubRest.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
15 changes: 15 additions & 0 deletions backend/src/main/java/fr/zelytra/github/TauriRelease.java
Original file line number Diff line number Diff line change
@@ -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<String, Platform> platforms;
}

class Platform {
public String signature;
public String url;
}
2 changes: 1 addition & 1 deletion deployment/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
12 changes: 0 additions & 12 deletions website/src/objects/Github.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 1 addition & 2 deletions website/src/objects/HTTPAxios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 5 additions & 17 deletions website/src/objects/stores/appStore.ts
Original file line number Diff line number Diff line change
@@ -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
})
},
});

0 comments on commit f2fdd0c

Please sign in to comment.