initial commit
This commit is contained in:
commit
97b5923c1e
39
.drone.yml
Normal file
39
.drone.yml
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
kind: pipeline
|
||||||
|
type: docker
|
||||||
|
name: default
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: test
|
||||||
|
image: openjdk:8-jdk
|
||||||
|
commands:
|
||||||
|
- echo "test"
|
||||||
|
- ./gradlew build
|
||||||
|
|
||||||
|
- name: build
|
||||||
|
image: docker:stable-dind
|
||||||
|
volumes:
|
||||||
|
- name: dockersock
|
||||||
|
path: /var/run
|
||||||
|
environment:
|
||||||
|
DOCKER_USERNAME:
|
||||||
|
from_secret: DOCKER_USERNAME
|
||||||
|
DOCKER_PASSWORD:
|
||||||
|
from_secret: DOCKER_PASSWORD
|
||||||
|
commands:
|
||||||
|
- echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin registry.kmlabz.com
|
||||||
|
- docker build -t="registry.kmlabz.com/$DOCKER_USERNAME/$DRONE_REPO_NAME" .
|
||||||
|
- docker build -t="registry.kmlabz.com/$DOCKER_USERNAME/$DRONE_REPO_NAME:$DRONE_BUILD_NUMBER" .
|
||||||
|
- docker push "registry.kmlabz.com/$DOCKER_USERNAME/$DRONE_REPO_NAME"
|
||||||
|
- docker push "registry.kmlabz.com/$DOCKER_USERNAME/$DRONE_REPO_NAME:$DRONE_BUILD_NUMBER"
|
||||||
|
|
||||||
|
services:
|
||||||
|
- name: docker
|
||||||
|
image: docker:stable-dind
|
||||||
|
privileged: true
|
||||||
|
volumes:
|
||||||
|
- name: dockersock
|
||||||
|
path: /var/run
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
- name: dockersock
|
||||||
|
temp: {}
|
7
.gitignore
vendored
Normal file
7
.gitignore
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
/.gradle
|
||||||
|
/.idea
|
||||||
|
/out
|
||||||
|
/build
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
*.iws
|
14
Dockerfile
Normal file
14
Dockerfile
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
FROM openjdk:8-jre-alpine
|
||||||
|
|
||||||
|
ENV APPLICATION_USER ktor
|
||||||
|
RUN adduser -D -g '' $APPLICATION_USER
|
||||||
|
|
||||||
|
RUN mkdir /app
|
||||||
|
RUN chown -R $APPLICATION_USER /app
|
||||||
|
|
||||||
|
USER $APPLICATION_USER
|
||||||
|
|
||||||
|
COPY ./build/libs/my-application.jar /app/my-application.jar
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
CMD ["java", "-server", "-XX:+UnlockExperimentalVMOptions", "-XX:+UseCGroupMemoryLimitForHeap", "-XX:InitialRAMFraction=2", "-XX:MinRAMFraction=2", "-XX:MaxRAMFraction=2", "-XX:+UseG1GC", "-XX:MaxGCPauseMillis=100", "-XX:+UseStringDeduplication", "-jar", "my-application.jar"]
|
28
api.http
Normal file
28
api.http
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
# Output Service - RDB
|
||||||
|
# This is the feature extraction interface of the Birbnetes system.
|
||||||
|
|
||||||
|
###
|
||||||
|
|
||||||
|
# Get all negative decision objects
|
||||||
|
GET {{host}}/output/filter/negative
|
||||||
|
|
||||||
|
###
|
||||||
|
|
||||||
|
# Get positive decision objects
|
||||||
|
GET {{host}}/output/filter/positive
|
||||||
|
|
||||||
|
###
|
||||||
|
|
||||||
|
# Get decision before a date
|
||||||
|
GET {{host}}/output/after/{{param_dateAfter}}
|
||||||
|
|
||||||
|
###
|
||||||
|
|
||||||
|
# Get decision before a date
|
||||||
|
GET {{host}}/output/before/{{param_dateBefore}}
|
||||||
|
|
||||||
|
###
|
||||||
|
|
||||||
|
# Get decision by ID
|
||||||
|
GET {{host}}/output/{{param_tagID}}
|
||||||
|
|
143
api.yaml
Normal file
143
api.yaml
Normal file
@ -0,0 +1,143 @@
|
|||||||
|
swagger: "2.0"
|
||||||
|
info:
|
||||||
|
description: "This is the feature extraction interface of the Birbnetes system."
|
||||||
|
version: "1.0.0"
|
||||||
|
title: "Output Service - RDB"
|
||||||
|
contact:
|
||||||
|
email: "tormakristof@tormakristof.eu"
|
||||||
|
license:
|
||||||
|
name: "Apache 2.0"
|
||||||
|
url: "http://www.apache.org/licenses/LICENSE-2.0.html"
|
||||||
|
host: "dev.k8s.tcloud.enginner"
|
||||||
|
basePath: "/api/extractor/rdb/v1"
|
||||||
|
tags:
|
||||||
|
- name: "output"
|
||||||
|
description: "Output Service - RDB interaction"
|
||||||
|
schemes:
|
||||||
|
- "https"
|
||||||
|
- "http"
|
||||||
|
paths:
|
||||||
|
/output/filter/negative:
|
||||||
|
get:
|
||||||
|
summary: Get all negative decision objects
|
||||||
|
operationId: getallnegative
|
||||||
|
tags:
|
||||||
|
- output
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: Array of decision objects
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/OutputObjects'
|
||||||
|
"404":
|
||||||
|
description: No object matching filter
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/ApiResponse'
|
||||||
|
|
||||||
|
/output/filter/positive:
|
||||||
|
get:
|
||||||
|
summary: Get positive decision objects
|
||||||
|
operationId: getallpositive
|
||||||
|
tags:
|
||||||
|
- output
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: Array of decision objects
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/OutputObjects'
|
||||||
|
"404":
|
||||||
|
description: No object matching filter
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/ApiResponse'
|
||||||
|
|
||||||
|
/output/after/{dateAfter}:
|
||||||
|
get:
|
||||||
|
summary: Get decision before a date
|
||||||
|
operationId: getallafter
|
||||||
|
tags:
|
||||||
|
- output
|
||||||
|
parameters:
|
||||||
|
- name: dateAfter
|
||||||
|
in: path
|
||||||
|
description: Date of filter
|
||||||
|
required: true
|
||||||
|
type: string
|
||||||
|
format: date
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: Array of decision objects
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/OutputObjects'
|
||||||
|
"404":
|
||||||
|
description: No object matching filter
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/ApiResponse'
|
||||||
|
|
||||||
|
/output/before/{dateBefore}:
|
||||||
|
get:
|
||||||
|
summary: Get decision before a date
|
||||||
|
operationId: getallbefore
|
||||||
|
tags:
|
||||||
|
- output
|
||||||
|
parameters:
|
||||||
|
- name: dateBefore
|
||||||
|
in: path
|
||||||
|
description: Date of filter
|
||||||
|
required: true
|
||||||
|
type: string
|
||||||
|
format: date
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: Array of decision objects
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/OutputObjects'
|
||||||
|
"404":
|
||||||
|
description: NO objects matching filter
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/ApiResponse'
|
||||||
|
|
||||||
|
/output/{tagID}:
|
||||||
|
get:
|
||||||
|
summary: Get decision by ID
|
||||||
|
operationId: getDecision
|
||||||
|
tags:
|
||||||
|
- output
|
||||||
|
parameters:
|
||||||
|
- name: tagID
|
||||||
|
in: path
|
||||||
|
description: ID of wave file
|
||||||
|
required: true
|
||||||
|
type: integer
|
||||||
|
format: int32
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: Decision object
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/OutputObject'
|
||||||
|
"404":
|
||||||
|
description: Tag not found
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/ApiResponse'
|
||||||
|
|
||||||
|
definitions:
|
||||||
|
OutputObjects:
|
||||||
|
type: array
|
||||||
|
items:
|
||||||
|
$ref: '#/definitions/OutputObject'
|
||||||
|
OutputObject:
|
||||||
|
type: "object"
|
||||||
|
properties:
|
||||||
|
tag:
|
||||||
|
type: "string"
|
||||||
|
decison:
|
||||||
|
type: "boolean"
|
||||||
|
date:
|
||||||
|
type: "string"
|
||||||
|
format: "date"
|
||||||
|
ApiResponse:
|
||||||
|
type: "object"
|
||||||
|
properties:
|
||||||
|
status:
|
||||||
|
type: "string"
|
||||||
|
message:
|
||||||
|
type: "string"
|
||||||
|
|
56
build.gradle
Normal file
56
build.gradle
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
buildscript {
|
||||||
|
repositories {
|
||||||
|
jcenter()
|
||||||
|
maven { url "https://plugins.gradle.org/m2/" }
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||||
|
classpath "com.github.jengelman.gradle.plugins:shadow:5.2.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
apply plugin: 'kotlin'
|
||||||
|
apply plugin: "com.github.johnrengelman.shadow"
|
||||||
|
apply plugin: 'application'
|
||||||
|
|
||||||
|
group 'com.kmalbz'
|
||||||
|
version '0.0.1'
|
||||||
|
mainClassName = "io.ktor.server.netty.EngineMain"
|
||||||
|
|
||||||
|
sourceSets {
|
||||||
|
main.kotlin.srcDirs = main.java.srcDirs = ['src']
|
||||||
|
test.kotlin.srcDirs = test.java.srcDirs = ['test']
|
||||||
|
main.resources.srcDirs = ['resources']
|
||||||
|
test.resources.srcDirs = ['testresources']
|
||||||
|
}
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenLocal()
|
||||||
|
jcenter()
|
||||||
|
maven { url 'https://kotlin.bintray.com/ktor' }
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
|
||||||
|
implementation "io.ktor:ktor-server-netty:$ktor_version"
|
||||||
|
implementation "ch.qos.logback:logback-classic:$logback_version"
|
||||||
|
implementation "io.ktor:ktor-server-core:$ktor_version"
|
||||||
|
implementation "io.ktor:ktor-gson:$ktor_version"
|
||||||
|
implementation "io.ktor:ktor-server-host-common:$ktor_version"
|
||||||
|
implementation "io.ktor:ktor-client-core:$ktor_version"
|
||||||
|
implementation "io.ktor:ktor-client-core-jvm:$ktor_version"
|
||||||
|
implementation "io.ktor:ktor-client-apache:$ktor_version"
|
||||||
|
implementation "io.ktor:ktor-jackson:$ktor_version"
|
||||||
|
implementation "io.ktor:ktor-auth:$ktor_version"
|
||||||
|
testImplementation "io.ktor:ktor-server-tests:$ktor_version"
|
||||||
|
}
|
||||||
|
|
||||||
|
kotlin.experimental.coroutines = 'enable'
|
||||||
|
|
||||||
|
|
||||||
|
shadowJar {
|
||||||
|
baseName = 'output-service-rdb'
|
||||||
|
classifier = null
|
||||||
|
version = null
|
||||||
|
}
|
4
gradle.properties
Normal file
4
gradle.properties
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
ktor_version=1.3.2
|
||||||
|
kotlin.code.style=official
|
||||||
|
kotlin_version=1.3.70
|
||||||
|
logback_version=1.2.1
|
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
Binary file not shown.
6
gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
6
gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#Sat Apr 04 18:02:20 CEST 2020
|
||||||
|
distributionBase=GRADLE_USER_HOME
|
||||||
|
distributionPath=wrapper/dists
|
||||||
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
|
zipStorePath=wrapper/dists
|
||||||
|
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip
|
172
gradlew
vendored
Executable file
172
gradlew
vendored
Executable file
@ -0,0 +1,172 @@
|
|||||||
|
#!/usr/bin/env sh
|
||||||
|
|
||||||
|
##############################################################################
|
||||||
|
##
|
||||||
|
## Gradle start up script for UN*X
|
||||||
|
##
|
||||||
|
##############################################################################
|
||||||
|
|
||||||
|
# Attempt to set APP_HOME
|
||||||
|
# Resolve links: $0 may be a link
|
||||||
|
PRG="$0"
|
||||||
|
# Need this for relative symlinks.
|
||||||
|
while [ -h "$PRG" ] ; do
|
||||||
|
ls=`ls -ld "$PRG"`
|
||||||
|
link=`expr "$ls" : '.*-> \(.*\)$'`
|
||||||
|
if expr "$link" : '/.*' > /dev/null; then
|
||||||
|
PRG="$link"
|
||||||
|
else
|
||||||
|
PRG=`dirname "$PRG"`"/$link"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
SAVED="`pwd`"
|
||||||
|
cd "`dirname \"$PRG\"`/" >/dev/null
|
||||||
|
APP_HOME="`pwd -P`"
|
||||||
|
cd "$SAVED" >/dev/null
|
||||||
|
|
||||||
|
APP_NAME="Gradle"
|
||||||
|
APP_BASE_NAME=`basename "$0"`
|
||||||
|
|
||||||
|
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||||
|
DEFAULT_JVM_OPTS=""
|
||||||
|
|
||||||
|
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||||
|
MAX_FD="maximum"
|
||||||
|
|
||||||
|
warn () {
|
||||||
|
echo "$*"
|
||||||
|
}
|
||||||
|
|
||||||
|
die () {
|
||||||
|
echo
|
||||||
|
echo "$*"
|
||||||
|
echo
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# OS specific support (must be 'true' or 'false').
|
||||||
|
cygwin=false
|
||||||
|
msys=false
|
||||||
|
darwin=false
|
||||||
|
nonstop=false
|
||||||
|
case "`uname`" in
|
||||||
|
CYGWIN* )
|
||||||
|
cygwin=true
|
||||||
|
;;
|
||||||
|
Darwin* )
|
||||||
|
darwin=true
|
||||||
|
;;
|
||||||
|
MINGW* )
|
||||||
|
msys=true
|
||||||
|
;;
|
||||||
|
NONSTOP* )
|
||||||
|
nonstop=true
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
||||||
|
|
||||||
|
# Determine the Java command to use to start the JVM.
|
||||||
|
if [ -n "$JAVA_HOME" ] ; then
|
||||||
|
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
||||||
|
# IBM's JDK on AIX uses strange locations for the executables
|
||||||
|
JAVACMD="$JAVA_HOME/jre/sh/java"
|
||||||
|
else
|
||||||
|
JAVACMD="$JAVA_HOME/bin/java"
|
||||||
|
fi
|
||||||
|
if [ ! -x "$JAVACMD" ] ; then
|
||||||
|
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
||||||
|
|
||||||
|
Please set the JAVA_HOME variable in your environment to match the
|
||||||
|
location of your Java installation."
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
JAVACMD="java"
|
||||||
|
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||||
|
|
||||||
|
Please set the JAVA_HOME variable in your environment to match the
|
||||||
|
location of your Java installation."
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Increase the maximum file descriptors if we can.
|
||||||
|
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
|
||||||
|
MAX_FD_LIMIT=`ulimit -H -n`
|
||||||
|
if [ $? -eq 0 ] ; then
|
||||||
|
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
|
||||||
|
MAX_FD="$MAX_FD_LIMIT"
|
||||||
|
fi
|
||||||
|
ulimit -n $MAX_FD
|
||||||
|
if [ $? -ne 0 ] ; then
|
||||||
|
warn "Could not set maximum file descriptor limit: $MAX_FD"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# For Darwin, add options to specify how the application appears in the dock
|
||||||
|
if $darwin; then
|
||||||
|
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
|
||||||
|
fi
|
||||||
|
|
||||||
|
# For Cygwin, switch paths to Windows format before running java
|
||||||
|
if $cygwin ; then
|
||||||
|
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
|
||||||
|
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
|
||||||
|
JAVACMD=`cygpath --unix "$JAVACMD"`
|
||||||
|
|
||||||
|
# We build the pattern for arguments to be converted via cygpath
|
||||||
|
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
|
||||||
|
SEP=""
|
||||||
|
for dir in $ROOTDIRSRAW ; do
|
||||||
|
ROOTDIRS="$ROOTDIRS$SEP$dir"
|
||||||
|
SEP="|"
|
||||||
|
done
|
||||||
|
OURCYGPATTERN="(^($ROOTDIRS))"
|
||||||
|
# Add a user-defined pattern to the cygpath arguments
|
||||||
|
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
|
||||||
|
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
|
||||||
|
fi
|
||||||
|
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
||||||
|
i=0
|
||||||
|
for arg in "$@" ; do
|
||||||
|
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
|
||||||
|
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
|
||||||
|
|
||||||
|
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
|
||||||
|
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
|
||||||
|
else
|
||||||
|
eval `echo args$i`="\"$arg\""
|
||||||
|
fi
|
||||||
|
i=$((i+1))
|
||||||
|
done
|
||||||
|
case $i in
|
||||||
|
(0) set -- ;;
|
||||||
|
(1) set -- "$args0" ;;
|
||||||
|
(2) set -- "$args0" "$args1" ;;
|
||||||
|
(3) set -- "$args0" "$args1" "$args2" ;;
|
||||||
|
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
|
||||||
|
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
|
||||||
|
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
|
||||||
|
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
|
||||||
|
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
|
||||||
|
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Escape application args
|
||||||
|
save () {
|
||||||
|
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
|
||||||
|
echo " "
|
||||||
|
}
|
||||||
|
APP_ARGS=$(save "$@")
|
||||||
|
|
||||||
|
# Collect all arguments for the java command, following the shell quoting and substitution rules
|
||||||
|
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
|
||||||
|
|
||||||
|
# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
|
||||||
|
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
|
||||||
|
cd "$(dirname "$0")"
|
||||||
|
fi
|
||||||
|
|
||||||
|
exec "$JAVACMD" "$@"
|
84
gradlew.bat
vendored
Normal file
84
gradlew.bat
vendored
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
@if "%DEBUG%" == "" @echo off
|
||||||
|
@rem ##########################################################################
|
||||||
|
@rem
|
||||||
|
@rem Gradle startup script for Windows
|
||||||
|
@rem
|
||||||
|
@rem ##########################################################################
|
||||||
|
|
||||||
|
@rem Set local scope for the variables with windows NT shell
|
||||||
|
if "%OS%"=="Windows_NT" setlocal
|
||||||
|
|
||||||
|
set DIRNAME=%~dp0
|
||||||
|
if "%DIRNAME%" == "" set DIRNAME=.
|
||||||
|
set APP_BASE_NAME=%~n0
|
||||||
|
set APP_HOME=%DIRNAME%
|
||||||
|
|
||||||
|
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||||
|
set DEFAULT_JVM_OPTS=
|
||||||
|
|
||||||
|
@rem Find java.exe
|
||||||
|
if defined JAVA_HOME goto findJavaFromJavaHome
|
||||||
|
|
||||||
|
set JAVA_EXE=java.exe
|
||||||
|
%JAVA_EXE% -version >NUL 2>&1
|
||||||
|
if "%ERRORLEVEL%" == "0" goto init
|
||||||
|
|
||||||
|
echo.
|
||||||
|
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||||
|
echo.
|
||||||
|
echo Please set the JAVA_HOME variable in your environment to match the
|
||||||
|
echo location of your Java installation.
|
||||||
|
|
||||||
|
goto fail
|
||||||
|
|
||||||
|
:findJavaFromJavaHome
|
||||||
|
set JAVA_HOME=%JAVA_HOME:"=%
|
||||||
|
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
||||||
|
|
||||||
|
if exist "%JAVA_EXE%" goto init
|
||||||
|
|
||||||
|
echo.
|
||||||
|
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
||||||
|
echo.
|
||||||
|
echo Please set the JAVA_HOME variable in your environment to match the
|
||||||
|
echo location of your Java installation.
|
||||||
|
|
||||||
|
goto fail
|
||||||
|
|
||||||
|
:init
|
||||||
|
@rem Get command-line arguments, handling Windows variants
|
||||||
|
|
||||||
|
if not "%OS%" == "Windows_NT" goto win9xME_args
|
||||||
|
|
||||||
|
:win9xME_args
|
||||||
|
@rem Slurp the command line arguments.
|
||||||
|
set CMD_LINE_ARGS=
|
||||||
|
set _SKIP=2
|
||||||
|
|
||||||
|
:win9xME_args_slurp
|
||||||
|
if "x%~1" == "x" goto execute
|
||||||
|
|
||||||
|
set CMD_LINE_ARGS=%*
|
||||||
|
|
||||||
|
:execute
|
||||||
|
@rem Setup the command line
|
||||||
|
|
||||||
|
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
||||||
|
|
||||||
|
@rem Execute Gradle
|
||||||
|
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
|
||||||
|
|
||||||
|
:end
|
||||||
|
@rem End local scope for the variables with windows NT shell
|
||||||
|
if "%ERRORLEVEL%"=="0" goto mainEnd
|
||||||
|
|
||||||
|
:fail
|
||||||
|
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
||||||
|
rem the _cmd.exe /c_ return code!
|
||||||
|
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
|
||||||
|
exit /b 1
|
||||||
|
|
||||||
|
:mainEnd
|
||||||
|
if "%OS%"=="Windows_NT" endlocal
|
||||||
|
|
||||||
|
:omega
|
8
http-client.env.json
Normal file
8
http-client.env.json
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"localhost": {
|
||||||
|
"host": "http://127.0.0.1:8080",
|
||||||
|
"param_dateAfter": "dateAfter",
|
||||||
|
"param_dateBefore": "dateBefore",
|
||||||
|
"param_tagID": "tagID"
|
||||||
|
}
|
||||||
|
}
|
9
resources/application.conf
Normal file
9
resources/application.conf
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
ktor {
|
||||||
|
deployment {
|
||||||
|
port = 8080
|
||||||
|
port = ${?PORT}
|
||||||
|
}
|
||||||
|
application {
|
||||||
|
modules = [ com.kmalbz.ApplicationKt.module ]
|
||||||
|
}
|
||||||
|
}
|
12
resources/logback.xml
Normal file
12
resources/logback.xml
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<configuration>
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
<root level="trace">
|
||||||
|
<appender-ref ref="STDOUT"/>
|
||||||
|
</root>
|
||||||
|
<logger name="org.eclipse.jetty" level="INFO"/>
|
||||||
|
<logger name="io.netty" level="INFO"/>
|
||||||
|
</configuration>
|
1
settings.gradle
Normal file
1
settings.gradle
Normal file
@ -0,0 +1 @@
|
|||||||
|
rootProject.name = "output-sevice-rdb"
|
73
src/Application.kt
Normal file
73
src/Application.kt
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
package com.kmalbz
|
||||||
|
|
||||||
|
import io.ktor.application.*
|
||||||
|
import io.ktor.response.*
|
||||||
|
import io.ktor.request.*
|
||||||
|
import io.ktor.routing.*
|
||||||
|
import io.ktor.http.*
|
||||||
|
import io.ktor.gson.*
|
||||||
|
import io.ktor.features.*
|
||||||
|
import io.ktor.client.*
|
||||||
|
import io.ktor.client.engine.apache.*
|
||||||
|
import com.fasterxml.jackson.databind.*
|
||||||
|
import io.ktor.jackson.*
|
||||||
|
import io.ktor.auth.*
|
||||||
|
import kotlin.reflect.*
|
||||||
|
import java.util.*
|
||||||
|
import io.ktor.swagger.experimental.*
|
||||||
|
|
||||||
|
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
|
||||||
|
|
||||||
|
@Suppress("unused") // Referenced in application.conf
|
||||||
|
@kotlin.jvm.JvmOverloads
|
||||||
|
fun Application.module(testing: Boolean = false) {
|
||||||
|
install(ContentNegotiation) {
|
||||||
|
gson {
|
||||||
|
}
|
||||||
|
|
||||||
|
jackson {
|
||||||
|
enable(SerializationFeature.INDENT_OUTPUT)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val client = HttpClient(Apache) {
|
||||||
|
}
|
||||||
|
|
||||||
|
install(Authentication) {
|
||||||
|
}
|
||||||
|
|
||||||
|
routing {
|
||||||
|
get("/") {
|
||||||
|
call.respondText("HELLO WORLD!", contentType = ContentType.Text.Plain)
|
||||||
|
}
|
||||||
|
|
||||||
|
get("/json/gson") {
|
||||||
|
call.respond(mapOf("hello" to "world"))
|
||||||
|
}
|
||||||
|
|
||||||
|
install(StatusPages) {
|
||||||
|
exception<AuthenticationException> { cause ->
|
||||||
|
call.respond(HttpStatusCode.Unauthorized)
|
||||||
|
}
|
||||||
|
exception<AuthorizationException> { cause ->
|
||||||
|
call.respond(HttpStatusCode.Forbidden)
|
||||||
|
}
|
||||||
|
|
||||||
|
exception<HttpException> { cause ->
|
||||||
|
call.respond(cause.code, cause.description)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
get("/json/jackson") {
|
||||||
|
call.respond(mapOf("hello" to "world"))
|
||||||
|
}
|
||||||
|
|
||||||
|
OutputServiceRDBServer().apply {
|
||||||
|
registerOutput()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class AuthenticationException : RuntimeException()
|
||||||
|
class AuthorizationException : RuntimeException()
|
||||||
|
|
17
src/OutputServiceRDB.kt
Normal file
17
src/OutputServiceRDB.kt
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package com.kmalbz
|
||||||
|
|
||||||
|
import java.util.*
|
||||||
|
import io.ktor.http.*
|
||||||
|
import io.ktor.request.*
|
||||||
|
import io.ktor.swagger.experimental.*
|
||||||
|
|
||||||
|
data class OutputObject(
|
||||||
|
val tag: String,
|
||||||
|
val decison: Boolean,
|
||||||
|
val date: Date
|
||||||
|
)
|
||||||
|
|
||||||
|
data class ApiResponse(
|
||||||
|
val status: String,
|
||||||
|
val message: String
|
||||||
|
)
|
75
src/OutputServiceRDBClient.kt
Normal file
75
src/OutputServiceRDBClient.kt
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
package com.kmalbz
|
||||||
|
|
||||||
|
import io.ktor.client.*
|
||||||
|
import io.ktor.client.request.*
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Output Service - RDB Client
|
||||||
|
*
|
||||||
|
* This is the feature extraction interface of the Birbnetes system.
|
||||||
|
*/
|
||||||
|
open class OutputServiceRDBClient(val endpoint: String, val client: HttpClient = HttpClient()) {
|
||||||
|
/**
|
||||||
|
* Get all negative decision objects
|
||||||
|
*
|
||||||
|
* @return Array of decision objects
|
||||||
|
*/
|
||||||
|
suspend fun getallnegative(
|
||||||
|
): List<OutputObject> {
|
||||||
|
return client.get<List<OutputObject>>("$endpoint/output/filter/negative") {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get positive decision objects
|
||||||
|
*
|
||||||
|
* @return Array of decision objects
|
||||||
|
*/
|
||||||
|
suspend fun getallpositive(
|
||||||
|
): List<OutputObject> {
|
||||||
|
return client.get<List<OutputObject>>("$endpoint/output/filter/positive") {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get decision before a date
|
||||||
|
*
|
||||||
|
* @param dateAfter Date of filter
|
||||||
|
*
|
||||||
|
* @return Array of decision objects
|
||||||
|
*/
|
||||||
|
suspend fun getallafter(
|
||||||
|
dateAfter: Date // PATH
|
||||||
|
): List<OutputObject> {
|
||||||
|
return client.get<List<OutputObject>>("$endpoint/output/after/$dateAfter") {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get decision before a date
|
||||||
|
*
|
||||||
|
* @param dateBefore Date of filter
|
||||||
|
*
|
||||||
|
* @return Array of decision objects
|
||||||
|
*/
|
||||||
|
suspend fun getallbefore(
|
||||||
|
dateBefore: Date // PATH
|
||||||
|
): List<OutputObject> {
|
||||||
|
return client.get<List<OutputObject>>("$endpoint/output/before/$dateBefore") {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get decision by ID
|
||||||
|
*
|
||||||
|
* @param tagID ID of wave file
|
||||||
|
*
|
||||||
|
* @return Decision object
|
||||||
|
*/
|
||||||
|
suspend fun getDecision(
|
||||||
|
tagID: Int // PATH
|
||||||
|
): OutputObject {
|
||||||
|
return client.get<OutputObject>("$endpoint/output/$tagID") {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
61
src/OutputServiceRDBServer.kt
Normal file
61
src/OutputServiceRDBServer.kt
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
package com.kmalbz
|
||||||
|
|
||||||
|
import io.ktor.application.*
|
||||||
|
import io.ktor.response.*
|
||||||
|
import io.ktor.routing.*
|
||||||
|
import java.util.*
|
||||||
|
import io.ktor.swagger.experimental.*
|
||||||
|
import io.ktor.auth.*
|
||||||
|
import io.ktor.http.*
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Output Service - RDB
|
||||||
|
*
|
||||||
|
* This is the output interface of the Birbnetes system.
|
||||||
|
*/
|
||||||
|
class OutputServiceRDBServer() {
|
||||||
|
/**
|
||||||
|
* output
|
||||||
|
*/
|
||||||
|
fun Routing.registerOutput() {
|
||||||
|
get("/output/filter/negative") {
|
||||||
|
if (false) httpException(HttpStatusCode.NotFound)
|
||||||
|
|
||||||
|
call.respond(listOf())
|
||||||
|
}
|
||||||
|
|
||||||
|
get("/output/filter/positive") {
|
||||||
|
if (false) httpException(HttpStatusCode.NotFound)
|
||||||
|
|
||||||
|
call.respond(listOf())
|
||||||
|
}
|
||||||
|
|
||||||
|
get("/output/after/{dateAfter}") {
|
||||||
|
val dateAfter = call.getPath<Date>("dateAfter")
|
||||||
|
|
||||||
|
if (false) httpException(HttpStatusCode.NotFound)
|
||||||
|
|
||||||
|
call.respond(listOf())
|
||||||
|
}
|
||||||
|
|
||||||
|
get("/output/before/{dateBefore}") {
|
||||||
|
val dateBefore = call.getPath<Date>("dateBefore")
|
||||||
|
|
||||||
|
if (false) httpException(HttpStatusCode.NotFound)
|
||||||
|
|
||||||
|
call.respond(listOf())
|
||||||
|
}
|
||||||
|
|
||||||
|
get("/output/{tagID}") {
|
||||||
|
val tagID = call.getPath<Int>("tagID")
|
||||||
|
|
||||||
|
if (false) httpException(HttpStatusCode.NotFound)
|
||||||
|
|
||||||
|
call.respond(OutputObject(
|
||||||
|
tag = "tag",
|
||||||
|
decison = false,
|
||||||
|
date = Date()
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
395
src/io/ktor/swagger/experimental/SwaggerUtils.kt
Normal file
395
src/io/ktor/swagger/experimental/SwaggerUtils.kt
Normal file
@ -0,0 +1,395 @@
|
|||||||
|
package io.ktor.swagger.experimental
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.module.kotlin.*
|
||||||
|
import kotlin.coroutines.*
|
||||||
|
import kotlin.coroutines.intrinsics.*
|
||||||
|
import kotlinx.coroutines.*
|
||||||
|
import io.ktor.application.*
|
||||||
|
import io.ktor.auth.authenticate
|
||||||
|
import io.ktor.client.*
|
||||||
|
import io.ktor.client.call.*
|
||||||
|
import io.ktor.client.request.*
|
||||||
|
import io.ktor.client.response.*
|
||||||
|
import io.ktor.content.*
|
||||||
|
import io.ktor.http.*
|
||||||
|
import io.ktor.request.*
|
||||||
|
import io.ktor.response.*
|
||||||
|
import io.ktor.routing.*
|
||||||
|
import io.ktor.util.*
|
||||||
|
import java.lang.reflect.*
|
||||||
|
import java.lang.reflect.Type
|
||||||
|
|
||||||
|
class HttpException(val code: HttpStatusCode, val description: String = code.description) : RuntimeException(description)
|
||||||
|
|
||||||
|
fun httpException(code: HttpStatusCode, message: String = code.description): Nothing = throw HttpException(code, message)
|
||||||
|
fun httpException(code: Int, message: String = "Error $code"): Nothing = throw HttpException(HttpStatusCode(code, message))
|
||||||
|
@Suppress("unused")
|
||||||
|
inline fun <T> T.verifyParam(name: String, callback: (T) -> Boolean): T {
|
||||||
|
if (!callback(this)) throw IllegalArgumentException("$name"); return this
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun <T> T.checkRequest(cond: Boolean, callback: () -> String) {
|
||||||
|
if (!cond) httpException(HttpStatusCode.BadRequest, callback())
|
||||||
|
}
|
||||||
|
|
||||||
|
interface SwaggerBaseApi {
|
||||||
|
}
|
||||||
|
|
||||||
|
interface SwaggerBaseServer {
|
||||||
|
}
|
||||||
|
|
||||||
|
class ApplicationCallContext(val call: ApplicationCall) : CoroutineContext.Element {
|
||||||
|
object KEY : CoroutineContext.Key<ApplicationCallContext>
|
||||||
|
|
||||||
|
override val key: CoroutineContext.Key<*> = KEY
|
||||||
|
}
|
||||||
|
|
||||||
|
@Suppress("unused")
|
||||||
|
suspend fun SwaggerBaseServer.call(): ApplicationCall {
|
||||||
|
return coroutineContext[ApplicationCallContext.KEY]?.call ?: error("ApplicationCall not available")
|
||||||
|
}
|
||||||
|
|
||||||
|
annotation class Method(val method: String)
|
||||||
|
annotation class Body(val name: String)
|
||||||
|
annotation class Header(val name: String)
|
||||||
|
annotation class Query(val name: String)
|
||||||
|
annotation class Path(val name: String) // Reused
|
||||||
|
annotation class FormData(val name: String)
|
||||||
|
annotation class Auth(vararg val auths: String)
|
||||||
|
|
||||||
|
//interface FeatureClass
|
||||||
|
//annotation class Feature(val clazz: KClass<out FeatureClass>)
|
||||||
|
|
||||||
|
inline fun <reified T : SwaggerBaseApi> createClient(client: HttpClient, rootUrl: String): T =
|
||||||
|
createClient(T::class.java, client, rootUrl)
|
||||||
|
|
||||||
|
fun <T : SwaggerBaseApi> createClient(clazz: Class<T>, client: HttpClient, rootUrl: String): T {
|
||||||
|
val rootUrlTrim = rootUrl.trimEnd('/')
|
||||||
|
val apiClass = ApiClass.parse(clazz)
|
||||||
|
var authContext = LinkedHashMap<String, String>()
|
||||||
|
|
||||||
|
return Proxy.newProxyInstance(clazz.classLoader, arrayOf(clazz)) { proxy, method, args ->
|
||||||
|
val info = apiClass.getInfo(method) ?: error("Can't find method $method")
|
||||||
|
val rparams = info.params.zip(args.slice(0 until info.params.size)).map { ApiClass.ApiParamInfoValue<Any?>(it.first as ApiClass.ApiParamInfo<Any?>, it.second) }.associateBy { it.name }
|
||||||
|
|
||||||
|
//val params = method.parameters
|
||||||
|
val cont = args.lastOrNull() as? Continuation<Any>?
|
||||||
|
?: throw RuntimeException("Just implemented suspend functions")
|
||||||
|
|
||||||
|
val continuationReturnType = method.genericParameterTypes.firstOrNull()?.extractFirstGenericType()
|
||||||
|
|
||||||
|
val realReturnType = continuationReturnType ?: method.returnType
|
||||||
|
|
||||||
|
val pathPattern = info.path
|
||||||
|
|
||||||
|
val pathReplaced = pathPattern.replace { "${rparams[it]?.value}" }
|
||||||
|
|
||||||
|
kotlinx.coroutines.GlobalScope.apply {
|
||||||
|
launch {
|
||||||
|
try {
|
||||||
|
val fullUrl = "$rootUrlTrim/$pathReplaced"
|
||||||
|
val res = client.call(fullUrl) {
|
||||||
|
this.method = HttpMethod(info.httpMethod)
|
||||||
|
val body = linkedMapOf<String, Any?>()
|
||||||
|
val formData = linkedMapOf<String, Any?>()
|
||||||
|
for (param in rparams.values) {
|
||||||
|
when (param.source) {
|
||||||
|
Source.QUERY -> parameter(param.name, "${param.value}")
|
||||||
|
Source.HEADER -> header(param.name, "${param.value}")
|
||||||
|
Source.BODY -> body[param.name] = param.value
|
||||||
|
Source.FORM_DATA -> formData[param.name] = param.value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (body.isNotEmpty()) {
|
||||||
|
this.contentType(io.ktor.http.ContentType.Application.Json)
|
||||||
|
this.body = ByteArrayContent(Json.stringify(body).toByteArray(Charsets.UTF_8))
|
||||||
|
}
|
||||||
|
if (formData.isNotEmpty()) {
|
||||||
|
this.contentType(io.ktor.http.ContentType.Application.FormUrlEncoded)
|
||||||
|
this.body = ByteArrayContent(formData.map { it.key to it.value.toString() }.formUrlEncode().toByteArray(Charsets.UTF_8))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (res.response.status.value < 400) {
|
||||||
|
cont.resume(Json.parse(res.response.readText(), realReturnType))
|
||||||
|
} else {
|
||||||
|
throw HttpExceptionWithContent(res.response.status, res.response.readText())
|
||||||
|
}
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
cont.resumeWithException(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
COROUTINE_SUSPENDED
|
||||||
|
} as T
|
||||||
|
}
|
||||||
|
|
||||||
|
class HttpExceptionWithContent(val code: HttpStatusCode, val content: String) :
|
||||||
|
RuntimeException("HTTP ERROR $code : $content")
|
||||||
|
|
||||||
|
fun Routing.registerRoutes(server: SwaggerBaseServer) {
|
||||||
|
val clazz = ApiClass.parse(server::class.java)
|
||||||
|
|
||||||
|
for (method in clazz.methods) {
|
||||||
|
authenticateIfNotEmpty(method.auths) {
|
||||||
|
route(method.path.pathPattern, HttpMethod(method.httpMethod)) {
|
||||||
|
handle {
|
||||||
|
val args = arrayListOf<Any?>()
|
||||||
|
for (param in method.params) {
|
||||||
|
args += param.get(call)
|
||||||
|
}
|
||||||
|
withContext(ApplicationCallContext(call)) {
|
||||||
|
val result = method.method.invokeSuspend(server, args)
|
||||||
|
call.respondText(Json.stringify(result ?: Any()), ContentType.Application.Json)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum class Source {
|
||||||
|
BODY, QUERY, FORM_DATA, HEADER, PATH
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Route.authenticateIfNotEmpty(configurations: List<String>, optional: Boolean = false, build: Route.() -> Unit): Route {
|
||||||
|
return if (configurations.isEmpty()) {
|
||||||
|
build()
|
||||||
|
this
|
||||||
|
} else {
|
||||||
|
authenticate(*configurations.toTypedArray(), optional = optional, build = build)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class ApiClass(val clazz: Class<*>, val methods: List<ApiMethodInfo>) {
|
||||||
|
val methodsBySignature = methods.associateBy { it.methodSignature }
|
||||||
|
|
||||||
|
fun getInfo(method: java.lang.reflect.Method) = methodsBySignature[method.signature]
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun parse(clazz: Class<*>): ApiClass {
|
||||||
|
val imethods = arrayListOf<ApiMethodInfo>()
|
||||||
|
for (method in clazz.methods) {
|
||||||
|
val path = method.getAnnotationInAncestors(Path::class.java)?.name
|
||||||
|
val httpMethod = method.getAnnotationInAncestors(Method::class.java)?.method ?: "GET"
|
||||||
|
//println("METHOD: $method, $path")
|
||||||
|
if (path != null) {
|
||||||
|
val params = arrayListOf<ApiParamInfo<*>>()
|
||||||
|
for ((ptype, annotations) in method.parameterTypes.zip(method.parameterAnnotationsInAncestors)) {
|
||||||
|
// Skip the continuation last argument!
|
||||||
|
if (ptype.isAssignableFrom(Continuation::class.java)) continue
|
||||||
|
|
||||||
|
val body = annotations.filterIsInstance<Body>().firstOrNull()?.name
|
||||||
|
val query = annotations.filterIsInstance<Query>().firstOrNull()?.name
|
||||||
|
val formData = annotations.filterIsInstance<FormData>().firstOrNull()?.name
|
||||||
|
val header = annotations.filterIsInstance<Header>().firstOrNull()?.name
|
||||||
|
val ppath = annotations.filterIsInstance<Path>().firstOrNull()?.name
|
||||||
|
|
||||||
|
val source = when {
|
||||||
|
body != null -> Source.BODY
|
||||||
|
query != null -> Source.QUERY
|
||||||
|
formData != null -> Source.FORM_DATA
|
||||||
|
header != null -> Source.HEADER
|
||||||
|
ppath != null -> Source.PATH
|
||||||
|
else -> Source.QUERY
|
||||||
|
}
|
||||||
|
val rname = body ?: query ?: formData ?: header ?: ppath ?: "unknown"
|
||||||
|
|
||||||
|
//println(" - $ptype, ${annotations.toList()}")
|
||||||
|
|
||||||
|
params += ApiParamInfo(source, rname, ptype)
|
||||||
|
}
|
||||||
|
|
||||||
|
//println("METHOD: $instance, $method, $httpMethod, $path")
|
||||||
|
//for (param in params) println(" - $param")
|
||||||
|
|
||||||
|
val auths = method.getAnnotationInAncestors(Auth::class.java)?.auths?.toList() ?: listOf()
|
||||||
|
|
||||||
|
imethods += ApiMethodInfo(method, PathPattern(path.trim('/')), httpMethod, auths, params)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ApiClass(clazz, imethods)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ApiMethodInfo(val method: java.lang.reflect.Method, val path: PathPattern, val httpMethod: String, val auths: List<String>, val params: List<ApiParamInfo<*>>) {
|
||||||
|
val methodSignature = method.signature
|
||||||
|
}
|
||||||
|
|
||||||
|
data class ApiParamInfo<T>(val source: Source, val name: String, val type: Class<T>) {
|
||||||
|
suspend fun get(call: ApplicationCall): T {
|
||||||
|
return call.getTyped(source, name, type)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
data class ApiParamInfoValue<T>(val info: ApiParamInfo<T>, val value: T) {
|
||||||
|
val source get() = info.source
|
||||||
|
val type get() = info.type
|
||||||
|
val name get() = info.name
|
||||||
|
}
|
||||||
|
|
||||||
|
class PathPattern(val pathPattern: String) {
|
||||||
|
companion object {
|
||||||
|
val PARAM_REGEX = Regex("\\{(\\w*)\\}")
|
||||||
|
}
|
||||||
|
|
||||||
|
val pathNames by lazy { PARAM_REGEX.findAll(pathPattern).map { it.groupValues[1] }.toList() }
|
||||||
|
val pathRegex by lazy { Regex(replace { "(\\w+)" }) }
|
||||||
|
|
||||||
|
fun replace(replacer: (name: String) -> String): String {
|
||||||
|
return pathPattern.replace(PARAM_REGEX) { mr -> replacer(mr.groupValues[1]) }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun extract(path: String): List<String> {
|
||||||
|
return pathRegex.find(path)?.groupValues?.drop(1) ?: listOf()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
data class MethodSignature(val name: String, val types: List<Class<*>>)
|
||||||
|
|
||||||
|
val java.lang.reflect.Method.signature get() = MethodSignature(name, parameterTypes.toList())
|
||||||
|
|
||||||
|
object Json {
|
||||||
|
@PublishedApi
|
||||||
|
internal val objectMapper = jacksonObjectMapper()
|
||||||
|
|
||||||
|
fun <T> convert(value: Any?, clazz: Class<T>): T = objectMapper.convertValue(value, clazz)
|
||||||
|
fun <T> parse(str: String, clazz: Class<T>): T = objectMapper.readValue(str, clazz)
|
||||||
|
fun <T> stringify(value: T): String = objectMapper.writeValueAsString(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
//inline fun <reified T> ApplicationCall.getTyped(source: String, name: String): T =
|
||||||
|
// objectMapper.convertValue(getRaw(source, name), T::class.java)
|
||||||
|
suspend fun <T> ApplicationCall.getTyped(source: Source, name: String, clazz: Class<T>): T {
|
||||||
|
return Json.convert(getRaw(source, name), clazz)
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun <T> ApplicationCall.getTypedOrNull(source: Source, name: String, clazz: Class<T>): T? =
|
||||||
|
getRaw(source, name)?.let { Json.convert(it, clazz) }
|
||||||
|
|
||||||
|
suspend fun ApplicationCall.getRaw(source: Source, name: String): Any? {
|
||||||
|
return when (source) {
|
||||||
|
Source.PATH -> this.parameters.get(name)
|
||||||
|
Source.QUERY -> this.request.queryParameters.get(name)
|
||||||
|
Source.BODY -> this.getCachedUntypedBody()[name]
|
||||||
|
Source.FORM_DATA -> TODO()
|
||||||
|
Source.HEADER -> this.request.header(name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val CACHED_BODY_KEY = AttributeKey<Any>("CACHED_BODY_KEY")
|
||||||
|
|
||||||
|
inline fun <T : Any> Attributes.computeIfAbsentInline(key: AttributeKey<T>, block: () -> T): T {
|
||||||
|
if (!this.contains(key)) {
|
||||||
|
this.put(key, block())
|
||||||
|
}
|
||||||
|
return this[key]
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun ApplicationCall.getCachedUntypedBody(): Map<String, Any?> {
|
||||||
|
return attributes.computeIfAbsentInline(CACHED_BODY_KEY) {
|
||||||
|
Json.parse(this@getCachedUntypedBody.receive(), HashMap::class.java)
|
||||||
|
} as Map<String, Any?>
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////
|
||||||
|
// Reflection Tools
|
||||||
|
///////////////////////////////////////////////////
|
||||||
|
|
||||||
|
val java.lang.Class<*>.allTypes: Set<Class<*>>
|
||||||
|
get() {
|
||||||
|
val types = LinkedHashSet<Class<*>>()
|
||||||
|
val explore = arrayListOf(this)
|
||||||
|
while (explore.isNotEmpty()) {
|
||||||
|
val item = explore.removeAt(explore.size - 1) ?: continue
|
||||||
|
types += item
|
||||||
|
explore += item.superclass
|
||||||
|
explore += item.interfaces
|
||||||
|
}
|
||||||
|
return types
|
||||||
|
}
|
||||||
|
|
||||||
|
val java.lang.reflect.Method.parameterAnnotationsInAncestors: List<List<Annotation>>
|
||||||
|
get() {
|
||||||
|
val allMethods = this.declaringClass.allTypes.map {
|
||||||
|
try {
|
||||||
|
it.getDeclaredMethod(name, *parameterTypes) ?: null
|
||||||
|
} catch (e: NoSuchMethodException) {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}.filterNotNull()
|
||||||
|
val out = Array<ArrayList<Annotation>>(parameterTypes.size) { arrayListOf() }.toList()
|
||||||
|
for (method in allMethods) {
|
||||||
|
for ((index, annotations) in method.parameterAnnotations.withIndex()) {
|
||||||
|
out[index] += annotations
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun java.lang.reflect.Method.invokeSuspend(obj: Any?, args: List<Any?>): Any? = suspendCoroutine { c ->
|
||||||
|
val method = this@invokeSuspend
|
||||||
|
|
||||||
|
val lastParam = method.parameterTypes.lastOrNull()
|
||||||
|
val margs = java.util.ArrayList(args)
|
||||||
|
|
||||||
|
if (lastParam != null && lastParam.isAssignableFrom(Continuation::class.java)) {
|
||||||
|
margs += c
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
val result = method.invoke(obj, *margs.toTypedArray())
|
||||||
|
if (result != COROUTINE_SUSPENDED) {
|
||||||
|
c.resume(result)
|
||||||
|
}
|
||||||
|
} catch (e: InvocationTargetException) {
|
||||||
|
c.resumeWithException(e.targetException)
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
c.resumeWithException(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun <T : Annotation> java.lang.reflect.Method.getAnnotationInAncestors(clazz: Class<T>): T? {
|
||||||
|
val res = this.getAnnotation(clazz) ?: this.getDeclaredAnnotation(clazz)
|
||||||
|
if (res != null) return res
|
||||||
|
|
||||||
|
// Try interfaces
|
||||||
|
for (ifc in this.declaringClass.interfaces) {
|
||||||
|
return ignoreErrors { ifc?.getMethod(name, *parameterTypes)?.getAnnotationInAncestors(clazz) } ?: continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try ancestor
|
||||||
|
return ignoreErrors { this.declaringClass.superclass?.getMethod(name, *parameterTypes) }?.getAnnotationInAncestors(
|
||||||
|
clazz
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun <T> ignoreErrors(callback: () -> T): T? = try {
|
||||||
|
callback()
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Type.extractFirstGenericType(): Class<*> {
|
||||||
|
if (this is ParameterizedType) {
|
||||||
|
return this.actualTypeArguments.first().extractFirstGenericType()
|
||||||
|
}
|
||||||
|
if (this is WildcardType) {
|
||||||
|
val tt = this.lowerBounds.firstOrNull() ?: this.upperBounds.firstOrNull()
|
||||||
|
?: error("WildcardType without lower/upper bounds")
|
||||||
|
return tt.extractFirstGenericType()
|
||||||
|
}
|
||||||
|
if (this is Class<*>) {
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
error("Couldn't find right generic type")
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend inline fun <reified T : Any> ApplicationCall.getBodyParam(name: String, noinline default: () -> T = { error("mandatory $name") }): T =
|
||||||
|
getTypedOrNull(Source.BODY, name, T::class.java) ?: default()
|
||||||
|
|
||||||
|
suspend inline fun <reified T : Any> ApplicationCall.getPath(name: String, noinline default: () -> T = { error("mandatory $name") }): T =
|
||||||
|
getTypedOrNull(Source.PATH, name, T::class.java) ?: default()
|
||||||
|
|
||||||
|
suspend inline fun <reified T : Any> ApplicationCall.getQuery(name: String, noinline default: () -> T = { error("mandatory $name") }): T =
|
||||||
|
getTypedOrNull(Source.QUERY, name, T::class.java) ?: default()
|
31
test/ApplicationTest.kt
Normal file
31
test/ApplicationTest.kt
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package com.kmalbz
|
||||||
|
|
||||||
|
import io.ktor.application.*
|
||||||
|
import io.ktor.response.*
|
||||||
|
import io.ktor.request.*
|
||||||
|
import io.ktor.routing.*
|
||||||
|
import io.ktor.http.*
|
||||||
|
import io.ktor.gson.*
|
||||||
|
import io.ktor.features.*
|
||||||
|
import io.ktor.client.*
|
||||||
|
import io.ktor.client.engine.apache.*
|
||||||
|
import com.fasterxml.jackson.databind.*
|
||||||
|
import io.ktor.jackson.*
|
||||||
|
import io.ktor.auth.*
|
||||||
|
import kotlin.reflect.*
|
||||||
|
import java.util.*
|
||||||
|
import io.ktor.swagger.experimental.*
|
||||||
|
import kotlin.test.*
|
||||||
|
import io.ktor.server.testing.*
|
||||||
|
|
||||||
|
class ApplicationTest {
|
||||||
|
@Test
|
||||||
|
fun testRoot() {
|
||||||
|
withTestApplication({ module(testing = true) }) {
|
||||||
|
handleRequest(HttpMethod.Get, "/").apply {
|
||||||
|
assertEquals(HttpStatusCode.OK, response.status())
|
||||||
|
assertEquals("HELLO WORLD!", response.content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
98
test/OutputServiceRDBServerTest.kt
Normal file
98
test/OutputServiceRDBServerTest.kt
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
package com.kmalbz
|
||||||
|
|
||||||
|
import java.util.*
|
||||||
|
import io.ktor.config.*
|
||||||
|
import io.ktor.http.*
|
||||||
|
import io.ktor.request.*
|
||||||
|
import io.ktor.server.testing.*
|
||||||
|
import io.ktor.swagger.experimental.*
|
||||||
|
import kotlin.test.*
|
||||||
|
|
||||||
|
class SwaggerRoutesTest {
|
||||||
|
/**
|
||||||
|
* @see OutputServiceRDBServer.getallnegative
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
fun testGetallnegative() {
|
||||||
|
withTestApplication {
|
||||||
|
// @TODO: Adjust path as required
|
||||||
|
handleRequest(HttpMethod.Get, "/output/filter/negative") {
|
||||||
|
}.apply {
|
||||||
|
// @TODO: Your test here
|
||||||
|
assertEquals(HttpStatusCode.OK, response.status())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see OutputServiceRDBServer.getallpositive
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
fun testGetallpositive() {
|
||||||
|
withTestApplication {
|
||||||
|
// @TODO: Adjust path as required
|
||||||
|
handleRequest(HttpMethod.Get, "/output/filter/positive") {
|
||||||
|
}.apply {
|
||||||
|
// @TODO: Your test here
|
||||||
|
assertEquals(HttpStatusCode.OK, response.status())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see OutputServiceRDBServer.getallafter
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
fun testGetallafter() {
|
||||||
|
withTestApplication {
|
||||||
|
// @TODO: Adjust path as required
|
||||||
|
handleRequest(HttpMethod.Get, "/output/after/{dateAfter}") {
|
||||||
|
}.apply {
|
||||||
|
// @TODO: Your test here
|
||||||
|
assertEquals(HttpStatusCode.OK, response.status())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see OutputServiceRDBServer.getallbefore
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
fun testGetallbefore() {
|
||||||
|
withTestApplication {
|
||||||
|
// @TODO: Adjust path as required
|
||||||
|
handleRequest(HttpMethod.Get, "/output/before/{dateBefore}") {
|
||||||
|
}.apply {
|
||||||
|
// @TODO: Your test here
|
||||||
|
assertEquals(HttpStatusCode.OK, response.status())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see OutputServiceRDBServer.getDecision
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
fun testGetDecision() {
|
||||||
|
withTestApplication {
|
||||||
|
// @TODO: Adjust path as required
|
||||||
|
handleRequest(HttpMethod.Get, "/output/{tagID}") {
|
||||||
|
}.apply {
|
||||||
|
// @TODO: Your test here
|
||||||
|
assertEquals(HttpStatusCode.OK, response.status())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun <R> withTestApplication(test: TestApplicationEngine.() -> R): R {
|
||||||
|
return withApplication(createTestEnvironment()) {
|
||||||
|
(environment.config as MapApplicationConfig).apply {
|
||||||
|
put("jwt.secret", "TODO-change-this-supersecret-or-use-SECRET-env")
|
||||||
|
}
|
||||||
|
application.module()
|
||||||
|
test()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun TestApplicationRequest.setBodyJson(value: Any?) = setBody(Json.stringify(value))
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user