Skip to content

Commit b9eb474

Browse files
author
Kris Brown
committed
CORS and async issues resolved
1 parent c41933e commit b9eb474

File tree

2 files changed

+50
-30
lines changed

2 files changed

+50
-30
lines changed

packages/algjulia-interop/scripts/endpoint.jl

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,26 @@ using CatColabInterop
1010
using Oxygen
1111
using HTTP
1212

13+
const CORS_HEADERS = [
14+
"Access-Control-Allow-Origin" => "*",
15+
"Access-Control-Allow-Headers" => "*",
16+
"Access-Control-Allow-Methods" => "POST, GET, OPTIONS"
17+
]
18+
19+
function CorsHandler(handle)
20+
return function (req::HTTP.Request)
21+
# return headers on OPTIONS request
22+
if HTTP.method(req) == "OPTIONS"
23+
return HTTP.Response(200, CORS_HEADERS)
24+
else
25+
r = handle(req)
26+
append!(r.headers, ["Access-Control-Allow-Origin" => "*"])
27+
r
28+
29+
end
30+
end
31+
end
32+
1333
defaults = [:Catlab,:ACSets] # all extensions to date
1434

1535
for pkg in (isempty(ARGS) ? defaults : ARGS )
@@ -27,4 +47,4 @@ for m in methods(CatColabInterop.endpoint)
2747
invoke(fntype.instance, Tuple{argtypes...}, Val(name))
2848
end
2949

30-
serve()
50+
serve(middleware=[CorsHandler])
Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11

22
import type { DiagramAnalysisProps } from "../../analysis";
33
import type { GraphLayoutConfig } from "../../visualization";
4-
import { GraphVisualization } from "./graph_visualization";
5-
import { diagramToGraphviz } from "./diagram_graph";
4+
// import { GraphVisualization } from "./graph_visualization";
5+
// import { diagramToGraphviz } from "./diagram_graph";
6+
import { createResource, Switch, Match } from "solid-js";
7+
68
import axios from 'axios';
79

810
const config = {
@@ -27,34 +29,32 @@ export default function TabularView(
2729
title?: string;
2830
},
2931
) {
30-
const graph = () => {
31-
const theory = props.liveDiagram.liveModel.theory();
32-
const model = props.liveDiagram.liveModel.elaboratedModel();
33-
const diagram = props.liveDiagram.elaboratedDiagram();
34-
if (theory && model && diagram) {
35-
return diagramToGraphviz(diagram, model, theory);
36-
}
37-
};
38-
39-
// const call = async (data: string) => {return await ;}
40-
41-
const res = () => {
42-
const model = props.liveDiagram.liveModel.elaboratedModel();
43-
const diagram = props.liveDiagram.elaboratedDiagram();
44-
const data = axios.post("http://127.0.0.1:8080/acsetcolim", {
45-
model:model?.presentation(), diagram:diagram?.presentation()
46-
}, config);
47-
return data.then(res => {
48-
console.log(res); // getting a value
49-
return res});
50-
}
5132

33+
const [res] = createResource(
34+
() => {
35+
const model = props.liveDiagram.liveModel.elaboratedModel();
36+
const diagram = props.liveDiagram.elaboratedDiagram();
37+
return model && diagram && [model, diagram];
38+
},
39+
async ([model, diagram]) => {
40+
const response = await axios.post("http://127.0.0.1:8080/acsetcolim", {
41+
model: model?.presentation(), diagram: diagram?.presentation()
42+
}, config);
43+
// console.log(response.data);
44+
return response.data;
45+
}
46+
);
5247
return (
53-
<GraphVisualization
54-
title={"TABULAR VIEW: "+res()}
55-
graph={graph()}
56-
config={props.content}
57-
changeConfig={props.changeContent}
58-
/>
48+
<Switch>
49+
<Match when={res.loading}>
50+
<div>⏳ Loading model...</div>
51+
</Match>
52+
<Match when={res.error}>
53+
<div>❌ Error loading model: {res.error?.message || "Unknown error"}</div>
54+
</Match>
55+
<Match when={res()}>
56+
{(res) => <div>Tabular Instance: {JSON.stringify(res())}</div>}
57+
</Match>
58+
</Switch>
5959
);
6060
}

0 commit comments

Comments
 (0)