Skip to content

Commit

Permalink
fix: update versions endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
KaiVolland committed Jun 21, 2024
1 parent ab863e7 commit 66464d6
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 43 deletions.
8 changes: 3 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { swagger } from '@elysiajs/swagger'
import { html } from '@elysiajs/html'
import { transFormApi, transform } from "./routes/api";
import loggisch from 'loggisch';
import { versions } from "./routes/info";
import { versions, versionsApi } from "./routes/info";

loggisch.setLogLevel('trace');

const port = process.env.NODE_API_PORT || 8888;

const app = new Elysia()
export const app = new Elysia()
.use(swagger({
path: "/api-docs",
exclude: ["/api-docs", "/api-docs/json"],
Expand All @@ -23,7 +23,7 @@ const app = new Elysia()
}))
.group("/info", (app) => app
.use(html())
.get("/versions", versions)
.get("/versions", versions, versionsApi)
)
.group("/api", (app) => app
.post("/transform", transform, transFormApi)
Expand All @@ -33,5 +33,3 @@ const app = new Elysia()
console.log(
`🦊 Elysia is running at http://${app.server?.hostname}:${app.server?.port}`
);

export default app;
76 changes: 49 additions & 27 deletions src/routes/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { Handler, ParseError, t } from "elysia";
import GeoStylerLyrxParser, { LyrxParser } from "geostyler-lyrx-parser";
import MapboxStyleParser from "geostyler-mapbox-parser";
import QGISStyleParser from "geostyler-qgis-parser";
// import LyrxParser from "geostyler-lyrx-parser";
import SldParser from "geostyler-sld-parser";

Expand All @@ -20,22 +23,38 @@ export const transFormApi = {
body: t.Any({
description: 'The style to transform in the specified format',
required: true,
example: {
"name": "My Style",
"rules": [
{
"name": "My Rule",
"symbolizers": [
{
"kind": "Mark",
"wellKnownName": "Circle",
"color": "#FF0000",
"radius": 6
}
]
}
]
}
examples: [
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<StyledLayerDescriptor version="1.0.0"
xsi:schemaLocation="http://www.opengis.net/sld StyledLayerDescriptor.xsd"
xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc"
xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:se="http://www.opengis.net/se">
<NamedLayer>
<Name>My Style</Name>
<UserStyle>
<Name>My Style</Name>
<Title>My Style</Title>
<FeatureTypeStyle>
<Rule>
<Name>My Rule</Name>
<PointSymbolizer>
<Graphic>
<Mark>
<WellKnownName>circle</WellKnownName>
<Fill>
<CssParameter name="fill">#FF0000</CssParameter>
</Fill>
</Mark>
<Size>12</Size>
</Graphic>
</PointSymbolizer>
</Rule>
</FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>`
]
}),
response: t.Any({
description: 'The transformed style in the specified format'
Expand All @@ -47,9 +66,7 @@ export const transform: Handler = async ({
query: { sourceFormat, targetFormat }
}) => {

const sourceStyle = body as string;

if (!sourceStyle) {
if (!body) {
log.error('Error: No source style style given in POST body.');
throw new ParseError('Error', 'No source style style given in POST body.');
}
Expand All @@ -64,8 +81,12 @@ export const transform: Handler = async ({

let readResponse;

// TODO: type should be fixed here
let sourceStyle = body as any;

// if no sourceParser is given we expect the body to be a geostyler-style
if (sourceParser === undefined) {
readResponse = { output: JSON.parse(sourceStyle) };
readResponse = { output: sourceStyle };
} else {
readResponse = await sourceParser.readStyle(sourceStyle);
if (Array.isArray(readResponse.errors) && readResponse.errors.length) {
Expand All @@ -74,6 +95,7 @@ export const transform: Handler = async ({
}
}

// if no targetParser is given we return a geostyler-style
if (targetParser === undefined) {
return readResponse.output;
}
Expand Down Expand Up @@ -104,7 +126,7 @@ export const transform: Handler = async ({
* @param paramVal Query param value for the format, e.g. 'qml'
* @returns Content-Type
*/
const getContentTypeFromUrlParam = (paramVal: string) => {
const getContentTypeFromParserName = (paramVal: string) => {
if (!paramVal) {
return undefined;
}
Expand Down Expand Up @@ -135,16 +157,16 @@ const getParserFromUrlParam = (paramVal: string) => {
}

switch (paramVal.toLowerCase()) {
// case 'lyrx':
// return new LyrxParser();
// case 'mapbox':
// return new MapboxParser();
case 'lyrx':
return new LyrxParser();
case 'mapbox':
return new MapboxStyleParser();
// case 'mapserver':
// return new MapfileParser();
case 'sld':
return new SldParser();
// case 'qml':
// return new QgisParser();
case 'qml':
return new QGISStyleParser();
case 'geostyler':
default:
return undefined;
Expand Down
60 changes: 50 additions & 10 deletions src/routes/info.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,63 @@
import { html } from '@elysiajs/html'
import { Handler } from 'elysia';
// import { html } from '@elysiajs/html'
import { Handler, t } from 'elysia';
import {
dependencies as deps,
version
} from '../../package.json';

export const versionsApi = {
response: t.Any({
description: 'Information about the installed versions of the GeoStyler parsers.' +
' If the `Accept` header is set to `application/json`, the response will be a JSON object. ' +
'Otherwise, the response will be an HTML page.'
})
};

export const versions: Handler = ({
request
}) => {
if (request.headers.get('accept') === 'text/html') {
if (request.headers.get('accept') === 'application/json') {
return {
'geostyler-rest': version,
'geostyler-mapbox-parser': getVersionString('geostyler-mapbox-parser'),
'geostyler-mapfile-parser': getVersionString('geostyler-mapfile-parser'),
'geostyler-qgis-parser': getVersionString('geostyler-qgis-parser'),
'geostyler-sld-parser': getVersionString('geostyler-sld-parser'),
'geostyler-arcgis-parser': getVersionString('geostyler-lyrx-parser')
};
} else {
return (
<html lang='en'>
<head>
<title>Hello World</title>
<title>GeoStyler REST API Versions</title>
</head>
<body>
<h1>Hello World</h1>
</body>
GeoStyler REST version {version}
<ul>
<li>
GeoStyler Mapbox Parser: {getVersionString('geostyler-mapbox-parser')}
</li>
<li>
GeoStyler Mapfile Parser: {getVersionString('geostyler-mapfile-parser')}
</li>
<li>
GeoStyler QGIS Parser: {getVersionString('geostyler-qgis-parser')}
</li>
<li>
GeoStyler SLD Parser: {getVersionString('geostyler-sld-parser')}
</li>
<li>
GeoStyler ArcGIS Parser: {getVersionString('geostyler-lyrx-parser')}
</li>
</ul>
</body>
</html>
);
} else {
return {
peter: 'pan'
};
}
};

const getVersionString = (parserName: string) => {
return deps[parserName as keyof typeof deps]
? `${deps[parserName as keyof typeof deps]}`
: 'not installed';
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"types": ["bun-types"], /* Specify type package names to be included without being referenced in a source file. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
// "resolveJsonModule": true, /* Enable importing .json files. */
"resolveJsonModule": true, /* Enable importing .json files. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */

/* JavaScript Support */
Expand Down

0 comments on commit 66464d6

Please sign in to comment.