Skip to content

Commit

Permalink
add version info to api between web server and browser
Browse files Browse the repository at this point in the history
  • Loading branch information
john-tornblom committed Jun 22, 2024
1 parent 5062a6a commit 8594197
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 9 deletions.
7 changes: 5 additions & 2 deletions Makefile.pc
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
# along with this program; see the file COPYING. If not see
# <http://www.gnu.org/licenses/>.

VERSION_TAG := $(shell git describe --abbrev=10 --dirty --always --tags)

PYTHON ?= python3

BIN := websrv.pc
SRCS := src/main.c src/websrv.c src/asset.c src/fs.c
SRCS += src/pc/sys.c

LDADD := -lmicrohttpd
CFLAGS := -Wall -DVERSION_TAG=\"$(VERSION_TAG)\"
LDADD := -lmicrohttpd

ASSETS := $(wildcard assets/*)
GEN_SRCS := $(patsubst assets/%,gen/%, $(ASSETS:=.c))
Expand All @@ -37,5 +40,5 @@ gen/%.c: assets/% gen
$(PYTHON) gen-asset-module.py --path $* $< > $@

$(BIN): $(SRCS) $(GEN_SRCS)
$(CC) -o $@ $^ $(LDADD)
$(CC) $(CFLAGS) -o $@ $^ $(LDADD)

4 changes: 3 additions & 1 deletion Makefile.ps5
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ else
$(error PS5_PAYLOAD_SDK is undefined)
endif

VERSION_TAG := $(shell git describe --abbrev=10 --dirty --always --tags)

PYTHON ?= python3

BIN := websrv.elf
SRCS := src/main.c src/websrv.c src/asset.c src/fs.c
SRCS += src/ps5/sys.c src/ps5/pt.c src/ps5/elfldr.c src/ps5/hbldr.c
CFLAGS := -Wall -Werror -Isrc
CFLAGS := -Wall -Werror -Isrc -DVERSION_TAG=\"$(VERSION_TAG)\"
LDADD := -lkernel_sys -lmicrohttpd -lSceSystemService -lSceUserService

ASSETS := $(wildcard assets/*)
Expand Down
16 changes: 16 additions & 0 deletions assets/apiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,20 @@ class ApiClient {
}
return await response.text();
}

static async getVersion() {
try {
const response = await fetch(baseURL + '/version');
if (response.ok) {
return await response.json();
}
} catch (error) {
}
return {
api: 0,
tag: '',
date: '',
time: ''
};
}
}
8 changes: 8 additions & 0 deletions assets/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,14 @@ window.onload = async function () {
// window.location.href = "/";
});

const ver = await ApiClient.getVersion();
if (ver.api > 0) {
const verstr = ` ${ver.tag} (compiled at ${ver.date} ${ver.time})`;
document.title += verstr;
} else {
document.title += ' (unknown version)';
}

registerExtensionMessagesListener();

// for home page carousel
Expand Down
31 changes: 25 additions & 6 deletions homebrew/Mednafen/homebrew.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ async function main() {
}
}

async function checkApiVersion() {
try {
const ver = await ApiClient.getVersion();
console.log(ver);
if (ver.api > 0) {
return true;
}
} catch(error) {
console.error(error);
}

alert('Incompatible web server');
return false;
}

async function getRomList() {
let listing = await ApiClient.fsListDir(ROMDIR);
return listing.filter(entry =>
Expand All @@ -88,17 +103,21 @@ async function main() {
mainText: "Mednafen",
secondaryText: 'Multi-system Emulator',
onclick: async () => {
let items = await getRomList();
showCarousel(items);
if(await checkApiVersion()) {
let items = await getRomList();
showCarousel(items);
}
},
options: [
{
text: "Browse ROM...",
onclick: async () => {
return {
path: PAYLOAD,
args: await pickFile(window.workingDir)
};
if(await checkApiVersion()) {
return {
path: PAYLOAD,
args: await pickFile(window.workingDir)
};
}
}
}
]
Expand Down
29 changes: 29 additions & 0 deletions src/version.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* Copyright (C) 2024 John Törnblom
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3, or (at your option) any
later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, see
<http://www.gnu.org/licenses/>. */

#pragma once

#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)

#define PAGE_VERSION \
"{" \
"\"tag\": " TOSTRING(VERSION_TAG) "," \
"\"date\": " TOSTRING(__DATE__) "," \
"\"time\": " TOSTRING(__TIME__) "," \
"\"api\": 1" \
"}"

27 changes: 27 additions & 0 deletions src/websrv.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ along with this program; see the file COPYING. If not, see
#include "asset.h"
#include "fs.h"
#include "sys.h"
#include "version.h"
#include "websrv.h"


Expand All @@ -42,6 +43,28 @@ websrv_queue_response(struct MHD_Connection *conn, unsigned int status,
}



/**
* Respond to a version request.
**/
static enum MHD_Result
version_request(struct MHD_Connection *conn) {
size_t size = strlen(PAGE_VERSION);
enum MHD_Result ret = MHD_NO;
struct MHD_Response *resp;
void* data = PAGE_VERSION;

if((resp=MHD_create_response_from_buffer(size, data,
MHD_RESPMEM_PERSISTENT))) {
MHD_add_response_header(resp, "Content-Type", "application/json");
ret = websrv_queue_response(conn, MHD_HTTP_OK, resp);
MHD_destroy_response(resp);
}

return ret;
}


/**
* Respond to a launch request.
**/
Expand Down Expand Up @@ -157,6 +180,10 @@ ahc_echo(void *cls, struct MHD_Connection *conn,
return hbldr_request(conn);
}

if(!strcmp("/version", url)) {
return version_request(conn);
}

if(!strcmp("/", url) || !url[0]) {
return asset_request(conn, "/index.html");
}
Expand Down

0 comments on commit 8594197

Please sign in to comment.