svg-layout-designer-react/test-server/http.js
2022-08-05 10:45:16 +02:00

142 lines
3.4 KiB
JavaScript

// http.js
import { serve } from 'bun';
serve({
port: 5000,
fetch(request) {
console.log(`${request.method}: ${request.url}`);
if (request.method === 'POST') {
const url = new URL(request.url);
let json;
if (url.pathname === '/GetSVGLayoutConfiguration') {
json = JSON.stringify(GetSVGLayoutConfiguration());
} else if (url.pathname === '/FillHoleWithChassis') {
json = JSON.stringify(FillHoleWithChassis(request));
} else if (url.pathname === '/SplitRemplissage') {
json = JSON.stringify(SplitRemplissage(request));
} else {
// TODO: Return 404 rather than this
json = JSON.stringify(GetSVGLayoutConfiguration());
}
return new Response(json, {
status: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': '*'
}
});
}
return new Response('Welcome to Bun!', {
status: 200,
headers: {
'Content-Type': 'text/plain',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': '*'
}
});
}
});
const GetSVGLayoutConfiguration = () => {
return {
AvailableContainers: [
{
Type: 'Chassis',
Width: 500,
Style: {
fillOpacity: 0,
borderWidth: 2,
stroke: 'red'
}
},
{
Type: 'Trou',
Width: 300,
Style: {
fillOpacity: 0,
borderWidth: 2,
stroke: 'green'
}
},
{
Type: 'Montant',
Width: 100,
Style: {
fillOpacity: 0,
borderWidth: 2,
stroke: 'blue',
transform: 'translateX(-50%)',
transformOrigin: 'center',
transformBox: 'fill-box'
}
}
],
AvailableSymbols: [
{
Height: 0,
Image: {
Base64Image: null,
Name: null,
Svg: null,
Url: 'https://www.manutan.fr/img/S/GRP/ST/AIG3930272.jpg'
},
Name: 'Poteau structure',
Width: 0,
XPositionReference: 1
},
{
Height: 0,
Image: {
Base64Image: null,
Name: null,
Svg: null,
Url: 'https://e7.pngegg.com/pngimages/647/127/png-clipart-svg-working-group-information-world-wide-web-internet-structure.png'
},
Name: 'Joint de structure',
Width: 0,
XPositionReference: 0
}
],
MainContainer: {
Height: 200,
Width: 1000
}
};
};
const FillHoleWithChassis = (request) => {
const maxWidthChassis = 3000;
const nbChassis = Math.ceil(request.ContainerActions.Width / maxWidthChassis);
const lstModels = [];
for (let i = 0; i <= nbChassis; i++) {
if (i === 1 && request.ContainerAction.ExistOnBefore) {
lstModels.Add({ Type: 'Dilatation' });
}
lstModels.Add({ Type: 'Chassis' });
if (i < nbChassis) {
lstModels.Add({ Type: 'Dilatation' });
}
if (i === nbChassis && request.ContainerAction.ExistOnAfter) {
lstModels.Add({ Type: 'Dilatation' });
}
}
return {
ApplicationState: {
Containers: lstModels
}
};
};
const SplitRemplissage = (request) => {
const lstModels = [
{ Type: 'Remplissage' },
{ Type: 'Montant' },
{ Type: 'Remplissage' }
];
return {
ApplicationState: { Containers: lstModels }
};
};