├── .github ├── config │ └── logback.xml ├── renovate.json ├── scripts │ └── test.sh └── workflows │ ├── develop.yml │ ├── release.yml │ └── test_install_script.yml ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── NOTICE ├── README.md ├── build.gradle.kts ├── gradle.properties ├── gradle ├── libs.versions.toml └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── install.sh ├── scripting-definition ├── build.gradle.kts └── src │ └── main │ └── kotlin │ ├── ScriptDefinition.kt │ ├── annotation │ └── Import.kt │ └── script │ └── ServerScript.kt ├── scripting-host ├── build.gradle.kts └── src │ ├── main │ ├── kotlin │ │ ├── CacheImpl.kt │ │ ├── FCGIRecord.kt │ │ ├── FCGIRequestMessage.kt │ │ ├── FCGIResponseMessage.kt │ │ ├── KeyValuePair.kt │ │ ├── Main.kt │ │ ├── ManagementRequestState.kt │ │ ├── RequestState.kt │ │ ├── ScriptingHost.kt │ │ └── okio.kt │ └── resources │ │ ├── kss.properties │ │ └── logback.xml │ ├── release │ ├── config │ │ ├── kss.properties.sample │ │ └── logback.xml.sample │ └── service │ │ ├── kss.plist │ │ └── kss.service │ └── test │ ├── kotlin │ ├── ServerScriptingHostCacheTest.kt │ ├── ServerScriptingHostScriptsTest.kt │ └── TestUtils.kt │ └── resources │ ├── big_output.body │ ├── big_output.server.kts │ ├── cache_data.body │ ├── cache_data.server.kts │ ├── cache_import_script_1.body │ ├── cache_import_script_1.server.kts │ ├── cache_import_script_2.body │ ├── cache_import_script_2.server.kts │ ├── custom_headers.body │ ├── custom_headers.header │ ├── custom_headers.server.kts │ ├── dependency.body │ ├── dependency.server.kts │ ├── dependency_repository.body │ ├── dependency_repository.server.kts │ ├── empty.server.kts │ ├── exception.header │ ├── exception.server.kts │ ├── exception.status │ ├── import_function.body │ ├── import_function.server.kts │ ├── import_invalid.header │ ├── import_invalid.server.kts │ ├── import_invalid.status │ ├── import_not_existent.header │ ├── import_not_existent.server.kts │ ├── import_not_existent.status │ ├── import_script.body │ ├── import_script.header │ ├── import_script.server.kts │ ├── imports │ ├── invocation_counter.server.kts │ ├── no_cache_control.server.kts │ └── to_json.server.kts │ ├── invalid.header │ ├── invalid.server.kts │ ├── invalid.status │ ├── known_status.body │ ├── known_status.header │ ├── known_status.server.kts │ ├── multiple_output.body │ ├── multiple_output.server.kts │ ├── simple_script.body │ ├── simple_script.server.kts │ ├── unknown_status.body │ ├── unknown_status.header │ └── unknown_status.server.kts └── settings.gradle.kts /.github/config/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | log.txt 26 | 27 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%kvp-%mdc- %msg%n 28 | 29 | 30 | 31 | 32 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%kvp-%mdc- %msg%n 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /.github/renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://docs.renovatebot.com/renovate-schema.json", 3 | "extends": [ 4 | "config:recommended" 5 | ], 6 | "kotlin-script": { 7 | "fileMatch": ["^.+\\.server\\.kts$"], 8 | "ignorePaths": [] 9 | }, 10 | "labels": ["dependencies"] 11 | } 12 | -------------------------------------------------------------------------------- /.github/scripts/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # 4 | # Copyright 2024 Eduard Wolf 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | set -eu 20 | set -o pipefail 21 | 22 | html_dir="$1" 23 | 24 | cp -R scripting-host/src/test/resources/imports "$html_dir" 25 | 26 | for file in scripting-host/src/test/resources/*.server.kts; do 27 | cp "$file" "$html_dir" 28 | test_file_name=$(basename "$file") 29 | test_name="${test_file_name%%.server.kts}" 30 | expected_body="${file%%.server.kts}.body" 31 | header_file="${file%%.server.kts}.header" 32 | expected_header='expected_header.txt' 33 | if [ -f "$header_file" ]; then 34 | sed 's%Status:%HTTP/1.1 %g' "$header_file" > "$expected_header" 35 | else 36 | echo "HTTP/1.1 200 OK" > "$expected_header" 37 | fi 38 | echo "test $test_name" 39 | 40 | function test_request() { 41 | expected="$1" 42 | curl --request GET -s \ 43 | --url "http://localhost:8080/$test_name.kts" \ 44 | --output 'test_result.txt' \ 45 | --dump-header 'test_headers.txt' 46 | if [ -f "$expected" ]; then 47 | diff "$expected" 'test_result.txt' 48 | elif [ -s 'test_result.txt' ]; then 49 | echo 'expected empty body but got' 50 | cat 'test_result.txt' 51 | fi 52 | if [[ -n "$expected_header" ]]; then 53 | missing_lines=$(diff --ignore-all-space -U 0 expected_header.txt test_headers.txt | tail -n +3 | grep -c '^-' || true) 54 | if [ "$missing_lines" -gt 0 ]; then 55 | echo "expected headers [$(cat expected_header.txt)] but found [$(cat test_headers.txt)]" 56 | exit 1 57 | fi 58 | fi 59 | } 60 | if [ "$test_name" == 'cache_data' ]; then 61 | for (( i = 1; i <= 5; i++ )); do 62 | sed "s/{counter}/$i/g" "$expected_body" > 'expected_result.txt' 63 | test_request 'expected_result.txt' 64 | done 65 | else 66 | test_request "$expected_body" 67 | fi 68 | done 69 | -------------------------------------------------------------------------------- /.github/workflows/develop.yml: -------------------------------------------------------------------------------- 1 | name: Develop 2 | 3 | on: 4 | workflow_dispatch: 5 | pull_request: 6 | push: 7 | branches: 8 | - main 9 | 10 | jobs: 11 | build: 12 | runs-on: ubuntu-24.04 13 | steps: 14 | - name: checkout 15 | uses: actions/checkout@v4.2.2 16 | 17 | - name: Setup JDK 18 | uses: actions/setup-java@v4.6.0 19 | with: 20 | distribution: 'temurin' 21 | java-version: 17 22 | 23 | - name: Setup Gradle 24 | uses: gradle/actions/setup-gradle@v4.2.2 25 | 26 | - name: Build 27 | run: | 28 | ./gradlew build 29 | mv scripting-host/build/distributions/scripting-host-release*.tar.gz scripting-host-release.tar.gz 30 | 31 | - name: Upload build 32 | uses: actions/upload-artifact@v4.6.0 33 | with: 34 | name: release-artifact 35 | path: scripting-host-release.tar.gz 36 | compression-level: '0' 37 | retention-days: 7 38 | 39 | test: 40 | runs-on: ubuntu-24.04 41 | needs: [build] 42 | steps: 43 | - name: checkout 44 | uses: actions/checkout@v4.2.2 45 | 46 | - name: Setup JDK 47 | uses: actions/setup-java@v4.6.0 48 | with: 49 | distribution: 'temurin' 50 | java-version: 17 51 | 52 | - name: Download build 53 | uses: actions/download-artifact@v4.1.8 54 | with: 55 | name: release-artifact 56 | 57 | - name: Untar release artifact 58 | run: | 59 | mkdir scripting-host-release 60 | tar --extract --gunzip --directory scripting-host-release --strip-components 1 --file scripting-host-release.tar.gz 61 | 62 | - name: Test version output 63 | timeout-minutes: 1 64 | run: | 65 | version="$(cat gradle.properties | sed -n 's/version=//p')" 66 | hostVersion=$(./scripting-host-release/bin/kss --version | grep "Version: ") 67 | if [ "$hostVersion" != "Version: $version" ]; then 68 | echo "host version ($hostVersion) doesn't match gradle properties version ($version)" 69 | exit 1 70 | fi 71 | 72 | - name: Start nginx 73 | uses: nyurik/action-setup-nginx@v1.1 74 | id: start_ngingx 75 | with: 76 | conf-file-text: | 77 | user runner docker; 78 | worker_processes 1; 79 | events { 80 | worker_connections 1024; 81 | } 82 | http { 83 | include mime.types; 84 | default_type application/octet-stream; 85 | sendfile on; 86 | keepalive_timeout 65; 87 | server { 88 | listen 8080; 89 | server_name localhost; 90 | location ~ ^(.*)\.kts(\?.*)?$ { 91 | root html; 92 | try_files \$1.server.kts =404; 93 | include fastcgi_params; 94 | fastcgi_pass unix:$RUNNER_TEMP/kss.sock; 95 | } 96 | } 97 | } 98 | 99 | - name: Create test config 100 | run: | 101 | echo "socket.address=$RUNNER_TEMP/kss.sock" >> kss.properties 102 | cp .github/config/logback.xml kss.logback.xml 103 | echo "logging.logback.configurationFile=kss.logback.xml" >> kss.properties 104 | 105 | - name: Run tests on nginx 106 | uses: BerniWittmann/background-server-action@v1.1.1 107 | with: 108 | start: ./scripting-host-release/bin/kss 109 | wait-on: sleep 5 110 | command: ./.github/scripts/test.sh "${{ steps.start_ngingx.outputs.html-dir }}" 111 | 112 | - name: Test logfile not empty 113 | run: | 114 | if [ ! -f log.txt ]; then 115 | echo "log.txt file doesn't exist" 116 | exit 1 117 | fi 118 | if [ ! -s log.txt ]; then 119 | echo 'log.txt file is empty' 120 | exit 2 121 | fi 122 | 123 | - name: Print nginx error logs 124 | if: ${{ failure() }} 125 | run: cat "${{ steps.start_ngingx.outputs.error-log }}" 126 | 127 | - name: Print nginx access logs 128 | if: ${{ failure() }} 129 | run: cat "${{ steps.start_ngingx.outputs.access-log }}" 130 | 131 | - name: Print kss process logs 132 | if: ${{ failure() }} 133 | run: cat log.txt 134 | 135 | test_service: 136 | strategy: 137 | fail-fast: false 138 | matrix: 139 | runner: 140 | - ubuntu-24.04 141 | - macos-14 142 | runs-on: ${{ matrix.runner }} 143 | needs: build 144 | steps: 145 | - name: checkout 146 | uses: actions/checkout@v4.2.2 147 | 148 | - name: Setup JDK 149 | uses: actions/setup-java@v4.6.0 150 | with: 151 | distribution: 'temurin' 152 | java-version: 17 153 | 154 | # HOME directory needs to be accessible by _www, so the java executable can be used 155 | # not needed for linux, because JDK is not put in user directory 156 | - name: Set launchd path 157 | if: ${{ startsWith(matrix.runner, 'macos') }} 158 | run: | 159 | chmod o+rx "$HOME" 160 | sudo launchctl setenv JAVA_HOME "$JAVA_HOME" 161 | 162 | - name: Download build 163 | uses: actions/download-artifact@v4.1.8 164 | with: 165 | name: release-artifact 166 | 167 | - name: install latest version 168 | run: sudo ./install.sh --release-fetch-mode archive=scripting-host-release.tar.gz 169 | 170 | - name: setup runner config 171 | id: runner_config 172 | run: | 173 | if [[ '${{ matrix.runner }}' == ubuntu* ]] 174 | then 175 | echo 'config_dir=/usr/share/kss/' >> $GITHUB_OUTPUT 176 | echo 'start_cmd=systemctl start kss' >> $GITHUB_OUTPUT 177 | echo 'user=www-data' >> $GITHUB_OUTPUT 178 | else 179 | echo 'config_dir=/Library/Application Support/kss/' >> $GITHUB_OUTPUT 180 | echo 'start_cmd=launchctl load -w /Library/LaunchDaemons/kss.plist' >> $GITHUB_OUTPUT 181 | echo 'user=_www' >> $GITHUB_OUTPUT 182 | fi 183 | 184 | - name: setup socket 185 | run: | 186 | sudo mkdir -p /var/run/kss/ 187 | sudo chown '${{ steps.runner_config.outputs.user }}' /var/run/kss/ 188 | 189 | - name: Start kss 190 | run: | 191 | sudo cp .github/config/logback.xml '${{ steps.runner_config.outputs.config_dir }}kss.logback.xml' 192 | echo "logging.logback.configurationFile=${{ steps.runner_config.outputs.config_dir }}kss.logback.xml" >> kss.properties 193 | echo "dependencies.maven.homeDirectory=${{ steps.runner_config.outputs.config_dir }}.m2" >> kss.properties 194 | sudo chown '${{ steps.runner_config.outputs.user }}' '${{ steps.runner_config.outputs.config_dir }}' 195 | sudo mkdir '${{ steps.runner_config.outputs.config_dir }}.m2' 196 | sudo chown '${{ steps.runner_config.outputs.user }}' '${{ steps.runner_config.outputs.config_dir }}.m2' 197 | sudo mv kss.properties '${{ steps.runner_config.outputs.config_dir }}' 198 | sudo ${{ steps.runner_config.outputs.start_cmd }} 199 | sleep 2 200 | 201 | - name: Start nginx 202 | uses: nyurik/action-setup-nginx@v1.1 203 | id: start_ngingx 204 | with: 205 | conf-file-text: | 206 | user ${{ steps.runner_config.outputs.user }}; 207 | worker_processes 1; 208 | events { 209 | worker_connections 1024; 210 | } 211 | http { 212 | include mime.types; 213 | default_type application/octet-stream; 214 | sendfile on; 215 | keepalive_timeout 65; 216 | server { 217 | listen 8080; 218 | server_name localhost; 219 | location ~ ^(.*)\.kts(\?.*)?$ { 220 | root html; 221 | try_files \$1.server.kts =404; 222 | include fastcgi_params; 223 | fastcgi_pass unix:/var/run/kss/kss.sock; 224 | } 225 | } 226 | } 227 | 228 | - name: run tests 229 | run: ./.github/scripts/test.sh "${{ steps.start_ngingx.outputs.html-dir }}" 230 | 231 | - name: Print nginx error logs 232 | if: ${{ failure() }} 233 | run: cat "${{ steps.start_ngingx.outputs.error-log }}" 234 | 235 | - name: Print nginx access logs 236 | if: ${{ failure() }} 237 | run: cat "${{ steps.start_ngingx.outputs.access-log }}" 238 | 239 | - name: Print kss process logs 240 | if: ${{ failure() }} 241 | run: cat '${{ steps.runner_config.outputs.config_dir }}log.txt' 242 | 243 | - name: Print macos kss process logs 244 | if: ${{ failure() && startsWith(matrix.runner, 'macos') }} 245 | run: | 246 | if [ -f '/Library/Logs/kss.log' ] 247 | then 248 | cat '/Library/Logs/kss.log' 249 | fi 250 | 251 | - name: Print macos kss error logs 252 | if: ${{ failure() && startsWith(matrix.runner, 'macos') }} 253 | run: | 254 | if [ -f '/Library/Logs/kss-error.log' ] 255 | then 256 | cat '/Library/Logs/kss-error.log' 257 | fi 258 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Create Release 2 | 3 | on: 4 | push: 5 | tags: 6 | - '*' 7 | 8 | jobs: 9 | createRelease: 10 | runs-on: ubuntu-24.04 11 | permissions: 12 | contents: write 13 | steps: 14 | - name: checkout 15 | uses: actions/checkout@v4.2.2 16 | 17 | - name: set up JDK 18 | uses: actions/setup-java@v4.6.0 19 | with: 20 | distribution: 'temurin' 21 | java-version: 17 22 | 23 | - name: Build 24 | run: ./gradlew assembleReleaseDist 25 | 26 | - name: Create info variables 27 | id: variables 28 | run: | 29 | gradleVersion="$(cat gradle.properties | sed -n 's/version=//p')" 30 | gitVersion="${GITHUB_REF#refs/*/}" 31 | if [ "$gradleVersion" != "$gitVersion" ]; then 32 | echo "gradle properties version ($gradleVersion) doesn't match git tag version ($gitVersion)" 33 | exit 1 34 | fi 35 | echo "version=$gitVersion" >> $GITHUB_OUTPUT 36 | 37 | - name: Get Changelog 38 | id: changelog 39 | uses: mindsers/changelog-reader-action@v2.2.3 40 | with: 41 | version: ${{ steps.variables.outputs.version }} 42 | validation_level: error 43 | 44 | - name: Create Release 45 | uses: softprops/action-gh-release@v2 46 | with: 47 | body: "${{ steps.changelog.outputs.changes }}" 48 | fail_on_unmatched_files: true 49 | files: | 50 | scripting-host/build/distributions/scripting-host-release*.tar.gz 51 | scripting-host/build/distributions/scripting-host-release*.zip 52 | draft: false 53 | prerelease: false 54 | -------------------------------------------------------------------------------- /.github/workflows/test_install_script.yml: -------------------------------------------------------------------------------- 1 | name: Test Install Script 2 | 3 | on: 4 | pull_request: 5 | paths: 6 | - 'install.sh' 7 | - '.github/workflows/test_install_script.yml' 8 | release: 9 | types: [released] 10 | 11 | jobs: 12 | test_install_script: 13 | strategy: 14 | matrix: 15 | runner: 16 | - ubuntu-24.04 17 | - macos-14 18 | version: 19 | - '0.3.0' 20 | - '0.4.0' 21 | - '0.5.0' 22 | - '0.6.0' 23 | - '' 24 | exclude: 25 | - runner: macos-14 26 | version: 0.3.0 27 | runs-on: ${{ matrix.runner }} 28 | steps: 29 | - name: checkout 30 | uses: actions/checkout@v4.2.2 31 | 32 | - name: Setup JDK 33 | uses: actions/setup-java@v4.6.0 34 | with: 35 | distribution: 'temurin' 36 | java-version: 17 37 | 38 | # HOME directory needs to be accessible by _www, so the java executable can be used 39 | # not needed for linux, because JDK is not put in user directory 40 | - name: Set launchd path 41 | if: ${{ startsWith(matrix.runner, 'macos') }} 42 | run: | 43 | chmod o+rx "$HOME" 44 | sudo launchctl setenv JAVA_HOME "$JAVA_HOME" 45 | 46 | - name: install latest version 47 | if: ${{ matrix.version == '' }} 48 | run: sudo --preserve-env ./install.sh 49 | env: 50 | GH_TOKEN: ${{ github.token }} 51 | 52 | - name: install specific version 53 | if: ${{ matrix.version != '' }} 54 | run: sudo --preserve-env ./install.sh --release-version ${{ matrix.version }} 55 | env: 56 | GH_TOKEN: ${{ github.token }} 57 | 58 | - name: setup socket 59 | run: | 60 | sudo mkdir -p /var/run/kss/ 61 | sudo chmod a+w /var/run/kss/ 62 | 63 | - name: add timeout command to osx 64 | if: ${{ startsWith(matrix.runner, 'macos') }} 65 | run: | 66 | brew install coreutils 67 | sudo ln -s /usr/local/bin/gtimeout /usr/local/bin/timeout 68 | 69 | - name: check install 70 | # 124 is cancelled after timeout 71 | run: | 72 | timeout 5s kss || exit_code=$? 73 | if [ "$exit_code" -ne 124 ] 74 | then 75 | echo "expected exit code 124 but was $exit_code" 76 | exit "$exit_code" 77 | fi 78 | 79 | - name: setup runner config 80 | id: runner_config 81 | run: | 82 | if [[ '${{ matrix.runner }}' == ubuntu* ]] 83 | then 84 | echo 'config_dir=/usr/share/kss/' >> $GITHUB_OUTPUT 85 | echo 'start_cmd=systemctl start kss' >> $GITHUB_OUTPUT 86 | echo 'user=www-data' >> $GITHUB_OUTPUT 87 | else 88 | echo 'config_dir=/Library/Application Support/kss/' >> $GITHUB_OUTPUT 89 | echo 'start_cmd=launchctl load -w /Library/LaunchDaemons/kss.plist' >> $GITHUB_OUTPUT 90 | echo 'user=_www' >> $GITHUB_OUTPUT 91 | fi 92 | 93 | - name: setup socket 94 | run: | 95 | sudo mkdir -p /var/run/kss/ 96 | sudo chown '${{ steps.runner_config.outputs.user }}' /var/run/kss/ 97 | 98 | - name: Start kss 99 | run: | 100 | sudo cp .github/config/logback.xml '${{ steps.runner_config.outputs.config_dir }}kss.logback.xml' 101 | echo "logging.logback.configurationFile=${{ steps.runner_config.outputs.config_dir }}kss.logback.xml" >> kss.properties 102 | echo "dependencies.maven.homeDirectory=${{ steps.runner_config.outputs.config_dir }}.m2" >> kss.properties 103 | sudo chown '${{ steps.runner_config.outputs.user }}' '${{ steps.runner_config.outputs.config_dir }}' 104 | sudo mkdir '${{ steps.runner_config.outputs.config_dir }}.m2' 105 | sudo chown '${{ steps.runner_config.outputs.user }}' '${{ steps.runner_config.outputs.config_dir }}.m2' 106 | sudo mv kss.properties '${{ steps.runner_config.outputs.config_dir }}' 107 | sudo ${{ steps.runner_config.outputs.start_cmd }} 108 | sleep 5 109 | 110 | - name: test linux service exists 111 | if: ${{ startsWith(matrix.runner, 'ubuntu') }} 112 | run: systemctl status kss.service 113 | 114 | - name: test macos service exists 115 | if: ${{ startsWith(matrix.runner, 'macos') }} 116 | run: sudo launchctl list 'net.edwardday.kss' 117 | 118 | - name: upgrade to latest version 119 | run: | 120 | sudo --preserve-env ./install.sh 121 | sleep 5 122 | env: 123 | GH_TOKEN: ${{ github.token }} 124 | 125 | - name: test linux service exists after upgrade 126 | if: ${{ startsWith(matrix.runner, 'ubuntu') }} 127 | run: systemctl status kss.service 128 | 129 | - name: test macos service exists after upgrade 130 | if: ${{ startsWith(matrix.runner, 'macos') }} 131 | run: sudo launchctl list 'net.edwardday.kss' 132 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | build/ 3 | !gradle/wrapper/gradle-wrapper.jar 4 | !**/src/main/**/build/ 5 | !**/src/test/**/build/ 6 | 7 | ### IntelliJ IDEA ### 8 | .idea/ 9 | 10 | ### Eclipse ### 11 | .apt_generated 12 | .classpath 13 | .factorypath 14 | .project 15 | .settings 16 | .springBeans 17 | .sts4-cache 18 | bin/ 19 | !**/src/main/**/bin/ 20 | !**/src/test/**/bin/ 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | 29 | ### VS Code ### 30 | .vscode/ 31 | 32 | ### Mac OS ### 33 | .DS_Store 34 | 35 | ### Project ### 36 | /kss.properties 37 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # CHANGELOG 2 | 3 | ## [UNRELEASED] 4 | 5 | ## [0.6.0] - 2025-01-25 6 | 7 | ### Added 8 | 9 | - script exception handling 10 | - add option to import other scripts via `@Import` annotation 11 | - upgrade current service via install script 12 | 13 | ## [0.5.0] - 2025-01-05 14 | 15 | ### Added 16 | 17 | - test for `@Repository` annotation 18 | - change logging configuration via properties file 19 | - test for install script 20 | - option to install via release archive file 21 | - option to set local maven repository settings 22 | - version and help command line option 23 | - option to specify install version explicitly 24 | 25 | ### Changed 26 | 27 | - updated various dependencies 28 | - default user for macos set to _www 29 | 30 | ## [0.4.0] - 2024-08-25 31 | 32 | ### Added 33 | 34 | - MacOS option in install script 35 | 36 | ## [0.3.0] - 2024-08-18 37 | 38 | ### Added 39 | 40 | - Install script 41 | - sample configuration file 42 | - README 43 | 44 | ### Changed 45 | 46 | - add working directory to service file 47 | 48 | ## [0.2.0] - 2024-08-17 49 | 50 | ### Added 51 | 52 | - Service file in release 53 | 54 | ### Changed 55 | 56 | - Release tar archive gzip 57 | - Adapt release content structure 58 | 59 | ## [0.1.0] - 2024-07-06 60 | 61 | ### Added 62 | 63 | - Initial Version 64 | 65 | [0.5.0]: https://github.com/EdwarDDay/kotlin-server-scripts/releases/tag/0.5.0 66 | 67 | [0.4.0]: https://github.com/EdwarDDay/kotlin-server-scripts/releases/tag/0.4.0 68 | 69 | [0.3.0]: https://github.com/EdwarDDay/kotlin-server-scripts/releases/tag/0.3.0 70 | 71 | [0.2.0]: https://github.com/EdwarDDay/kotlin-server-scripts/releases/tag/0.2.0 72 | 73 | [0.1.0]: https://github.com/EdwarDDay/kotlin-server-scripts/releases/tag/0.1.0 74 | 75 | [UNRELEASED]: https://github.com/EdwarDDay/kotlin-server-scripts 76 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | Copyright 2024 Eduard Wolf 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Kotlin Server Scripts 2 | 3 | Kotlin Server Scripts let you run kotlin scripts in response to server requests, without setting up a complete project. 4 | 5 | ## Setup 6 | 7 | There is an easy to use install script to install the latest release on a linux system. It downloads the release 8 | archive, installs the executable and creates and enables a system service. 9 | 10 | ```shell 11 | curl 'https://raw.githubusercontent.com/EdwarDDay/kotlin-server-scripts/main/install.sh' --output install.sh 12 | chmod u+x install.sh 13 | ./install.sh # admin privileges might be needed 14 | ``` 15 | 16 | The `install.sh` script will put the executable in a directory, creates a system service file with a service execution 17 | user and puts a configuration sample file in a config directory based on the OS. If you want to configure any of these 18 | settings or see the default values, execute `./install.sh --help` for more information. 19 | 20 | To fetch the release asset, the script needs either authorized access to the GitHub API - either via a GH PAT 21 | (`--token` option) or via the authenticated `gh` commandline tool - or needs the `jq` tool to parse the GitHub REST 22 | response. 23 | 24 | ### Nginx setup 25 | 26 | To execute server scripts, add something like the following configuration to you nginx.conf: 27 | 28 | ```nginx 29 | # match all kts files 30 | location ~ ^(.*)\.kts(\?.*)?$ { 31 | # match the server script with .server.kts extension 32 | try_files \$1.server.kts =404; 33 | # use default fastcgir parameters 34 | include fastcgi_params; 35 | # pass request to kss process 36 | fastcgi_pass unix:/var/run/kss/kss.sock; 37 | } 38 | ``` 39 | 40 | ## Example 41 | 42 | Example script looks like: 43 | 44 | `HelloWorld.server.kts` 45 | ```kotlin 46 | setHeader("Content-Type", "text/html") 47 | status(200) 48 | writeOutput( 49 | """ 50 | 51 | 52 | 53 | Hello Kotlin 54 | 55 | 56 |

Hello Kotlin

57 | 59 | """ 60 | ) 61 | ``` 62 | 63 | You can find more examples in the [test folder](scripting-host/src/test/resources) 64 | 65 | ### Add dependencies 66 | 67 | If you want to add a maven dependency, you can add a `@file:DependsOn(""")` annotation to the file. If your 68 | dependency isn't in the maven central repository, you can add additional repositories via a 69 | `@file:Repository("")` annotation at file level. 70 | 71 | ### Add script dependencies 72 | 73 | If you want to execute another `server.kts` script file, you can import it via `@file:Import("")`. 74 | 75 | ### Change logging configuration 76 | 77 | You can reference a [logback configuration file](https://logback.qos.ch/manual/configuration.html) in the 78 | `kss.properties` configuration file via `logging.logback.configurationFile=`. This way you can change the 79 | log level from the default `info` or log to a file instead of the command line as it's done by default. 80 | 81 | ## Building 82 | 83 | To build the library just run `./gradlew scripting-host:build`. 84 | -------------------------------------------------------------------------------- /build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Eduard Wolf 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | plugins { 18 | alias(libs.plugins.kotlin.jvm) apply false 19 | } 20 | 21 | group = "net.edwardday.serverscript" 22 | 23 | allprojects { 24 | repositories { 25 | mavenCentral() 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2024 Eduard Wolf 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | kotlin.code.style=official 18 | org.gradle.caching=true 19 | org.gradle.parallel=true 20 | 21 | version=0.7.0-SNAPSHOT 22 | -------------------------------------------------------------------------------- /gradle/libs.versions.toml: -------------------------------------------------------------------------------- 1 | [versions] 2 | clikt = "5.0.2" 3 | kotlin = "2.1.0" 4 | kotlinx-coroutines = "1.10.1" 5 | logback = "1.5.16" 6 | oshai-logging = "7.0.3" 7 | okio = "3.10.2" 8 | 9 | [libraries] 10 | clikt = { module = "com.github.ajalt.clikt:clikt", version.ref = "clikt" } 11 | kotlin-scripting-common = { module = "org.jetbrains.kotlin:kotlin-scripting-common", version.ref = "kotlin" } 12 | kotlin-scripting-dependencies = { module = "org.jetbrains.kotlin:kotlin-scripting-dependencies", version.ref = "kotlin" } 13 | kotlin-scripting-dependencies-maven = { module = "org.jetbrains.kotlin:kotlin-scripting-dependencies-maven", version.ref = "kotlin" } 14 | kotlin-scripting-jvm = { module = "org.jetbrains.kotlin:kotlin-scripting-jvm", version.ref = "kotlin" } 15 | kotlin-scripting-jvm-host = { module = "org.jetbrains.kotlin:kotlin-scripting-jvm-host", version.ref = "kotlin" } 16 | kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" } 17 | kotlinx-coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" } 18 | kotlinx-coroutines-slf4j = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-slf4j", version.ref = "kotlinx-coroutines" } 19 | kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinx-coroutines" } 20 | logback-logger = { module = "ch.qos.logback:logback-classic", version.ref = "logback"} 21 | oshai-logging = { module = "io.github.oshai:kotlin-logging-jvm", version.ref = "oshai-logging" } 22 | okio = { module = "com.squareup.okio:okio", version.ref = "okio" } 23 | 24 | [plugins] 25 | kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } 26 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EdwarDDay/kotlin-server-scripts/cdfed81c9ee3780333eb9e6feae8d10940dc1b84/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.12.1-bin.zip 4 | networkTimeout=10000 5 | validateDistributionUrl=true 6 | zipStoreBase=GRADLE_USER_HOME 7 | zipStorePath=wrapper/dists 8 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | # SPDX-License-Identifier: Apache-2.0 19 | # 20 | 21 | ############################################################################## 22 | # 23 | # Gradle start up script for POSIX generated by Gradle. 24 | # 25 | # Important for running: 26 | # 27 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 28 | # noncompliant, but you have some other compliant shell such as ksh or 29 | # bash, then to run this script, type that shell name before the whole 30 | # command line, like: 31 | # 32 | # ksh Gradle 33 | # 34 | # Busybox and similar reduced shells will NOT work, because this script 35 | # requires all of these POSIX shell features: 36 | # * functions; 37 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 38 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 39 | # * compound commands having a testable exit status, especially «case»; 40 | # * various built-in commands including «command», «set», and «ulimit». 41 | # 42 | # Important for patching: 43 | # 44 | # (2) This script targets any POSIX shell, so it avoids extensions provided 45 | # by Bash, Ksh, etc; in particular arrays are avoided. 46 | # 47 | # The "traditional" practice of packing multiple parameters into a 48 | # space-separated string is a well documented source of bugs and security 49 | # problems, so this is (mostly) avoided, by progressively accumulating 50 | # options in "$@", and eventually passing that to Java. 51 | # 52 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 53 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 54 | # see the in-line comments for details. 55 | # 56 | # There are tweaks for specific operating systems such as AIX, CygWin, 57 | # Darwin, MinGW, and NonStop. 58 | # 59 | # (3) This script is generated from the Groovy template 60 | # https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 61 | # within the Gradle project. 62 | # 63 | # You can find Gradle at https://github.com/gradle/gradle/. 64 | # 65 | ############################################################################## 66 | 67 | # Attempt to set APP_HOME 68 | 69 | # Resolve links: $0 may be a link 70 | app_path=$0 71 | 72 | # Need this for daisy-chained symlinks. 73 | while 74 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 75 | [ -h "$app_path" ] 76 | do 77 | ls=$( ls -ld "$app_path" ) 78 | link=${ls#*' -> '} 79 | case $link in #( 80 | /*) app_path=$link ;; #( 81 | *) app_path=$APP_HOME$link ;; 82 | esac 83 | done 84 | 85 | # This is normally unused 86 | # shellcheck disable=SC2034 87 | APP_BASE_NAME=${0##*/} 88 | # Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) 89 | APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit 90 | 91 | # Use the maximum available, or set MAX_FD != -1 to use that value. 92 | MAX_FD=maximum 93 | 94 | warn () { 95 | echo "$*" 96 | } >&2 97 | 98 | die () { 99 | echo 100 | echo "$*" 101 | echo 102 | exit 1 103 | } >&2 104 | 105 | # OS specific support (must be 'true' or 'false'). 106 | cygwin=false 107 | msys=false 108 | darwin=false 109 | nonstop=false 110 | case "$( uname )" in #( 111 | CYGWIN* ) cygwin=true ;; #( 112 | Darwin* ) darwin=true ;; #( 113 | MSYS* | MINGW* ) msys=true ;; #( 114 | NONSTOP* ) nonstop=true ;; 115 | esac 116 | 117 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 118 | 119 | 120 | # Determine the Java command to use to start the JVM. 121 | if [ -n "$JAVA_HOME" ] ; then 122 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 123 | # IBM's JDK on AIX uses strange locations for the executables 124 | JAVACMD=$JAVA_HOME/jre/sh/java 125 | else 126 | JAVACMD=$JAVA_HOME/bin/java 127 | fi 128 | if [ ! -x "$JAVACMD" ] ; then 129 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 130 | 131 | Please set the JAVA_HOME variable in your environment to match the 132 | location of your Java installation." 133 | fi 134 | else 135 | JAVACMD=java 136 | if ! command -v java >/dev/null 2>&1 137 | then 138 | die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 139 | 140 | Please set the JAVA_HOME variable in your environment to match the 141 | location of your Java installation." 142 | fi 143 | fi 144 | 145 | # Increase the maximum file descriptors if we can. 146 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 147 | case $MAX_FD in #( 148 | max*) 149 | # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. 150 | # shellcheck disable=SC2039,SC3045 151 | MAX_FD=$( ulimit -H -n ) || 152 | warn "Could not query maximum file descriptor limit" 153 | esac 154 | case $MAX_FD in #( 155 | '' | soft) :;; #( 156 | *) 157 | # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. 158 | # shellcheck disable=SC2039,SC3045 159 | ulimit -n "$MAX_FD" || 160 | warn "Could not set maximum file descriptor limit to $MAX_FD" 161 | esac 162 | fi 163 | 164 | # Collect all arguments for the java command, stacking in reverse order: 165 | # * args from the command line 166 | # * the main class name 167 | # * -classpath 168 | # * -D...appname settings 169 | # * --module-path (only if needed) 170 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 171 | 172 | # For Cygwin or MSYS, switch paths to Windows format before running java 173 | if "$cygwin" || "$msys" ; then 174 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 175 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 176 | 177 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 178 | 179 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 180 | for arg do 181 | if 182 | case $arg in #( 183 | -*) false ;; # don't mess with options #( 184 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 185 | [ -e "$t" ] ;; #( 186 | *) false ;; 187 | esac 188 | then 189 | arg=$( cygpath --path --ignore --mixed "$arg" ) 190 | fi 191 | # Roll the args list around exactly as many times as the number of 192 | # args, so each arg winds up back in the position where it started, but 193 | # possibly modified. 194 | # 195 | # NB: a `for` loop captures its iteration list before it begins, so 196 | # changing the positional parameters here affects neither the number of 197 | # iterations, nor the values presented in `arg`. 198 | shift # remove old arg 199 | set -- "$@" "$arg" # push replacement arg 200 | done 201 | fi 202 | 203 | 204 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 205 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 206 | 207 | # Collect all arguments for the java command: 208 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, 209 | # and any embedded shellness will be escaped. 210 | # * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be 211 | # treated as '${Hostname}' itself on the command line. 212 | 213 | set -- \ 214 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 215 | -classpath "$CLASSPATH" \ 216 | org.gradle.wrapper.GradleWrapperMain \ 217 | "$@" 218 | 219 | # Stop when "xargs" is not available. 220 | if ! command -v xargs >/dev/null 2>&1 221 | then 222 | die "xargs is not available" 223 | fi 224 | 225 | # Use "xargs" to parse quoted args. 226 | # 227 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 228 | # 229 | # In Bash we could simply go: 230 | # 231 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 232 | # set -- "${ARGS[@]}" "$@" 233 | # 234 | # but POSIX shell has neither arrays nor command substitution, so instead we 235 | # post-process each arg (as a line of input to sed) to backslash-escape any 236 | # character that might be a shell metacharacter, then use eval to reverse 237 | # that process (while maintaining the separation between arguments), and wrap 238 | # the whole thing up as a single "set" statement. 239 | # 240 | # This will of course break if any of these variables contains a newline or 241 | # an unmatched quote. 242 | # 243 | 244 | eval "set -- $( 245 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 246 | xargs -n1 | 247 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 248 | tr '\n' ' ' 249 | )" '"$@"' 250 | 251 | exec "$JAVACMD" "$@" 252 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | @rem SPDX-License-Identifier: Apache-2.0 17 | @rem 18 | 19 | @if "%DEBUG%"=="" @echo off 20 | @rem ########################################################################## 21 | @rem 22 | @rem Gradle startup script for Windows 23 | @rem 24 | @rem ########################################################################## 25 | 26 | @rem Set local scope for the variables with windows NT shell 27 | if "%OS%"=="Windows_NT" setlocal 28 | 29 | set DIRNAME=%~dp0 30 | if "%DIRNAME%"=="" set DIRNAME=. 31 | @rem This is normally unused 32 | set APP_BASE_NAME=%~n0 33 | set APP_HOME=%DIRNAME% 34 | 35 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 36 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 37 | 38 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 39 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 40 | 41 | @rem Find java.exe 42 | if defined JAVA_HOME goto findJavaFromJavaHome 43 | 44 | set JAVA_EXE=java.exe 45 | %JAVA_EXE% -version >NUL 2>&1 46 | if %ERRORLEVEL% equ 0 goto execute 47 | 48 | echo. 1>&2 49 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 50 | echo. 1>&2 51 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 52 | echo location of your Java installation. 1>&2 53 | 54 | goto fail 55 | 56 | :findJavaFromJavaHome 57 | set JAVA_HOME=%JAVA_HOME:"=% 58 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 59 | 60 | if exist "%JAVA_EXE%" goto execute 61 | 62 | echo. 1>&2 63 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 64 | echo. 1>&2 65 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 66 | echo location of your Java installation. 1>&2 67 | 68 | goto fail 69 | 70 | :execute 71 | @rem Setup the command line 72 | 73 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 74 | 75 | 76 | @rem Execute Gradle 77 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 78 | 79 | :end 80 | @rem End local scope for the variables with windows NT shell 81 | if %ERRORLEVEL% equ 0 goto mainEnd 82 | 83 | :fail 84 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 85 | rem the _cmd.exe /c_ return code! 86 | set EXIT_CODE=%ERRORLEVEL% 87 | if %EXIT_CODE% equ 0 set EXIT_CODE=1 88 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% 89 | exit /b %EXIT_CODE% 90 | 91 | :mainEnd 92 | if "%OS%"=="Windows_NT" endlocal 93 | 94 | :omega 95 | -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # 4 | # Copyright 2024 Eduard Wolf 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | set -eu 20 | set -o pipefail 21 | 22 | if [[ -n $(command -v systemctl || echo '') ]]; then 23 | echo 'use systemctl default values' >&2 24 | service_binary='systemctl' 25 | execution_directory='/usr/bin/' 26 | service_directory='/etc/systemd/system/' 27 | configuration_directory='/usr/share/kss/' 28 | log_directory='' 29 | service_file='kss.service' 30 | service_user='www-data' 31 | elif [[ -n $(command -v launchctl || echo '') ]]; then 32 | echo 'use launchctl default values' >&2 33 | service_binary='launchctl' 34 | execution_directory='/usr/local/bin/' 35 | service_directory='/Library/LaunchDaemons/' 36 | configuration_directory='/Library/Application Support/kss/' 37 | log_directory='/Library/Logs/' 38 | service_file='kss.plist' 39 | service_user='_www' 40 | else 41 | echo 'use empty default values' >&2 42 | service_binary='' 43 | execution_directory='/usr/bin/' 44 | service_directory='' 45 | configuration_directory='' 46 | log_directory='' 47 | service_file='kss.service' 48 | service_user='' 49 | fi 50 | 51 | if [[ -n $(command -v gh || echo '') ]]; then 52 | release_fetch_mode="gh" 53 | else 54 | release_fetch_mode="unknown" 55 | fi 56 | 57 | PROGRAM_NAME=$(basename "$0") 58 | 59 | function usageText { 60 | echo 'Usage: '"$PROGRAM_NAME"' [OPTIONS]' 61 | echo 'install latest kss release' 62 | echo '' 63 | echo 'Options:' 64 | echo '-h/--help prints this usage' 65 | echo '-t/--token github token to use to download executables' 66 | echo "-d/--directory[$execution_directory]" 67 | echo ' directory for binary files' 68 | echo '-r/--release-fetch-mode Options:' 69 | echo ' gh - use authenticated Github commandline tool' 70 | echo ' curl-authenticated - use curl authenticated with the token from the --token option' 71 | echo ' curl - use curl without token - needs jq to parse rest response' 72 | echo ' archive= - use tar archive as release' 73 | echo '-v/--release-version Specify explicit release version (uses latest version by default)' 74 | echo "-s/--service-directory[$service_directory]" 75 | echo ' directory for service files' 76 | echo "-c/--configuration-directory[$configuration_directory]" 77 | echo ' directory for configuration files' 78 | echo "-l/--log-directory[$log_directory]" 79 | echo ' directory for log files (only used in MacOS)' 80 | echo "-u/--user[$service_user] user which runs the service process" 81 | } 82 | 83 | function usage { 84 | if [ -z "${1+''}" ] 85 | then 86 | usageText 87 | exit 0 88 | else 89 | echo "$1" >&2 90 | usageText >&2 91 | exit 1 92 | fi 93 | } 94 | 95 | authorization_token='' 96 | release_version='' 97 | release_fetch_mode_set='false' 98 | 99 | while [[ $# -gt 0 ]]; do 100 | key="$1" 101 | if [ -z "${2+''}" ]; then 102 | value='' 103 | else 104 | value="$2" 105 | fi 106 | case $key in 107 | -h|--help) 108 | usage 109 | ;; 110 | -t|--token) 111 | if [ "$value" ]; then 112 | authorization_token="$value" 113 | if [ "${release_fetch_mode_set}" == 'false' ] && [ "${release_fetch_mode}" != 'gh' ]; then 114 | release_fetch_mode='curl-authenticated' 115 | fi 116 | shift 117 | else 118 | usage 'the --token option needs an argument' 119 | fi 120 | ;; 121 | -d|--directory) 122 | if [ "$value" ]; then 123 | execution_directory="${value%/}/" 124 | shift 125 | else 126 | usage 'the --directory option needs an argument' 127 | fi 128 | ;; 129 | -v|--release-version) 130 | if [ "$value" ]; then 131 | release_version="$value" 132 | shift 133 | else 134 | usage 'the --release-version option needs an argument' 135 | fi 136 | ;; 137 | -r|--release-fetch-mode) 138 | release_fetch_mode_set='true' 139 | case $value in 140 | gh) 141 | if [[ -n $(command -v gh || echo '') ]]; then 142 | release_fetch_mode="gh" 143 | else 144 | usage '--release-fetch-mode set to gh but can'\''t find gh command' 145 | fi 146 | ;; 147 | curl-authenticated) 148 | release_fetch_mode="curl-authenticated" 149 | ;; 150 | curl) 151 | if [[ -n $(command -v jq || echo '') ]]; then 152 | release_fetch_mode="curl" 153 | else 154 | usage '--release-fetch-mode set to curl but can'\''t find jq command, which is needed to parse the REST API' 155 | fi 156 | ;; 157 | archive=*) 158 | release_fetch_mode="archive" 159 | archive_file="${value:8}" 160 | if [ ! -f "$archive_file" ]; then 161 | usage "couldn't find archive file '$archive_file'" 162 | fi 163 | ;; 164 | '') 165 | usage 'the --release-fetch-mode option needs an argument' 166 | ;; 167 | *) 168 | usage "unknown option ${value} for option --release-fetch-mode - please specify gh, curl-authenticated or curl" 169 | ;; 170 | esac 171 | shift 172 | ;; 173 | -s|--service-directory) 174 | if [ "$value" ]; then 175 | service_directory="${value%/}/" 176 | shift 177 | else 178 | usage 'the --service-directory option needs an argument' 179 | fi 180 | ;; 181 | -c|--configuration-directory) 182 | if [ "$value" ]; then 183 | configuration_directory="${value%/}/" 184 | shift 185 | else 186 | usage 'the --configuration-directory option needs an argument' 187 | fi 188 | ;; 189 | -l|--log-directory) 190 | if [ "$value" ]; then 191 | log_directory="${value%/}/" 192 | shift 193 | else 194 | usage 'the --log-directory option needs an argument' 195 | fi 196 | ;; 197 | -u|--user) 198 | if [ "$value" ]; then 199 | service_user="${value}" 200 | shift 201 | else 202 | usage 'the --user option needs an argument' 203 | fi 204 | ;; 205 | *) 206 | usage "unknown option ${key}" 207 | ;; 208 | esac 209 | shift 210 | done 211 | 212 | if [ "${release_fetch_mode}" == 'unknown' ] && [[ -n $(command -v jq || echo '') ]]; then 213 | release_fetch_mode='curl' 214 | fi 215 | 216 | if [ "${release_fetch_mode}" == 'curl-authenticated' ] && [ "${authorization_token}" == '' ]; then 217 | usage "'--release-fetch-mode' with option 'curl-authenticated' needs also '--token' to be specified" 218 | fi 219 | 220 | if [ "${release_fetch_mode}" == 'archive' ] && [ "${release_version}" != '' ]; then 221 | usage "'--release-fetch-mode' with option 'archive' doesn't support a specific release version (--release-version option)" 222 | fi 223 | 224 | if [ "${release_fetch_mode}" == 'unknown' ]; then 225 | usage "please specify '--release-fetch-mode' as no default option was found" 226 | fi 227 | 228 | if [ "${release_fetch_mode}" == 'gh' ]; then 229 | if ! gh auth status >/dev/null && [ -z "${GH_TOKEN:-}" ]; then 230 | usage "gh commandline tool is not setup. Please login via 'gh auth login', specify 'GH_TOKEN' or use '--release-fetch-mode curl' or '--release-fetch-mode curl-authenticated --token '" 231 | fi 232 | fi 233 | 234 | if [ ! -d "${execution_directory}" ]; then 235 | usage "'--directory' is set to '${execution_directory}' which is no existing directory. Please specify an existing directory." 236 | elif [ ! -w "${execution_directory}" ]; then 237 | usage "Current user has no writer permissions for '${execution_directory}'. Please execute with a different user." 238 | fi 239 | 240 | if [ ! -d "${service_directory}" ]; then 241 | echo "'--service-directory' is set to '${service_directory}' which is no existing directory. Service won't be installed" >&2 242 | service_directory='' 243 | elif [ ! -w "${service_directory}" ]; then 244 | echo "Current user has no writer permissions for '${service_directory}'. Service won't be installed" >&2 245 | service_directory='' 246 | elif [ -n "${service_binary}" ]; then 247 | if [ ! -d "${configuration_directory}" ]; then 248 | echo "'--configuration-directory' (${configuration_directory}) not found. Try to create it" >&2 249 | mkdir -p "${configuration_directory}" 250 | fi 251 | if [ -n "${log_directory}" ] && [ ! -d "${log_directory}" ]; then 252 | echo "'--log-directory' (${log_directory}) not found. Try to create it" >&2 253 | mkdir -p "${log_directory}" 254 | fi 255 | if [ ! -w "${configuration_directory}" ]; then 256 | echo "Current user has no writer permissions for '${configuration_directory}'. Service won't be installed" >&2 257 | service_directory='' 258 | elif ! id "$service_user" >/dev/null 2>&1; then 259 | echo "User '$service_user' does not exist. Service won't be installed" >&2 260 | service_directory='' 261 | fi 262 | fi 263 | 264 | authorization_args=() 265 | if [ "$authorization_token" ]; then 266 | authorization_args=(--header "Authorization: Bearer ${authorization_token}") 267 | fi 268 | 269 | github_args=(--location ${authorization_args[@]+"${authorization_args[@]}"} --header 'X-GitHub-Api-Version: 2022-11-28') 270 | 271 | if [ -z "$release_version" ]; then 272 | release_rest_path='latest' 273 | # shellcheck disable=SC2016 274 | query='query ($user: String!, $repo: String!) { repository(owner: $user, name: $repo) { latestRelease { releaseAssets(first: 10) { nodes { contentType url } } } } }' 275 | else 276 | release_rest_path="tags/$release_version" 277 | # shellcheck disable=SC2016 278 | query='query ($user: String!, $repo: String!, $release: String!) { repository(owner: $user, name: $repo) { release(tagName: $release) { releaseAssets(first: 10) { nodes { contentType url } } } } }' 279 | fi 280 | 281 | function extractUrlFromGraphqlQuery() { 282 | local response=$1 283 | if [[ -n $(command -v jq || echo '') ]]; then 284 | if [ -z "$release_version" ]; then 285 | jq --raw-output '.data.repository.latestRelease.releaseAssets.nodes | map(select(.contentType == "application/gzip"))[0].url' <<< "$response" 286 | else 287 | jq --raw-output '.data.repository.release.releaseAssets.nodes | map(select(.contentType == "application/gzip"))[0].url' <<< "$response" 288 | fi 289 | else 290 | local response_tmp="${response#*\"contentType\":\"application\/gzip\",\"url\":\"}" 291 | echo "${response_tmp%%\"*}" 292 | fi 293 | } 294 | 295 | function downloadBinary() { 296 | echo 'download binary' >&2 297 | curl --progress-bar --header 'Accept: application/octet-stream' --url "${url}" --output "${archive_name}" --fail 298 | } 299 | 300 | archive_name='kss.tar.gz' 301 | 302 | case "$release_fetch_mode" in 303 | archive) 304 | echo "use release archive from file $archive_file" >&2 305 | cp "$archive_file" "$archive_name" 306 | ;; 307 | gh) 308 | echo 'download latest release data via gh commandline tool' >&2 309 | response="$(gh api graphql -F 'user=EdwarDDay' -F 'repo=kotlin-server-scripts' -F "release=$release_version" -f "query=$query")" 310 | url="$(extractUrlFromGraphqlQuery "$response")" 311 | downloadBinary 312 | ;; 313 | curl-authenticated) 314 | echo 'download latest release data via curl and graphql api' >&2 315 | data="{ 316 | \"query\":\"$query\", 317 | \"variables\":{ 318 | \"user\":\"EdwarDDay\", 319 | \"repo\":\"kotlin-server-scripts\", 320 | \"release\":\"$release_version\" 321 | } 322 | }" 323 | response="$(curl --request POST "${github_args[@]}" --fail --silent --url 'https://api.github.com/graphql' --data "$data")" 324 | url="$(extractUrlFromGraphqlQuery "$response")" 325 | downloadBinary 326 | ;; 327 | # curl 328 | *) 329 | echo 'download latest release data via github rest endpoint and jq' >&2 330 | url="$(curl --request GET "${github_args[@]}" --fail --silent --url "https://api.github.com/repos/EdwarDDay/kotlin-server-scripts/releases/$release_rest_path" | jq --raw-output '.assets | map(select(.content_type == "application/gzip"))[0].url')" 331 | downloadBinary 332 | esac 333 | 334 | echo 'extract binary' >&2 335 | 336 | if [ -d "${execution_directory}kss_lib/" ]; then 337 | rm "${execution_directory}kss_lib/"*.jar 338 | fi 339 | archiveInternalName="$(tar --list --file "${archive_name}" --exclude="*/?*")" 340 | tar --extract --gunzip --file "${archive_name}" --strip-components 2 --directory "${execution_directory}" "${archiveInternalName}bin/" 341 | 342 | if [[ -n "${service_directory}" ]]; then 343 | echo 'configure service' >&2 344 | # escape sed escape char 345 | service_user="${service_user/|/\\\|}" 346 | tar --extract --gunzip --file "${archive_name}" --to-stdout "${archiveInternalName}service/$service_file" |\ 347 | sed "s|{{DIRECTORY}}|${execution_directory}|g" | \ 348 | sed "s|{{WORKING_DIRECTORY}}|${configuration_directory}|g" | \ 349 | sed "s|{{LOG_DIRECTORY}}|${log_directory}|g" | \ 350 | sed "s|{{USER}}|${service_user}|g" \ 351 | > "$service_file" 352 | case $service_binary in 353 | systemctl) 354 | if [ ! -f "$service_directory$service_file" ]; then 355 | mv "$service_file" "$service_directory$service_file" 356 | systemctl enable kss 357 | echo 'The system service is enabled. Run' 358 | echo '' 359 | echo 'systemctl start kss' 360 | echo '' 361 | echo 'to start the service' 362 | elif diff --brief "$service_file" "$service_directory$service_file" > /dev/null; then 363 | rm "$service_file" 364 | echo 'Restart system service' 365 | systemctl restart kss 366 | else 367 | mv "$service_file" "$service_directory$service_file" 368 | echo 'Restart system service' 369 | systemctl daemon-reload 370 | systemctl restart kss 371 | fi 372 | ;; 373 | launchctl) 374 | touch "${log_directory}kss.log" 375 | touch "${log_directory}kss-error.log" 376 | chown "$service_user" "${log_directory}kss.log" "${log_directory}kss-error.log" 377 | if [ ! -f "$service_directory$service_file" ]; then 378 | mv "$service_file" "$service_directory$service_file" 379 | chmod 600 "$service_directory$service_file" 380 | echo 'The launch daemon is enabled. Run' 381 | echo '' 382 | echo "launchctl load -w $service_directory$service_file" 383 | echo '' 384 | echo 'to start the daemon' 385 | else 386 | mv "$service_file" "$service_directory$service_file" 387 | chmod 600 "$service_directory$service_file" 388 | echo 'reloading launch daemon' 389 | launchctl unload "$service_directory$service_file" 390 | launchctl load -w "$service_directory$service_file" 391 | fi 392 | ;; 393 | *) 394 | mv "$service_file" "$service_directory$service_file" 395 | esac 396 | 397 | tar --extract --gunzip --file "${archive_name}" --strip-components 2 --directory "${configuration_directory}" "${archiveInternalName}config/" 398 | echo "A sample configuration is in '${configuration_directory}'. Remove the '.sample' extension and edit it as you see fit." 399 | echo "A sample logging file is in '${configuration_directory}'. Remove the '.sample' extension, reference it in the configuration and edit it as you see fit." 400 | fi 401 | 402 | echo 'delete archive' >&2 403 | rm "${archive_name}" 404 | -------------------------------------------------------------------------------- /scripting-definition/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Eduard Wolf 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | plugins { 18 | alias(libs.plugins.kotlin.jvm) 19 | } 20 | 21 | group = "net.edwardday.serverscript" 22 | 23 | dependencies { 24 | implementation(libs.kotlin.scripting.common) 25 | implementation(libs.kotlin.scripting.jvm) 26 | implementation(libs.kotlin.scripting.dependencies) 27 | implementation(libs.kotlin.scripting.dependencies.maven) 28 | // coroutines dependency is required for this particular definition 29 | implementation(libs.kotlinx.coroutines) 30 | } 31 | 32 | kotlin { 33 | jvmToolchain(17) 34 | compilerOptions { 35 | allWarningsAsErrors.set(true) 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /scripting-definition/src/main/kotlin/ScriptDefinition.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Eduard Wolf 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.edwardday.serverscript.scriptdefinition 18 | 19 | import kotlinx.coroutines.runBlocking 20 | import net.edwardday.serverscript.scriptdefinition.annotation.Import 21 | import net.edwardday.serverscript.scriptdefinition.script.ServerScript 22 | import java.io.File 23 | import kotlin.script.experimental.annotations.KotlinScript 24 | import kotlin.script.experimental.api.RefineScriptCompilationConfigurationHandler 25 | import kotlin.script.experimental.api.ResultWithDiagnostics 26 | import kotlin.script.experimental.api.ScriptCollectedData 27 | import kotlin.script.experimental.api.ScriptCompilationConfiguration 28 | import kotlin.script.experimental.api.ScriptConfigurationRefinementContext 29 | import kotlin.script.experimental.api.ScriptDiagnostic 30 | import kotlin.script.experimental.api.ScriptSourceAnnotation 31 | import kotlin.script.experimental.api.asDiagnostics 32 | import kotlin.script.experimental.api.asSuccess 33 | import kotlin.script.experimental.api.collectedAnnotations 34 | import kotlin.script.experimental.api.defaultImports 35 | import kotlin.script.experimental.api.implicitReceivers 36 | import kotlin.script.experimental.api.importScripts 37 | import kotlin.script.experimental.api.onSuccess 38 | import kotlin.script.experimental.api.refineConfiguration 39 | import kotlin.script.experimental.dependencies.CompoundDependenciesResolver 40 | import kotlin.script.experimental.dependencies.DependsOn 41 | import kotlin.script.experimental.dependencies.FileSystemDependenciesResolver 42 | import kotlin.script.experimental.dependencies.Repository 43 | import kotlin.script.experimental.dependencies.maven.MavenDependenciesResolver 44 | import kotlin.script.experimental.dependencies.resolveFromScriptSourceAnnotations 45 | import kotlin.script.experimental.host.FileBasedScriptSource 46 | import kotlin.script.experimental.host.toScriptSource 47 | import kotlin.script.experimental.jvm.dependenciesFromClassContext 48 | import kotlin.script.experimental.jvm.jvm 49 | import kotlin.script.experimental.jvm.updateClasspath 50 | 51 | 52 | const val SERVER_SCRIPT_FILE_EXTENSION = "server.kts" 53 | 54 | @KotlinScript( 55 | fileExtension = SERVER_SCRIPT_FILE_EXTENSION, 56 | compilationConfiguration = ServerScriptConfiguration::class, 57 | ) 58 | abstract class ServerScriptDefinition 59 | 60 | class ServerScriptConfiguration : ScriptCompilationConfiguration( 61 | body = { 62 | defaultImports(DependsOn::class, Repository::class, Import::class) 63 | defaultImports("net.edwardday.serverscript.scriptdefinition.script.*") 64 | jvm { 65 | dependenciesFromClassContext( 66 | ServerScriptDefinition::class, 67 | "kotlin-scripting-dependencies", 68 | "scripting-definition", 69 | ) 70 | } 71 | implicitReceivers(ServerScript::class) 72 | refineConfiguration { 73 | // the callback called when any of the listed file-level annotations are encountered in the compiled script 74 | // the processing is defined by the `handler`, that may return refined configuration depending on the annotations 75 | onAnnotations(DependsOn::class, Repository::class, handler = ServerScriptClasspathConfigurator()) 76 | onAnnotations(Import::class, handler = ServerScriptImportsConfigurator()) 77 | } 78 | 79 | } 80 | ) 81 | 82 | class ServerScriptImportsConfigurator : RefineScriptCompilationConfigurationHandler { 83 | override fun invoke(context: ScriptConfigurationRefinementContext): ResultWithDiagnostics { 84 | val annotations = context.collectedData?.get(ScriptCollectedData.collectedAnnotations) 85 | ?.takeIf(List>::isNotEmpty) 86 | val importAnnotations = annotations?.filter { it.annotation is Import }?.takeIf(List<*>::isNotEmpty) 87 | ?: return context.compilationConfiguration.asSuccess() 88 | val workingDir = (context.script as? FileBasedScriptSource)?.file?.absoluteFile?.parentFile 89 | val diagnostics = arrayListOf() 90 | val scripts = importAnnotations.mapNotNull { (annotation, location) -> 91 | val path = (annotation as Import).path.let { 92 | if (it.endsWith(".$SERVER_SCRIPT_FILE_EXTENSION")) it else "$it.$SERVER_SCRIPT_FILE_EXTENSION" 93 | } 94 | val absolutePathFile = File(path).takeIf(File::isAbsolute) 95 | when { 96 | absolutePathFile != null -> absolutePathFile.takeIf(File::isFile).also { 97 | if (it == null) { 98 | diagnostics += ScriptDiagnostic( 99 | code = ScriptDiagnostic.unspecifiedError, 100 | message = "cannot find import script file at $path", 101 | severity = ScriptDiagnostic.Severity.WARNING, 102 | locationWithId = location, 103 | ) 104 | } 105 | } 106 | 107 | workingDir != null -> File(workingDir, path).takeIf(File::isFile).also { 108 | if (it == null) { 109 | diagnostics += ScriptDiagnostic( 110 | code = ScriptDiagnostic.unspecifiedError, 111 | message = "cannot find import script file at ${workingDir.path}/$path", 112 | severity = ScriptDiagnostic.Severity.WARNING, 113 | locationWithId = location, 114 | ) 115 | } 116 | } 117 | 118 | else -> { 119 | diagnostics += ScriptDiagnostic( 120 | code = ScriptDiagnostic.unspecifiedError, 121 | message = "cannot find import script file with relative path ($path) from a script with unknown location", 122 | severity = ScriptDiagnostic.Severity.ERROR, 123 | locationWithId = location, 124 | ) 125 | null 126 | } 127 | }?.toScriptSource() 128 | } 129 | 130 | return if (diagnostics.isNotEmpty()) { 131 | ResultWithDiagnostics.Failure(diagnostics) 132 | } else { 133 | ScriptCompilationConfiguration(context.compilationConfiguration) { 134 | importScripts(scripts) 135 | }.asSuccess() 136 | } 137 | } 138 | 139 | } 140 | 141 | class ServerScriptClasspathConfigurator : RefineScriptCompilationConfigurationHandler { 142 | private val resolver = CompoundDependenciesResolver(FileSystemDependenciesResolver(), MavenDependenciesResolver()) 143 | 144 | override operator fun invoke(context: ScriptConfigurationRefinementContext): ResultWithDiagnostics = 145 | processAnnotations(context) 146 | 147 | private fun processAnnotations(context: ScriptConfigurationRefinementContext): ResultWithDiagnostics { 148 | val diagnostics = arrayListOf() 149 | 150 | val annotations = context.collectedData?.get(ScriptCollectedData.collectedAnnotations) 151 | ?.takeIf(List<*>::isNotEmpty) 152 | ?: return context.compilationConfiguration.asSuccess() 153 | 154 | val resolveResult = try { 155 | runBlocking { 156 | resolver.resolveFromScriptSourceAnnotations( 157 | annotations.filter { it.annotation is DependsOn || it.annotation is Repository } 158 | ) 159 | } 160 | } catch (e: Throwable) { 161 | ResultWithDiagnostics.Failure( 162 | *diagnostics.toTypedArray(), 163 | e.asDiagnostics(path = context.script.locationId) 164 | ) 165 | } 166 | 167 | return resolveResult.onSuccess { resolvedClassPath -> 168 | ScriptCompilationConfiguration(context.compilationConfiguration) { 169 | updateClasspath(resolvedClassPath) 170 | }.asSuccess() 171 | } 172 | } 173 | } 174 | -------------------------------------------------------------------------------- /scripting-definition/src/main/kotlin/annotation/Import.kt: -------------------------------------------------------------------------------- 1 | package net.edwardday.serverscript.scriptdefinition.annotation 2 | 3 | @Target(AnnotationTarget.FILE) 4 | @Repeatable 5 | @Retention(AnnotationRetention.SOURCE) 6 | annotation class Import(val path: String) 7 | -------------------------------------------------------------------------------- /scripting-definition/src/main/kotlin/script/ServerScript.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Eduard Wolf 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.edwardday.serverscript.scriptdefinition.script 18 | 19 | import java.nio.file.Path 20 | 21 | interface ServerScript { 22 | val parameters: Map> 23 | val cache: Cache 24 | 25 | fun path(name: String): Path 26 | 27 | fun setHeaders(key: String, value: List) 28 | fun getHeaders(key: String): List 29 | fun readInputLine(): String? 30 | fun writeOutput(output: String) 31 | fun writeError(output: String) 32 | } 33 | 34 | interface Cache { 35 | fun getOrSet(key: Any?, factory: () -> T): T 36 | fun updateOrSet(key: Any?, factory: () -> T, merge: (oldValue: T) -> T): T 37 | } 38 | 39 | fun ServerScript.setHeader(key: String, vararg values: String) { 40 | setHeaders(key, values.toList()) 41 | } 42 | 43 | fun ServerScript.addHeader(key: String, vararg value: String) { 44 | setHeaders(key, getHeaders(key) + value.asList()) 45 | } 46 | 47 | fun ServerScript.status(statusCode: Int) { 48 | val value = when (statusCode) { 49 | 100 -> "100 Continue" 50 | 101 -> "101 Switching Protocols" 51 | 102 -> "102 Processing" 52 | 103 -> "103 Early Hints" 53 | 54 | 200 -> "200 OK" 55 | 201 -> "201 Created" 56 | 202 -> "202 Accepted" 57 | 203 -> "203 Non-Authoritative Information" 58 | 204 -> "204 No Content" 59 | 205 -> "205 Reset Content" 60 | 206 -> "206 Partial Content" 61 | 207 -> "207 Multi-Status" 62 | 208 -> "208 Already Reported" 63 | 226 -> "226 IM Used" 64 | 65 | 300 -> "300 Multiple Choices" 66 | 301 -> "301 Moved Permanently" 67 | 302 -> "302 Found" 68 | 303 -> "303 See Other" 69 | 304 -> "304 Not Modified" 70 | 305 -> "305 Use Proxy Deprecated" 71 | 306 -> "306 unused" 72 | 307 -> "307 Temporary Redirect" 73 | 308 -> "308 Permanent Redirect" 74 | 75 | 400 -> "400 Bad Request" 76 | 401 -> "401 Unauthorized" 77 | 402 -> "402 Payment Required Experimental" 78 | 403 -> "403 Forbidden" 79 | 404 -> "404 Not Found" 80 | 405 -> "405 Method Not Allowed" 81 | 406 -> "406 Not Acceptable" 82 | 407 -> "407 Proxy Authentication Required" 83 | 408 -> "408 Request Timeout" 84 | 409 -> "409 Conflict" 85 | 410 -> "410 Gone" 86 | 411 -> "411 Length Required" 87 | 412 -> "412 Precondition Failed" 88 | 413 -> "413 Payload Too Large" 89 | 414 -> "414 URI Too Long" 90 | 415 -> "415 Unsupported Media Type" 91 | 416 -> "416 Range Not Satisfiable" 92 | 417 -> "417 Expectation Failed" 93 | 418 -> "418 I'm a teapot" 94 | 421 -> "421 Misdirected Request" 95 | 422 -> "422 Unprocessable Content" 96 | 423 -> "423 Locked" 97 | 424 -> "424 Failed Dependency" 98 | 425 -> "425 Too Early Experimental" 99 | 426 -> "426 Upgrade Required" 100 | 428 -> "428 Precondition Required" 101 | 429 -> "429 Too Many Requests" 102 | 431 -> "431 Request Header Fields Too Large" 103 | 451 -> "451 Unavailable For Legal Reasons" 104 | 105 | 500 -> "500 Internal Server Error" 106 | 501 -> "501 Not Implemented" 107 | 502 -> "502 Bad Gateway" 108 | 503 -> "503 Service Unavailable" 109 | 504 -> "504 Gateway Timeout" 110 | 505 -> "505 HTTP Version Not Supported" 111 | 506 -> "506 Variant Also Negotiates" 112 | 507 -> "507 Insufficient Storage" 113 | 508 -> "508 Loop Detected" 114 | 510 -> "510 Not Extended" 115 | 511 -> "511 Network Authentication Required" 116 | else -> statusCode.toString() 117 | } 118 | setHeader("Status", value) 119 | } -------------------------------------------------------------------------------- /scripting-host/build.gradle.kts: -------------------------------------------------------------------------------- 1 | import org.gradle.jvm.component.internal.JvmSoftwareComponentInternal 2 | 3 | /* 4 | * Copyright 2024 Eduard Wolf 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | plugins { 20 | alias(libs.plugins.kotlin.jvm) 21 | application 22 | } 23 | 24 | group = "net.edwardday.serverscript" 25 | version = findProperty("version") as String 26 | 27 | dependencies { 28 | implementation(libs.kotlin.scripting.common) 29 | implementation(libs.kotlin.scripting.jvm) 30 | implementation(libs.kotlin.scripting.jvm.host) 31 | implementation(project(":scripting-definition")) // the script definition module 32 | 33 | implementation(libs.clikt) 34 | implementation(libs.okio) 35 | implementation(libs.kotlinx.coroutines) 36 | implementation(libs.kotlinx.coroutines.slf4j) 37 | implementation(libs.oshai.logging) 38 | runtimeOnly(libs.logback.logger) 39 | 40 | testImplementation(libs.kotlin.test) 41 | testImplementation(libs.kotlinx.coroutines.test) 42 | } 43 | 44 | kotlin { 45 | jvmToolchain(17) 46 | compilerOptions { 47 | allWarningsAsErrors.set(true) 48 | } 49 | } 50 | 51 | val createReleaseStartScript by tasks.registering { 52 | val startScriptsTask = tasks.named("startScripts") 53 | dependsOn(startScriptsTask) 54 | val outputDirectory = layout.buildDirectory.dir("dist/release/scripts") 55 | outputs.dir(outputDirectory) 56 | doLast { 57 | val directory = outputDirectory.get().asFile 58 | startScriptsTask.get().outputs.files.flatMap { 59 | if (it.isFile) listOf(it) else it.listFiles()?.asList().orEmpty() 60 | }.map { script -> 61 | val classpathLineStart: String 62 | val replaceOriginal: String 63 | val replaceNewValue: String 64 | if (script.extension == "bat") { 65 | classpathLineStart = "set CLASSPATH=" 66 | replaceOriginal = "%APP_HOME%\\lib\\" 67 | replaceNewValue = "%APP_HOME%\\kss_lib\\" 68 | } else { 69 | classpathLineStart = "CLASSPATH=" 70 | replaceOriginal = "\$APP_HOME/lib/" 71 | replaceNewValue = "\$APP_HOME/kss_lib/" 72 | } 73 | File(directory, script.name).also { file -> 74 | script.bufferedReader().useLines { scriptLines -> 75 | file.bufferedWriter().use { writer -> 76 | scriptLines.forEach { line -> 77 | val updatedLine = if (!line.startsWith(classpathLineStart)) { 78 | line 79 | } else { 80 | line.replace(replaceOriginal, replaceNewValue) 81 | } 82 | writer.appendLine(updatedLine) 83 | } 84 | } 85 | } 86 | if (file.extension != "bat") { 87 | file.setExecutable(true, false) 88 | } 89 | } 90 | } 91 | } 92 | } 93 | 94 | distributions { 95 | create("release") { 96 | contents { 97 | into("bin") { 98 | from(createReleaseStartScript) 99 | into("kss_lib") { 100 | from(tasks.getByName("jar")) 101 | from((components["java"] as JvmSoftwareComponentInternal).mainFeature.runtimeClasspathConfiguration) 102 | } 103 | } 104 | from("src/release") 105 | } 106 | } 107 | } 108 | 109 | tasks.named("releaseDistTar") { 110 | compression = Compression.GZIP 111 | archiveExtension.set("tar.gz") 112 | } 113 | 114 | application { 115 | mainClass.set("net.edwardday.serverscript.scripthost.MainKt") 116 | applicationName = "kss" 117 | executableDir = "" 118 | } 119 | 120 | val processResourceVersionFile by tasks.registering { 121 | inputs.property("version", project.version) 122 | val outputFile = layout.buildDirectory.file("resources/main/version.properties") 123 | outputs.file(outputFile) 124 | doLast { 125 | val version = requireNotNull(inputs.properties["version"]) 126 | outputFile.get().asFile.printWriter().use { 127 | it.print("version=$version") 128 | } 129 | } 130 | } 131 | 132 | tasks.processResources { 133 | dependsOn(processResourceVersionFile) 134 | } 135 | -------------------------------------------------------------------------------- /scripting-host/src/main/kotlin/CacheImpl.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Eduard Wolf 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.edwardday.serverscript.scripthost 18 | 19 | import java.util.concurrent.atomic.AtomicReference 20 | import net.edwardday.serverscript.scriptdefinition.script.Cache 21 | 22 | class CacheImpl : Cache { 23 | private val map = AtomicReference>(emptyMap()) 24 | 25 | override fun getOrSet(key: Any?, factory: () -> T): T { 26 | val usedKey = key ?: NULL_VALUE 27 | val updatedMap = map.updateAndGet { 28 | val value = it[usedKey] 29 | if (value == null) { 30 | it + (usedKey to (factory() ?: NULL_VALUE)) 31 | } else { 32 | it 33 | } 34 | } 35 | @Suppress("UNCHECKED_CAST") 36 | return updatedMap.getValue(usedKey).takeUnless { it == NULL_VALUE } as T 37 | } 38 | 39 | override fun updateOrSet(key: Any?, factory: () -> T, merge: (oldValue: T) -> T): T { 40 | val usedKey = key ?: NULL_VALUE 41 | val updatedMap = map.updateAndGet { cachedMap -> 42 | val newValue = when (val value = cachedMap[usedKey]) { 43 | null -> factory() 44 | else -> @Suppress("UNCHECKED_CAST") merge(value.takeUnless { it == NULL_VALUE } as T) 45 | } 46 | cachedMap + (usedKey to (newValue ?: NULL_VALUE)) 47 | } 48 | @Suppress("UNCHECKED_CAST") 49 | return updatedMap.getValue(usedKey).takeUnless { it == NULL_VALUE } as T 50 | } 51 | } 52 | 53 | private val NULL_VALUE = Any() 54 | -------------------------------------------------------------------------------- /scripting-host/src/main/kotlin/FCGIRecord.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Eduard Wolf 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.edwardday.serverscript.scripthost 18 | 19 | import okio.Buffer 20 | import okio.BufferedSink 21 | import okio.BufferedSource 22 | import okio.IOException 23 | 24 | sealed interface FCGIRecord { 25 | 26 | sealed interface FCGIManageRecord : FCGIRecord 27 | sealed interface FCGIAppRecord : FCGIRecord 28 | 29 | sealed interface FCGIResponseRecord : FCGIRecord { 30 | val type: Int 31 | val contentLength: UShort 32 | fun write(sink: BufferedSink) 33 | } 34 | 35 | sealed interface FCGIRequestRecord : FCGIRecord 36 | 37 | 38 | class BeginRequest( 39 | val role: Role, 40 | val flags: UByte, 41 | ) : FCGIRequestRecord, FCGIAppRecord { 42 | val keepConnection: Boolean get() = (flags.toInt() and 1) != 0 43 | 44 | override fun toString(): String = "BeginRequest(role=$role, keepConnection=$keepConnection)" 45 | 46 | enum class Role { 47 | Responder, 48 | Authorizer, 49 | Filter, 50 | ; 51 | 52 | companion object { 53 | fun readRole(source: BufferedSource): Role { 54 | return when (val role = source.readShort().toInt()) { 55 | 1 -> Responder 56 | 2 -> Authorizer 57 | 3 -> Filter 58 | else -> throw IOException("unknown role $role") 59 | } 60 | } 61 | } 62 | } 63 | 64 | companion object { 65 | fun read(source: BufferedSource): BeginRequest { 66 | val result = BeginRequest( 67 | role = Role.readRole(source), 68 | flags = source.readByte().toUByte(), 69 | ) 70 | source.skip(5) // reserved 71 | return result 72 | } 73 | } 74 | } 75 | 76 | data object AbortRequest : FCGIRequestRecord, FCGIAppRecord { 77 | fun read(source: BufferedSource): AbortRequest { 78 | source.buffer.clear() 79 | return AbortRequest 80 | } 81 | } 82 | 83 | class EndRequest( 84 | internal val appStatus: Int, 85 | private val protocolStatus: ProtocolStatus, 86 | ) : FCGIResponseRecord, FCGIAppRecord { 87 | override val type: Int get() = 3 88 | override val contentLength: UShort get() = 8u 89 | 90 | override fun toString(): String = "EndRequest(appStatus=$appStatus, protocolStatus=$protocolStatus)" 91 | 92 | override fun write(sink: BufferedSink) { 93 | sink.writeInt(appStatus) 94 | sink.writeByte(protocolStatus.status.toInt()) 95 | repeat(3) { sink.writeByte(0) } 96 | } 97 | 98 | enum class ProtocolStatus(val status: UByte) { 99 | RequestComplete(0u), 100 | CantMpxConn(1u), 101 | Overload(2u), 102 | UnknownRole(3u), 103 | ; 104 | } 105 | 106 | companion object { 107 | @Suppress("UNUSED_PARAMETER") 108 | fun read(source: BufferedSource): Nothing { 109 | throw IOException("should not read end request record type") 110 | } 111 | } 112 | } 113 | 114 | class Params( 115 | val source: BufferedSource, 116 | ) : FCGIRequestRecord, FCGIAppRecord { 117 | 118 | override fun toString(): String = "Params(sourceBufferSize=${source.buffer.size})" 119 | 120 | companion object { 121 | fun read(source: BufferedSource): Params = Params(source) 122 | } 123 | } 124 | 125 | class Stdin( 126 | val source: BufferedSource, 127 | ) : FCGIRequestRecord, FCGIAppRecord { 128 | 129 | override fun toString(): String { 130 | return "Stdin(sourceBufferSize=${source.buffer.size})" 131 | } 132 | 133 | companion object { 134 | fun read(source: BufferedSource): Stdin = Stdin(source) 135 | } 136 | } 137 | 138 | class Stdout( 139 | private val sink: Buffer, 140 | ) : FCGIResponseRecord, FCGIAppRecord { 141 | 142 | override val type: Int get() = 6 143 | override val contentLength: UShort get() = sink.size.toUShort() 144 | 145 | override fun toString(): String = "Stdout(sinkSize=${sink.size})" 146 | 147 | override fun write(sink: BufferedSink) { 148 | sink.writeAll(this.sink) 149 | } 150 | 151 | companion object { 152 | @Suppress("UNUSED_PARAMETER") 153 | fun read(source: BufferedSource): Nothing { 154 | throw IOException("should not read end request record type") 155 | } 156 | } 157 | } 158 | 159 | class Stderr( 160 | private val sink: Buffer, 161 | ) : FCGIResponseRecord, FCGIAppRecord { 162 | 163 | override val type: Int get() = 7 164 | override val contentLength: UShort get() = sink.size.toUShort() 165 | 166 | override fun toString() = "Stderr(sinkSize=${sink.size})" 167 | 168 | override fun write(sink: BufferedSink) { 169 | sink.writeAll(this.sink) 170 | } 171 | 172 | companion object { 173 | @Suppress("UNUSED_PARAMETER") 174 | fun read(source: BufferedSource): Nothing { 175 | throw IOException("should not read end request record type") 176 | } 177 | } 178 | } 179 | 180 | class Data( 181 | val source: BufferedSource, 182 | ) : FCGIRequestRecord, FCGIAppRecord { 183 | 184 | override fun toString(): String { 185 | return "Data(sourceBufferSize=${source.buffer.size})" 186 | } 187 | 188 | companion object { 189 | fun read(source: BufferedSource): Data = Data(source) 190 | } 191 | } 192 | 193 | class GetValues( 194 | val source: BufferedSource, 195 | ) : FCGIRequestRecord, FCGIManageRecord { 196 | 197 | override fun toString(): String { 198 | return "GetValues(sourceBufferSize=${source.buffer.size})" 199 | } 200 | 201 | companion object { 202 | fun read(source: BufferedSource): GetValues = GetValues(source) 203 | } 204 | } 205 | 206 | class GetValuesResult( 207 | private val sink: Buffer, 208 | ) : FCGIResponseRecord, FCGIManageRecord { 209 | 210 | override val type: Int get() = 10 211 | override val contentLength: UShort get() = sink.size.toUShort() 212 | 213 | override fun toString(): String = "GetValuesResult(sinkBufferSize=${sink.size})" 214 | 215 | override fun write(sink: BufferedSink) { 216 | sink.writeAll(this.sink) 217 | } 218 | 219 | companion object { 220 | @Suppress("UNUSED_PARAMETER") 221 | fun read(source: BufferedSource): Nothing { 222 | throw IOException("should not read end request record type") 223 | } 224 | } 225 | } 226 | 227 | class UnknownType(private val unknownType: UByte) : FCGIResponseRecord, FCGIManageRecord { 228 | 229 | override val type: Int get() = 11 230 | override val contentLength: UShort get() = 8u 231 | 232 | override fun toString(): String = "UnknownType(unknownType=$unknownType)" 233 | 234 | override fun write(sink: BufferedSink) { 235 | sink.writeByte(unknownType.toInt()) 236 | repeat(7) { sink.writeByte(0) } 237 | } 238 | 239 | companion object { 240 | @Suppress("UNUSED_PARAMETER") 241 | fun read(source: BufferedSource): Nothing { 242 | throw IOException("should not read end request record type") 243 | } 244 | } 245 | } 246 | 247 | class ReadUnknownType(val type: UByte) : FCGIRequestRecord, FCGIManageRecord { 248 | 249 | override fun toString(): String { 250 | return "ReadUnknownType(type=$type)" 251 | } 252 | 253 | companion object { 254 | fun read(source: BufferedSource, type: UByte): ReadUnknownType { 255 | source.buffer.clear() 256 | return ReadUnknownType(type) 257 | } 258 | } 259 | } 260 | 261 | companion object { 262 | fun read(source: BufferedSource, type: UByte): FCGIRequestRecord { 263 | return when (type.toUInt()) { 264 | 1u -> BeginRequest.read(source) 265 | 2u -> AbortRequest.read(source) 266 | 3u -> EndRequest.read(source) 267 | 4u -> Params.read(source) 268 | 5u -> Stdin.read(source) 269 | 6u -> Stdout.read(source) 270 | 7u -> Stderr.read(source) 271 | 8u -> Data.read(source) 272 | 9u -> GetValues.read(source) 273 | 10u -> GetValuesResult.read(source) 274 | 11u -> UnknownType.read(source) 275 | else -> ReadUnknownType.read(source, type) 276 | } 277 | } 278 | } 279 | } -------------------------------------------------------------------------------- /scripting-host/src/main/kotlin/FCGIRequestMessage.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Eduard Wolf 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.edwardday.serverscript.scripthost 18 | 19 | import io.github.oshai.kotlinlogging.KotlinLogging 20 | import okio.BufferedSource 21 | import okio.IOException 22 | 23 | private val logger = KotlinLogging.logger {} 24 | 25 | class FCGIRequestMessage( 26 | version: UByte, 27 | val requestId: Int, 28 | val record: FCGIRecord.FCGIRequestRecord, 29 | ) { 30 | init { 31 | if (version.toInt() != 1) { 32 | throw IOException("unknown version $version") 33 | } 34 | } 35 | 36 | override fun toString(): String = "Message(requestId=$requestId, record=$record)" 37 | 38 | companion object { 39 | @Throws(IOException::class) 40 | fun read(source: BufferedSource): FCGIRequestMessage { 41 | source.require(8) 42 | val version = source.readByte().toUByte() 43 | val type = source.readByte().toUByte() 44 | val requestId = source.readShort().toUShort().toInt() 45 | val contentLength = source.readShort().toUShort().toLong() 46 | val paddingLength = source.readByte().toUByte().toLong() 47 | source.skip(1) // reserved 48 | source.require(contentLength + paddingLength) 49 | val record = FCGIRecord.read(source.readBuffer(contentLength), type) 50 | source.skip(paddingLength) 51 | return FCGIRequestMessage(version, requestId, record).also { 52 | logger.trace { "receive message $it" } 53 | } 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /scripting-host/src/main/kotlin/FCGIResponseMessage.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Eduard Wolf 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.edwardday.serverscript.scripthost 18 | 19 | import io.github.oshai.kotlinlogging.KotlinLogging 20 | import okio.BufferedSink 21 | import okio.IOException 22 | 23 | private val logger = KotlinLogging.logger {} 24 | 25 | class FCGIResponseMessage( 26 | private val requestId: Int, 27 | internal val record: FCGIRecord.FCGIResponseRecord, 28 | ) { 29 | 30 | override fun toString(): String = "Message(requestId=$requestId, record=$record)" 31 | 32 | @Throws(IOException::class) 33 | fun write(sink: BufferedSink) { 34 | logger.trace { "send message $this" } 35 | sink.writeByte(1) // version 36 | sink.writeByte(record.type) 37 | sink.writeShort(requestId) 38 | val contentLength = record.contentLength.toInt() 39 | sink.writeShort(contentLength) 40 | val paddingLength = (8 - (contentLength % 8)) % 8 41 | sink.writeByte(paddingLength) 42 | sink.writeByte(0) // reserved 43 | record.write(sink) 44 | repeat(paddingLength) { sink.writeByte(0) } 45 | } 46 | } -------------------------------------------------------------------------------- /scripting-host/src/main/kotlin/KeyValuePair.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Eduard Wolf 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.edwardday.serverscript.scripthost 18 | 19 | import okio.Buffer 20 | import okio.BufferedSink 21 | import okio.BufferedSource 22 | 23 | data class KeyValuePair(val key: String, val value: String) { 24 | 25 | override fun toString(): String = "{$key=$value}" 26 | 27 | class Builder { 28 | private val source = Buffer() 29 | 30 | fun addSource(source: BufferedSource) { 31 | this.source.writeAll(source) 32 | } 33 | 34 | private fun readLength(): ByteArray { 35 | val lengthFirstByte = source.readByte().toInt() 36 | val length = if ((lengthFirstByte and 0x80) == 0) { 37 | lengthFirstByte 38 | } else { 39 | (lengthFirstByte and 0x7f) shl 24 or 40 | (source.readByte().toInt() and 0xff) shl 16 or 41 | (source.readByte().toInt() and 0xff) shl 8 or 42 | source.readByte().toInt() and 0xff 43 | } 44 | return ByteArray(length) 45 | } 46 | 47 | private fun readContent(part: ByteArray) { 48 | source.readFully(part) 49 | } 50 | 51 | fun build(): List { 52 | if (source.exhausted()) return emptyList() 53 | 54 | return buildList { 55 | while (!source.exhausted()) { 56 | val key = readLength() 57 | val value = readLength() 58 | readContent(key) 59 | readContent(value) 60 | add(KeyValuePair(String(key), String(value))) 61 | } 62 | } 63 | } 64 | } 65 | 66 | companion object { 67 | private fun BufferedSink.writeLength(length: Int) { 68 | if (length <= 0x7f) writeByte(length) else writeInt(length or Int.MIN_VALUE) 69 | } 70 | 71 | fun List.toBuffer(): Buffer { 72 | val buffer = Buffer() 73 | forEach { keyValuePair -> 74 | val keyByteArray = keyValuePair.key.toByteArray() 75 | buffer.writeLength(keyByteArray.size) 76 | val valueByteArray = keyValuePair.value.toByteArray() 77 | buffer.writeLength(valueByteArray.size) 78 | buffer.write(keyByteArray) 79 | buffer.write(valueByteArray) 80 | } 81 | return buffer 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /scripting-host/src/main/kotlin/Main.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Eduard Wolf 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.edwardday.serverscript.scripthost 18 | 19 | import com.github.ajalt.clikt.command.SuspendingCliktCommand 20 | import com.github.ajalt.clikt.command.main 21 | import com.github.ajalt.clikt.parameters.options.flag 22 | import com.github.ajalt.clikt.parameters.options.option 23 | import io.github.oshai.kotlinlogging.KotlinLogging 24 | import java.io.File 25 | import java.io.IOException 26 | import java.io.InputStream 27 | import java.util.* 28 | 29 | private const val CONFIG_FILE_NAME = "kss.properties" 30 | 31 | suspend fun main(args: Array) = Main().main(args) 32 | 33 | private class Main : SuspendingCliktCommand() { 34 | val version by option("-v", "--version", help = "Print current version").flag() 35 | 36 | private fun readPropertiesFromClasspath(fileName: String): Properties? { 37 | return Thread.currentThread() 38 | .getContextClassLoader() 39 | .getResourceAsStream(fileName) 40 | ?.use { stream -> 41 | Properties().apply { 42 | load(stream) 43 | } 44 | } 45 | } 46 | 47 | private fun loadProperties(): Properties { 48 | // load default config 49 | val defaultConfig = try { 50 | readPropertiesFromClasspath(CONFIG_FILE_NAME) ?: error("Could not load default configuration") 51 | } catch (e: IOException) { 52 | error("Could not read default configuration because of ${e.localizedMessage}") 53 | } 54 | 55 | // load config file from working directory 56 | val workingDirectoryConfig = Properties(defaultConfig) 57 | try { 58 | File(CONFIG_FILE_NAME).takeIf(File::exists)?.bufferedReader()?.use(workingDirectoryConfig::load) 59 | } catch (e: IOException) { 60 | // Ignore for now, maybe log? 61 | } 62 | 63 | // load config from system properties 64 | val commandLineConfig = Properties(workingDirectoryConfig) 65 | workingDirectoryConfig.keys.forEach { key -> 66 | val keyAsString = key.toString() 67 | System.getProperty(keyAsString)?.also { value -> commandLineConfig[keyAsString] = value } 68 | } 69 | return commandLineConfig 70 | } 71 | 72 | override suspend fun run() { 73 | val appVersion = kotlin.runCatching { 74 | readPropertiesFromClasspath("version.properties")?.getProperty("version") 75 | }.getOrElse { "" } 76 | if (version) { 77 | echo("Version: $appVersion") 78 | echo("Java version: ${Runtime.version()}") 79 | return 80 | } 81 | val properties = loadProperties() 82 | 83 | val loggingFile = properties.getProperty("logging.logback.configurationFile").orEmpty() 84 | if (loggingFile.isNotEmpty() && File(loggingFile).exists()) { 85 | System.setProperty("logback.configurationFile", loggingFile) 86 | } 87 | 88 | val logger = KotlinLogging.logger {} 89 | 90 | logger.info { "Version: $appVersion" } 91 | logger.info { ("Java version: ${Runtime.version()}") } 92 | 93 | val mavenDependenciesSettings = properties.getProperty("dependencies.maven.settingsFile").orEmpty() 94 | val mavenDependenciesHome = properties.getProperty("dependencies.maven.homeDirectory").orEmpty() 95 | if (mavenDependenciesSettings.isNotEmpty() && File(mavenDependenciesSettings).exists()) { 96 | if (mavenDependenciesHome.isNotEmpty()) { 97 | logger.warn { "'dependencies.maven.settingsFile' overwrites 'dependencies.maven.homeDirectory' setting." } 98 | } 99 | System.setProperty("org.apache.maven.user-settings", mavenDependenciesSettings) 100 | } else { 101 | val mavenDependenciesHomeDirectory = File(mavenDependenciesHome) 102 | if (mavenDependenciesHome.isNotEmpty() && mavenDependenciesHomeDirectory.isDirectory()) { 103 | if (mavenDependenciesHomeDirectory.canWrite()) { 104 | val settingsFile = File(mavenDependenciesHomeDirectory, "settings.xml") 105 | val repositoryDirectory = File(mavenDependenciesHomeDirectory, "repository") 106 | repositoryDirectory.mkdir() 107 | settingsFile.printWriter().use { writer -> 108 | writer.println("""""") 111 | writer.println(""" ${repositoryDirectory.absolutePath}""") 112 | writer.println("""""") 113 | } 114 | System.setProperty("org.apache.maven.user-settings", settingsFile.absolutePath) 115 | } else { 116 | logger.warn { "User can't write in ${mavenDependenciesHomeDirectory.absolutePath}" } 117 | } 118 | } 119 | } 120 | val socket = properties.getProperty("socket.address") ?: "unix:/var/run/kss/kss.sock" 121 | val maxConnections = properties.getProperty("connections.max")?.toIntOrNull() ?: 4 122 | 123 | readFromSocket( 124 | socket = socket, 125 | maxConnections = maxConnections, 126 | ) 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /scripting-host/src/main/kotlin/ManagementRequestState.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Eduard Wolf 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.edwardday.serverscript.scripthost 18 | 19 | import io.github.oshai.kotlinlogging.KotlinLogging 20 | import kotlinx.coroutines.flow.Flow 21 | import kotlinx.coroutines.flow.flowOf 22 | import net.edwardday.serverscript.scripthost.KeyValuePair.Companion.toBuffer 23 | 24 | private val logger = KotlinLogging.logger {} 25 | 26 | object ManagementRequestState { 27 | fun handleGetValues(record: FCGIRecord.GetValues, maxConnections: Int): Flow { 28 | val builder = KeyValuePair.Builder() 29 | builder.addSource(record.source) 30 | val answers = builder.build().mapNotNull { pair -> 31 | when (pair.key) { 32 | "FCGI_MAX_CONNS" -> pair.copy(value = maxConnections.toString()) 33 | "FCGI_MAX_REQS" -> pair.copy(value = "4") 34 | "FCGI_MPXS_CONNS" -> pair.copy(value = "1") 35 | else -> null 36 | } 37 | }.also { logger.info { "Answer GetValues with $it" } } 38 | return flowOf(FCGIResponseMessage(0, FCGIRecord.GetValuesResult(answers.toBuffer()))) 39 | } 40 | 41 | fun handleUnknownType(type: UByte): Flow = 42 | flowOf(FCGIResponseMessage(0, FCGIRecord.UnknownType(type))) 43 | } 44 | -------------------------------------------------------------------------------- /scripting-host/src/main/kotlin/RequestState.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Eduard Wolf 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.edwardday.serverscript.scripthost 18 | 19 | import io.github.oshai.kotlinlogging.KotlinLogging 20 | import kotlinx.coroutines.Dispatchers 21 | import kotlinx.coroutines.channels.trySendBlocking 22 | import kotlinx.coroutines.flow.Flow 23 | import kotlinx.coroutines.flow.callbackFlow 24 | import kotlinx.coroutines.flow.emptyFlow 25 | import kotlinx.coroutines.flow.flowOf 26 | import kotlinx.coroutines.flow.flowOn 27 | import kotlinx.coroutines.flow.onEmpty 28 | import net.edwardday.serverscript.scriptdefinition.ServerScriptDefinition 29 | import net.edwardday.serverscript.scriptdefinition.script.Cache 30 | import net.edwardday.serverscript.scriptdefinition.script.ServerScript 31 | import okio.Buffer 32 | import okio.FileSystem 33 | import okio.Path 34 | import okio.Path.Companion.toPath 35 | import java.util.concurrent.atomic.AtomicBoolean 36 | import kotlin.script.experimental.api.ResultValue 37 | import kotlin.script.experimental.api.ResultWithDiagnostics 38 | import kotlin.script.experimental.api.ScriptDiagnostic 39 | import kotlin.script.experimental.api.ScriptEvaluationConfiguration 40 | import kotlin.script.experimental.api.SourceCode 41 | import kotlin.script.experimental.api.asSuccess 42 | import kotlin.script.experimental.api.implicitReceivers 43 | import kotlin.script.experimental.api.refineConfigurationBeforeEvaluate 44 | import kotlin.script.experimental.host.toScriptSource 45 | import kotlin.script.experimental.jvmhost.BasicJvmScriptingHost 46 | import java.nio.file.Path as NioPath 47 | 48 | private val logger = KotlinLogging.logger {} 49 | 50 | class RequestState( 51 | record: FCGIRecord.BeginRequest, 52 | private val cache: Cache, 53 | ) { 54 | private val responseHeaders: MutableMap> = mutableMapOf() 55 | val keepConnection: Boolean = record.keepConnection 56 | 57 | private val stdIn: Buffer = Buffer() 58 | private var stdInDone: Boolean = false 59 | private val data: Buffer = Buffer() 60 | private var dataStarted: Boolean = false 61 | private var dataDone: Boolean = false 62 | 63 | private var paramsBuilder: KeyValuePair.Builder? = KeyValuePair.Builder() 64 | private var params: Map>? = null 65 | 66 | fun handleRecord(record: FCGIRecord.FCGIRequestRecord): Flow { 67 | logger.trace { "handle record: $record" } 68 | return when (record) { 69 | is FCGIRecord.Params -> { 70 | paramsBuilder?.let { paramsBuilder -> 71 | val recordSource = record.source 72 | if (recordSource.exhausted()) { 73 | params = buildMap> { 74 | paramsBuilder.build().forEach { (key, value) -> 75 | merge(key, listOf(value)) { old, new -> old + new } 76 | } 77 | }.also { 78 | logger.trace { "read params from request: $it" } 79 | } 80 | this.paramsBuilder = null 81 | logger.trace { "finished params from request" } 82 | runScriptIfPossible() 83 | } else { 84 | paramsBuilder.addSource(recordSource) 85 | emptyFlow() 86 | } 87 | } ?: kotlin.run { 88 | logger.warn { "params from request already finished but got new param record" } 89 | emptyFlow() 90 | } 91 | } 92 | 93 | is FCGIRecord.Stdin -> { 94 | if (!stdInDone) { 95 | val recordSource = record.source 96 | if (recordSource.exhausted()) { 97 | stdInDone = true 98 | logger.trace { "finished stdin from request" } 99 | runScriptIfPossible() 100 | } else { 101 | stdIn.writeAll(recordSource) 102 | emptyFlow() 103 | } 104 | } else { 105 | emptyFlow() 106 | } 107 | } 108 | 109 | // handled add constructor level 110 | is FCGIRecord.BeginRequest -> emptyFlow() 111 | is FCGIRecord.Data -> { 112 | dataStarted = true 113 | if (!dataDone) { 114 | val recordSource = record.source 115 | if (recordSource.exhausted()) { 116 | dataDone = true 117 | logger.trace { "finished data from request" } 118 | runScriptIfPossible() 119 | } else { 120 | data.writeAll(recordSource) 121 | emptyFlow() 122 | } 123 | } else { 124 | emptyFlow() 125 | } 126 | } 127 | 128 | FCGIRecord.AbortRequest -> { 129 | logger.debug { "abort request" } 130 | runScriptIfPossible().onEmpty { 131 | logger.debug { "send end request because no script ran" } 132 | emit( 133 | FCGIRecord.EndRequest( 134 | appStatus = 400, // TODO set proper app status 135 | protocolStatus = FCGIRecord.EndRequest.ProtocolStatus.RequestComplete, 136 | ) 137 | ) 138 | } 139 | } 140 | 141 | is FCGIRecord.FCGIManageRecord -> emptyFlow() 142 | } 143 | } 144 | 145 | inner class ServerScriptImpl( 146 | override val cache: Cache, 147 | private val workingDirectory: Path, 148 | private val writeOutput: (String) -> Unit, 149 | private val writeError: (String) -> Unit, 150 | ) : ServerScript { 151 | override val parameters: Map> get() = this@RequestState.params ?: emptyMap() 152 | private var canSetHeaders = true 153 | 154 | override fun path(name: String): NioPath { 155 | val directPath = name.toPath(normalize = true) 156 | return if (directPath.isAbsolute) { 157 | directPath.toNioPath() 158 | } else { 159 | workingDirectory.resolve(name).toNioPath() 160 | } 161 | } 162 | 163 | override fun getHeaders(key: String): List = responseHeaders[key] ?: emptyList() 164 | override fun setHeaders(key: String, value: List) { 165 | responseHeaders[key] = value 166 | if (!canSetHeaders) { 167 | logger.error { "Tried to set header ($key=$value) but headers were already send" } 168 | } 169 | } 170 | 171 | override fun readInputLine(): String? = stdIn.readUtf8Line() 172 | 173 | override fun writeOutput(output: String) { 174 | writeOutput.invoke(output) 175 | canSetHeaders = false 176 | } 177 | 178 | override fun writeError(output: String) { 179 | writeError.invoke(output) 180 | } 181 | } 182 | 183 | private fun runScriptIfPossible(): Flow { 184 | return if (stdInDone && params != null && (!dataStarted || dataDone)) { 185 | val scriptFile: Path? = params!!.findScriptPath() 186 | if (scriptFile != null) { 187 | val scriptSource = scriptFile.toFile().toScriptSource() 188 | logger.debug { "execute script $scriptFile" } 189 | executeScript(scriptSource, FileSystem.SYSTEM.canonicalize(scriptFile).parent!!) 190 | } else { 191 | flowOf(FCGIRecord.EndRequest(3, FCGIRecord.EndRequest.ProtocolStatus.RequestComplete)) 192 | } 193 | } else { 194 | logger.trace { 195 | val reason = when { 196 | stdInDone -> "stdin not read" 197 | params == null -> "params not read" 198 | else -> "started and didn't finish data reading" 199 | } 200 | "didn't run script yet, because $reason" 201 | } 202 | emptyFlow() 203 | } 204 | } 205 | 206 | private fun executeScript( 207 | scriptSource: SourceCode, 208 | workingDirectory: Path, 209 | ): Flow { 210 | return callbackFlow { 211 | val haveToWriteHeaders = AtomicBoolean(true) 212 | fun writeOutput(output: String) { 213 | val stdOutBuffer = Buffer() 214 | if (haveToWriteHeaders.compareAndSet(/* expectedValue = */ true, /* newValue = */ false)) { 215 | responseHeaders.forEach { (key, values) -> 216 | values.forEach { value -> 217 | stdOutBuffer.writeUtf8(key) 218 | stdOutBuffer.writeUtf8CodePoint(':'.code) 219 | stdOutBuffer.writeUtf8(value) 220 | stdOutBuffer.writeUtf8CodePoint('\n'.code) 221 | } 222 | } 223 | stdOutBuffer.writeUtf8CodePoint('\n'.code) 224 | } 225 | stdOutBuffer.writeUtf8(output) 226 | while (!stdOutBuffer.exhausted()) { 227 | val out = stdOutBuffer.readBuffer(stdOutBuffer.size.coerceAtMost(DEFAULT_MAX_RECORD_SIZE)) 228 | trySendBlocking(FCGIRecord.Stdout(out)) 229 | } 230 | } 231 | 232 | var wroteError = false 233 | fun writeError(output: String) { 234 | val stdErrorBuffer = Buffer() 235 | stdErrorBuffer.writeUtf8(output) 236 | if (!stdErrorBuffer.exhausted()) { 237 | wroteError = true 238 | do { 239 | val out = stdErrorBuffer.readBuffer(stdErrorBuffer.size.coerceAtMost(DEFAULT_MAX_RECORD_SIZE)) 240 | trySendBlocking(FCGIRecord.Stderr(out)) 241 | } while (!stdErrorBuffer.exhausted()) 242 | } 243 | } 244 | 245 | val result = BasicJvmScriptingHost().evalWithTemplate( 246 | script = scriptSource, 247 | evaluation = { 248 | refineConfigurationBeforeEvaluate { context -> 249 | val sourceLocationId = context.compiledScript.sourceLocationId 250 | ScriptEvaluationConfiguration(context.evaluationConfiguration) { 251 | implicitReceivers( 252 | ServerScriptImpl( 253 | cache = cache.getOrSet(sourceLocationId, ::CacheImpl), 254 | workingDirectory = workingDirectory, 255 | writeOutput = ::writeOutput, 256 | writeError = ::writeError, 257 | ) 258 | ) 259 | }.asSuccess() 260 | } 261 | }, 262 | ) 263 | val appStatus: Int = when (result) { 264 | is ResultWithDiagnostics.Success -> when (val returnValue = result.value.returnValue) { 265 | is ResultValue.Error -> { 266 | logger.error(returnValue.error) { 267 | "script execution threw error (wrapping exception ${returnValue.wrappingException})" 268 | } 269 | 1 270 | } 271 | 272 | ResultValue.NotEvaluated -> { 273 | logger.error { "script execution wasn't evaluated" } 274 | error("scripts should always get evaluated") 275 | } 276 | 277 | is ResultValue.Unit, 278 | is ResultValue.Value, 279 | -> { 280 | logger.debug { "script execution successful" } 281 | 0 282 | } 283 | } 284 | 285 | is ResultWithDiagnostics.Failure -> { 286 | val exception = result.reports.firstNotNullOfOrNull(ScriptDiagnostic::exception) 287 | logger.error(exception) { "script compilation failed with result: $result" } 288 | 2 289 | } 290 | } 291 | // if nothing emitted yet 292 | if (haveToWriteHeaders.get()) { 293 | if (appStatus != 0) { 294 | send(FCGIRecord.Stdout(Buffer().writeUtf8("Status:500 Internal Server Error\n\n"))) 295 | } else { 296 | send(FCGIRecord.Stdout(Buffer().writeUtf8("\n"))) 297 | } 298 | } 299 | // empty as end marker 300 | send(FCGIRecord.Stdout(Buffer())) 301 | if (wroteError) { 302 | // empty as end marker 303 | send(FCGIRecord.Stderr(Buffer())) 304 | } 305 | send( 306 | FCGIRecord.EndRequest( 307 | appStatus = appStatus, 308 | protocolStatus = FCGIRecord.EndRequest.ProtocolStatus.RequestComplete, 309 | ) 310 | ) 311 | close() 312 | }.flowOn(Dispatchers.IO) 313 | } 314 | } 315 | 316 | private fun Map>.findScriptPath(): Path? { 317 | val scriptFileName = get("SCRIPT_FILENAME")?.singleOrNull() 318 | val scriptFileNamePath = scriptFileName?.toPath()?.takeIf(FileSystem.SYSTEM::exists) 319 | val documentRoot = get("DOCUMENT_ROOT")?.singleOrNull() 320 | val scriptName = get("SCRIPT_NAME")?.singleOrNull() 321 | 322 | return when { 323 | scriptFileNamePath != null -> { 324 | logger.debug { "resolved script via SCRIPT_FILENAME=$scriptFileName" } 325 | scriptFileNamePath 326 | } 327 | 328 | documentRoot != null && scriptName != null -> { 329 | val scriptNamePath = "$documentRoot$scriptName".toPath().takeIf(FileSystem.SYSTEM::exists) 330 | if (scriptNamePath != null) { 331 | logger.debug { "resolved script via DOCUMENT_ROOT/SCRIPT_NAME=$documentRoot$scriptName" } 332 | scriptNamePath 333 | } else { 334 | logger.warn { "couldn't resolve script via SCRIPT_FILENAME($scriptFileName)/DOCUMENT_ROOT($documentRoot)/SCRIPT_NAME($scriptName)" } 335 | null 336 | } 337 | } 338 | 339 | else -> { 340 | logger.warn { "couldn't resolve script via SCRIPT_FILENAME($scriptFileName)/DOCUMENT_ROOT($documentRoot)/SCRIPT_NAME($scriptName)" } 341 | null 342 | } 343 | } 344 | } 345 | 346 | private const val DEFAULT_MAX_RECORD_SIZE = 8184L 347 | -------------------------------------------------------------------------------- /scripting-host/src/main/kotlin/ScriptingHost.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Eduard Wolf 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.edwardday.serverscript.scripthost 18 | 19 | import io.github.oshai.kotlinlogging.KotlinLogging 20 | import io.github.oshai.kotlinlogging.withLoggingContext 21 | import kotlinx.coroutines.Dispatchers 22 | import kotlinx.coroutines.coroutineScope 23 | import kotlinx.coroutines.flow.Flow 24 | import kotlinx.coroutines.flow.emptyFlow 25 | import kotlinx.coroutines.flow.map 26 | import kotlinx.coroutines.flow.transform 27 | import kotlinx.coroutines.joinAll 28 | import kotlinx.coroutines.launch 29 | import kotlinx.coroutines.runInterruptible 30 | import kotlinx.coroutines.slf4j.MDCContext 31 | import net.edwardday.serverscript.scriptdefinition.script.Cache 32 | import okio.Buffer 33 | import okio.BufferedSource 34 | import okio.IOException 35 | import okio.buffer 36 | import okio.use 37 | import java.net.StandardProtocolFamily 38 | import java.net.UnixDomainSocketAddress 39 | import java.nio.channels.ServerSocketChannel 40 | import java.nio.file.Files 41 | import java.nio.file.attribute.PosixFilePermission 42 | import kotlin.io.path.absolutePathString 43 | import kotlin.io.path.setPosixFilePermissions 44 | 45 | private val logger = KotlinLogging.logger {} 46 | 47 | suspend fun readFromSocket(socket: String, maxConnections: Int) { 48 | logger.info { "start kss process" } 49 | logger.debug { "reading from $socket with $maxConnections max connections" } 50 | val socketAddress = UnixDomainSocketAddress.of(socket) 51 | runInterruptible { Files.deleteIfExists(socketAddress.path) } 52 | val socketPath = socketAddress.path.absolutePathString() 53 | 54 | val globalState = GlobalState(maxConnections) 55 | try { 56 | runInterruptible { 57 | ServerSocketChannel.open(StandardProtocolFamily.UNIX).bind(socketAddress) 58 | }.use { serverChannel -> 59 | logger.debug { "set socket permissions" } 60 | socketAddress.path.setPosixFilePermissions( 61 | setOf( 62 | PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE, 63 | PosixFilePermission.GROUP_READ, PosixFilePermission.GROUP_WRITE, 64 | ), 65 | ) 66 | listenToConnections( 67 | maxConnections = maxConnections, 68 | serverChannel = serverChannel, 69 | socketDescription = socketPath, 70 | globalState = globalState, 71 | ) 72 | } 73 | } finally { 74 | runInterruptible { 75 | Files.deleteIfExists(socketAddress.path) 76 | } 77 | } 78 | } 79 | 80 | private suspend fun listenToConnections( 81 | maxConnections: Int, 82 | serverChannel: ServerSocketChannel, 83 | socketDescription: String, 84 | globalState: GlobalState, 85 | ) { 86 | coroutineScope { 87 | List(maxConnections) { index -> 88 | withLoggingContext("Connection Index" to index.toString()) { 89 | launch(MDCContext()) { 90 | handleRequests( 91 | serverChannel = serverChannel, 92 | socketDescription = socketDescription, 93 | connectionState = globalState.connectionStates[index] 94 | ) 95 | } 96 | } 97 | }.joinAll() 98 | } 99 | } 100 | 101 | private suspend fun handleRequests( 102 | serverChannel: ServerSocketChannel, 103 | socketDescription: String, 104 | connectionState: GlobalState.ConnectionState, 105 | ): Nothing { 106 | while (true) { 107 | logger.debug { "listen for connection" } 108 | val socketChannel = try { 109 | runInterruptible(Dispatchers.IO, block = serverChannel::accept) 110 | } catch (e: Exception) { 111 | throw IllegalStateException("cannot accept socket of $socketDescription", e) 112 | } 113 | logger.debug { "open connection" } 114 | val source = socketChannel.source().buffer() 115 | val sink = socketChannel.sink().buffer() 116 | while (true) { 117 | try { 118 | logger.trace { "read message" } 119 | val message = runInterruptible(Dispatchers.IO) { FCGIRequestMessage.read(source) } 120 | val result = connectionState.handleMessage(message) 121 | var close = false 122 | result.collect { responseMessage -> 123 | when (responseMessage) { 124 | is GlobalState.HandleResult.CloseConnection -> close = true 125 | is GlobalState.HandleResult.Message -> runInterruptible { 126 | responseMessage.message.write(sink) 127 | sink.flush() 128 | } 129 | } 130 | } 131 | if (close) { 132 | logger.debug { "close connection" } 133 | // when connection closed 134 | runInterruptible { 135 | sink.close() 136 | source.close() 137 | } 138 | break 139 | } 140 | } catch (e: IOException) { 141 | logger.error(e) { "exception during connection" } 142 | try { 143 | runInterruptible { 144 | sink.close() 145 | source.close() 146 | } 147 | } catch (_: Exception) { 148 | } 149 | break 150 | } 151 | } 152 | } 153 | } 154 | 155 | class GlobalState(private val maxConnections: Int) { 156 | private val cache: Cache = CacheImpl() 157 | val connectionStates = List(maxConnections) { ConnectionState() } 158 | 159 | sealed class HandleResult { 160 | data object CloseConnection : HandleResult() 161 | data class Message(val message: FCGIResponseMessage) : HandleResult() 162 | } 163 | 164 | inner class ConnectionState internal constructor() { 165 | private val requests = mutableListOf() 166 | 167 | private fun getRequestState(request: FCGIRequestMessage): RequestState? { 168 | val index = request.requestId - 1 169 | return if (request.record is FCGIRecord.BeginRequest) { 170 | repeat(index + 1 - requests.size) { requests.add(null) } 171 | RequestState(request.record, cache).also { 172 | requests[index] = it 173 | } 174 | } else { 175 | requests.getOrNull(index) 176 | } 177 | } 178 | 179 | fun handleMessage(message: FCGIRequestMessage): Flow { 180 | return if (message.requestId == 0) { 181 | if (message.record is FCGIRecord.GetValues) { 182 | ManagementRequestState.handleGetValues(message.record, maxConnections) 183 | .map(HandleResult::Message) 184 | } else { 185 | logger.error { "can't handle ${message.record::class.simpleName} with request id 0" } 186 | emptyFlow() 187 | } 188 | } else if (message.record is FCGIRecord.ReadUnknownType) { 189 | logger.error { "can't handle unknown fcgi record type (${message.record.type}) for request ${message.requestId}" } 190 | ManagementRequestState.handleUnknownType(message.record.type).map(HandleResult::Message) 191 | } else { 192 | val requestState = getRequestState(message) 193 | if (requestState != null) { 194 | val responses = requestState.handleRecord(message.record) 195 | responses.map { HandleResult.Message(FCGIResponseMessage(message.requestId, it)) } 196 | .transform { response -> 197 | emit(response) 198 | if (response.message.record is FCGIRecord.EndRequest) { 199 | requests[message.requestId - 1] = null 200 | if (!requestState.keepConnection && requests.all { request -> request == null }) { 201 | emit(HandleResult.CloseConnection) 202 | } 203 | } 204 | } 205 | } else { 206 | logger.error { "received $message without a begin request" } 207 | emptyFlow() 208 | } 209 | } 210 | } 211 | } 212 | } 213 | 214 | fun BufferedSource.readBuffer(contentLength: Long): Buffer = Buffer().write(this, contentLength) 215 | -------------------------------------------------------------------------------- /scripting-host/src/main/kotlin/okio.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 Square, Inc. 3 | * Copyright 2024 Eduard Wolf 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package net.edwardday.serverscript.scripthost 18 | 19 | import java.io.IOException 20 | import java.nio.ByteBuffer 21 | import java.nio.channels.ReadableByteChannel 22 | import java.nio.channels.WritableByteChannel 23 | import java.util.concurrent.TimeUnit 24 | import kotlin.math.min 25 | import okio.AsyncTimeout 26 | import okio.Buffer 27 | import okio.Sink 28 | import okio.Source 29 | import okio.Timeout 30 | 31 | fun ReadableByteChannel.source(): Source { 32 | val timeout = AsyncTimeout() 33 | timeout.timeout(100, TimeUnit.MILLISECONDS) 34 | return timeout.source(ByteChannelSource(this, timeout)) 35 | } 36 | 37 | /* 38 | * Copy from 39 | * https://github.com/square/okio/blob/b284bc07803e4f9658a1e65c55d57abe28c85c1e/samples/src/jvmMain/java/okio/samples/ByteChannelSource.java 40 | * and converted to kotlin 41 | */ 42 | /** 43 | * Creates a [Source] around a [ReadableByteChannel] and efficiently reads data using an [Buffer.UnsafeCursor]. 44 | * 45 | * This is a basic example showing another use for the [Buffer.UnsafeCursor]. Using the [ByteBuffer.wrap] along with 46 | * access to [Buffer] segments, a [ReadableByteChannel] can be given direct access to [Buffer] data without having to 47 | * copy the data. 48 | */ 49 | internal class ByteChannelSource(private val channel: ReadableByteChannel, private val timeout: Timeout) : Source { 50 | private val cursor: Buffer.UnsafeCursor = Buffer.UnsafeCursor() 51 | 52 | @Throws(IOException::class) 53 | override fun read(sink: Buffer, byteCount: Long): Long { 54 | check(channel.isOpen) { "closed" } 55 | sink.readAndWriteUnsafe(cursor).use { _ -> 56 | timeout.throwIfReached() 57 | val oldSize = sink.size 58 | val length = min(8192, byteCount).toInt() 59 | cursor.expandBuffer(length) 60 | val read = channel.read(ByteBuffer.wrap(cursor.data, cursor.start, length)) 61 | return if (read == -1) { 62 | cursor.resizeBuffer(oldSize) 63 | -1 64 | } else { 65 | cursor.resizeBuffer(oldSize + read) 66 | read.toLong() 67 | } 68 | } 69 | } 70 | 71 | override fun timeout(): Timeout = timeout 72 | 73 | @Throws(IOException::class) 74 | override fun close() { 75 | channel.close() 76 | } 77 | } 78 | 79 | fun WritableByteChannel.sink(): Sink { 80 | val timeout = AsyncTimeout() 81 | timeout.timeout(100, TimeUnit.MILLISECONDS) 82 | return timeout.sink(ByteChannelSink(this, timeout)) 83 | } 84 | 85 | /* 86 | * Copy from 87 | * https://github.com/square/okio/blob/b284bc07803e4f9658a1e65c55d57abe28c85c1e/samples/src/jvmMain/java/okio/samples/ByteChannelSink.java 88 | * and converted to kotlin 89 | */ 90 | /** 91 | * Creates a [Sink] around a [WritableByteChannel] and efficiently writes data using an [Buffer.UnsafeCursor]. 92 | * 93 | * This is a basic example showing another use for the [Buffer.UnsafeCursor]. Using the [ByteBuffer.wrap] along with 94 | * access to [Buffer] segments, a [WritableByteChannel] can be given direct access to [Buffer] data without having to 95 | * copy the data. 96 | */ 97 | internal class ByteChannelSink(private val channel: WritableByteChannel, private val timeout: Timeout) : Sink { 98 | private val cursor: Buffer.UnsafeCursor = Buffer.UnsafeCursor() 99 | 100 | @Throws(IOException::class) 101 | override fun write(source: Buffer, byteCount: Long) { 102 | check(channel.isOpen) { "closed" } 103 | if (byteCount == 0L) return 104 | var remaining = byteCount 105 | while (remaining > 0) { 106 | timeout.throwIfReached() 107 | source.readUnsafe(cursor).use { _ -> 108 | cursor.seek(0) 109 | val length = min(cursor.end - cursor.start, remaining.toInt()) 110 | val written = channel.write(ByteBuffer.wrap(cursor.data, cursor.start, length)) 111 | remaining -= written.toLong() 112 | source.skip(written.toLong()) 113 | } 114 | } 115 | } 116 | 117 | override fun flush() {} 118 | override fun timeout(): Timeout = timeout 119 | 120 | @Throws(IOException::class) 121 | override fun close() { 122 | channel.close() 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /scripting-host/src/main/resources/kss.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2024 Eduard Wolf 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | connections.max=4 18 | socket.address=/var/run/kss/kss.sock 19 | logging.logback.configurationFile= 20 | dependencies.maven.settingsFile= 21 | dependencies.maven.homeDirectory= 22 | -------------------------------------------------------------------------------- /scripting-host/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%kvp-%mdc- %msg%n 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /scripting-host/src/release/config/kss.properties.sample: -------------------------------------------------------------------------------- 1 | # max amount of synchronous connections to the kss process 2 | connections.max=4 3 | # path to the socket to read fcgi data from 4 | socket.address=/var/run/kss/kss.sock 5 | # path to custom logback logging configuration file 6 | logging.logback.configurationFile= 7 | -------------------------------------------------------------------------------- /scripting-host/src/release/config/logback.xml.sample: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%kvp-%mdc- %msg%n 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /scripting-host/src/release/service/kss.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Label 6 | net.edwardday.kss 7 | 8 | Program 9 | {{DIRECTORY}}kss 10 | 11 | StandardOutPath 12 | {{LOG_DIRECTORY}}kss.log 13 | 14 | StandardErrorPath 15 | {{LOG_DIRECTORY}}kss-error.log 16 | 17 | WorkingDirectory 18 | {{WORKING_DIRECTORY}} 19 | 20 | KeepAlive 21 | 22 | 23 | UserName 24 | {{USER}} 25 | 26 | 27 | -------------------------------------------------------------------------------- /scripting-host/src/release/service/kss.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=The kss kotlin server scripts Process Manager 3 | After=network.target 4 | 5 | [Service] 6 | Type=simple 7 | User={{USER}} 8 | ExecStart={{DIRECTORY}}kss 9 | WorkingDirectory={{WORKING_DIRECTORY}} 10 | RestartSec=10 11 | RestartMaxDelaySec=300 12 | RestartSteps=29 13 | Restart=on-failure 14 | TimeoutStopSec=30 15 | 16 | [Install] 17 | WantedBy=multi-user.target 18 | -------------------------------------------------------------------------------- /scripting-host/src/test/kotlin/ServerScriptingHostCacheTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Eduard Wolf 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.edwardday.serverscript.scripthost 18 | 19 | import kotlinx.coroutines.test.runTest 20 | import kotlin.test.Test 21 | import kotlin.test.assertEquals 22 | 23 | class ServerScriptingHostCacheTest { 24 | 25 | @Test 26 | fun testCacheWithUnixDomainSockets() = runTest { 27 | val testData = readResource("cache_data") 28 | 29 | val urls = List(5) { testData } 30 | val actualResponses = executeWithUnixDomainSockets { 31 | executeScripts(it, urls) 32 | } 33 | 34 | repeat(5) { index -> 35 | val counter = index + 1 36 | val expected = testData.body.map { it.replace("{counter}", "$counter") } 37 | 38 | val actual = actualResponses[index] 39 | 40 | assertEquals(expected, actual) 41 | } 42 | } 43 | 44 | @Test 45 | fun testSharedCacheWithUnixDomainSockets() = runTest { 46 | val testData1 = readResource("cache_import_script_1") 47 | val testData2 = readResource("cache_import_script_2") 48 | 49 | val urls = listOf(testData1, testData2) 50 | val actualResponses = executeWithUnixDomainSockets { 51 | executeScripts(it, urls) 52 | } 53 | 54 | assertEquals(testData1.body, actualResponses[0]) 55 | assertEquals(testData2.body, actualResponses[1]) 56 | } 57 | } -------------------------------------------------------------------------------- /scripting-host/src/test/kotlin/ServerScriptingHostScriptsTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Eduard Wolf 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.edwardday.serverscript.scripthost 18 | 19 | import kotlinx.coroutines.test.runTest 20 | import org.junit.runner.RunWith 21 | import org.junit.runners.Parameterized 22 | import kotlin.test.Test 23 | import kotlin.test.assertEquals 24 | 25 | @RunWith(Parameterized::class) 26 | class ServerScriptingHostScriptsTest(private val testCaseName: String) { 27 | 28 | @Test 29 | fun testScript() = runTest { 30 | val testData = readResource(testCaseName) 31 | 32 | val actual = executeWithUnixDomainSockets { executeScript(it, testData) } 33 | 34 | assertEquals(testData.body, actual) 35 | } 36 | 37 | companion object { 38 | 39 | @JvmStatic 40 | @Parameterized.Parameters(name = "script:{0}") 41 | fun data() = listOf( 42 | "big_output", 43 | "custom_headers", 44 | "dependency", 45 | "dependency_repository", 46 | "empty", 47 | "exception", 48 | "import_function", 49 | "import_invalid", 50 | "import_not_existent", 51 | "import_script", 52 | "invalid", 53 | "known_status", 54 | "multiple_output", 55 | "simple_script", 56 | "unknown_status", 57 | ) 58 | } 59 | } -------------------------------------------------------------------------------- /scripting-host/src/test/kotlin/TestUtils.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Eduard Wolf 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.edwardday.serverscript.scripthost 18 | 19 | import kotlinx.coroutines.CoroutineScope 20 | import kotlinx.coroutines.Dispatchers 21 | import kotlinx.coroutines.coroutineScope 22 | import kotlinx.coroutines.launch 23 | import kotlinx.coroutines.runInterruptible 24 | import kotlinx.coroutines.yield 25 | import net.edwardday.serverscript.scripthost.KeyValuePair.Companion.toBuffer 26 | import java.io.BufferedReader 27 | import java.net.StandardProtocolFamily 28 | import java.net.UnixDomainSocketAddress 29 | import java.nio.ByteBuffer 30 | import java.nio.channels.SocketChannel 31 | import java.nio.file.Files 32 | import kotlin.io.path.ExperimentalPathApi 33 | import kotlin.io.path.absolutePathString 34 | import kotlin.io.path.deleteRecursively 35 | import kotlin.test.assertEquals 36 | import kotlin.test.assertFalse 37 | import kotlin.test.assertTrue 38 | 39 | fun readResource(resourceName: String): TestData { 40 | val contextClassLoader = Thread.currentThread().contextClassLoader 41 | val scriptUrl = contextClassLoader.getResource("$resourceName.server.kts")!!.file 42 | fun readFileText(suffix: String): String? = contextClassLoader.getResourceAsStream("$resourceName.$suffix") 43 | ?.bufferedReader()?.use(BufferedReader::readText) 44 | 45 | val body = readFileText("body") 46 | val header = readFileText("header") 47 | val status = readFileText("status")?.toInt() ?: 0 48 | val content = buildString { 49 | if (header != null) { 50 | append(header) 51 | append('\n') 52 | } 53 | append('\n') 54 | if (body != null) { 55 | append(body) 56 | } 57 | } 58 | return TestData( 59 | url = scriptUrl, 60 | body = content.lines(), 61 | status = status, 62 | ) 63 | } 64 | 65 | class TestData( 66 | val url: String, 67 | val body: List, 68 | val status: Int, 69 | ) 70 | 71 | @OptIn(ExperimentalPathApi::class) 72 | suspend fun executeWithUnixDomainSockets(block: suspend CoroutineScope.(SocketChannel) -> T): T { 73 | return coroutineScope { 74 | val directory = Files.createTempDirectory("sockets") 75 | val socket = directory.absolutePathString().let { if (it.endsWith('/')) it else "$it/" } + "fastcgi.socket" 76 | val hostJob = launch { readFromSocket(socket, 2) } 77 | yield() 78 | val udsSocketAddress = UnixDomainSocketAddress.of(socket) 79 | SocketChannel.open(StandardProtocolFamily.UNIX).use { channel -> 80 | assertTrue { channel.connect(udsSocketAddress) } 81 | block(channel) 82 | }.also { 83 | hostJob.cancel() 84 | directory.deleteRecursively() 85 | } 86 | } 87 | } 88 | 89 | suspend fun executeScripts( 90 | channel: SocketChannel, 91 | testData: List, 92 | ): List> { 93 | val result = testData.mapIndexed { index, testDate -> 94 | val keepConnection: Byte = if (index < testData.lastIndex) 1 else 0 95 | val buffer = ByteBuffer.allocate(8192) 96 | fun putMessageInBuffer( 97 | type: Byte, 98 | content: ByteArray, 99 | ) { 100 | buffer.put(1) // version 101 | buffer.put(type) // type begin request 102 | buffer.putShort(1) // request id 103 | buffer.putShort(content.size.toShort()) // content length 104 | buffer.put(0) // padding length 105 | buffer.put(0) // skipped 106 | buffer.put(content) 107 | // no padding 108 | } 109 | // Begin request 110 | putMessageInBuffer( 111 | type = 1, 112 | content = byteArrayOf( 113 | 0, 1, // role responder 114 | keepConnection, // flags 115 | 0, 0, 0, 0, 0, // unused 116 | ) 117 | ) 118 | 119 | // params 120 | putMessageInBuffer( 121 | type = 4, 122 | content = listOf(KeyValuePair("SCRIPT_FILENAME", testDate.url)).toBuffer().readByteArray(), 123 | ) 124 | 125 | // params end 126 | putMessageInBuffer( 127 | type = 4, 128 | content = byteArrayOf(), 129 | ) 130 | 131 | // stdin end 132 | putMessageInBuffer( 133 | type = 5, 134 | content = byteArrayOf(), 135 | ) 136 | 137 | buffer.flip() 138 | runInterruptible { channel.write(buffer) } 139 | 140 | suspend fun readIntoBuffer() { 141 | yield() 142 | if (!buffer.hasRemaining()) { 143 | buffer.clear() 144 | runInterruptible(Dispatchers.IO) { channel.read(buffer) } 145 | buffer.flip() 146 | } 147 | } 148 | // read response 149 | val response = buildString { 150 | while (true) { 151 | readIntoBuffer() 152 | assertEquals(1, buffer.get()) // version 153 | assertEquals(6, buffer.get()) // type data 154 | assertEquals(1, buffer.getShort()) // request id 155 | val contentLength = buffer.getShort() 156 | val paddingLength = buffer.get() 157 | buffer.get() // skipped 158 | if (contentLength > 0) { 159 | val content = ByteArray(contentLength.toInt()) 160 | buffer.get(content) 161 | append(String(content)) 162 | repeat(paddingLength.toInt()) { buffer.get() } // skip padding 163 | } else { 164 | assertEquals(0, paddingLength) 165 | break 166 | } 167 | } 168 | } 169 | 170 | readIntoBuffer() 171 | // end response 172 | assertEquals(1, buffer.get()) // version 173 | assertEquals(3, buffer.get()) // type end 174 | assertEquals(1, buffer.getShort()) // request id 175 | assertEquals(8, buffer.getShort()) // content length 176 | assertEquals(0, buffer.get()) // padding length 177 | buffer.get() // skipped 178 | assertEquals(testDate.status, buffer.getInt()) // app status 179 | assertEquals(0, buffer.get()) // protocol status RequestComplete 180 | repeat(3) { buffer.get() } // skip 181 | 182 | yield() 183 | assertFalse(buffer.hasRemaining()) 184 | 185 | response.split("\n") 186 | } 187 | 188 | return result 189 | } 190 | 191 | suspend fun executeScript(channel: SocketChannel, testData: TestData): List = 192 | executeScripts(channel, listOf(testData)).single() 193 | -------------------------------------------------------------------------------- /scripting-host/src/test/resources/big_output.body: -------------------------------------------------------------------------------- 1 | Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. 2 | Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. 3 | Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. 4 | Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. 5 | Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis. 6 | At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, At accusam aliquyam diam diam dolore dolores duo eirmod eos erat, et nonumy sed tempor et et invidunt justo labore Stet clita ea et gubergren, kasd magna no rebum. sanctus sea sed takimata ut vero voluptua. est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat. 7 | Consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus. 8 | Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. 9 | Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. 10 | Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. 11 | Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. 12 | Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis. 13 | At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, At accusam aliquyam diam diam dolore dolores duo eirmod eos erat, et nonumy sed tempor et et invidunt justo labore Stet clita ea et gubergren, kasd magna no rebum. sanctus sea sed takimata ut vero voluptua. est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat. 14 | Consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus. 15 | Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. 16 | Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. 17 | Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. 18 | Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. 19 | Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis. 20 | At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, At accusam aliquyam diam diam dolore dolores duo eirmod eos erat, et nonumy sed tempor et et invidunt justo labore Stet clita ea et gubergren, kasd magna no rebum. sanctus sea sed takimata ut vero voluptua. est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat. 21 | Consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus. 22 | Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero -------------------------------------------------------------------------------- /scripting-host/src/test/resources/big_output.server.kts: -------------------------------------------------------------------------------- 1 | writeOutput(""" 2 | Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. 3 | Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. 4 | Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. 5 | Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. 6 | Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis. 7 | At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, At accusam aliquyam diam diam dolore dolores duo eirmod eos erat, et nonumy sed tempor et et invidunt justo labore Stet clita ea et gubergren, kasd magna no rebum. sanctus sea sed takimata ut vero voluptua. est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat. 8 | Consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus. 9 | Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. 10 | Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. 11 | Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. 12 | Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. 13 | Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis. 14 | At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, At accusam aliquyam diam diam dolore dolores duo eirmod eos erat, et nonumy sed tempor et et invidunt justo labore Stet clita ea et gubergren, kasd magna no rebum. sanctus sea sed takimata ut vero voluptua. est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat. 15 | Consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus. 16 | Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. 17 | Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. 18 | Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. 19 | Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. 20 | Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis. 21 | At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, At accusam aliquyam diam diam dolore dolores duo eirmod eos erat, et nonumy sed tempor et et invidunt justo labore Stet clita ea et gubergren, kasd magna no rebum. sanctus sea sed takimata ut vero voluptua. est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat. 22 | Consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus. 23 | Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero 24 | """.trimIndent()) 25 | -------------------------------------------------------------------------------- /scripting-host/src/test/resources/cache_data.body: -------------------------------------------------------------------------------- 1 | counter: {counter} -------------------------------------------------------------------------------- /scripting-host/src/test/resources/cache_data.server.kts: -------------------------------------------------------------------------------- 1 | val counter: Int = cache.updateOrSet("COUNTER", { 1 }, 1::plus) 2 | writeOutput("counter: $counter") 3 | -------------------------------------------------------------------------------- /scripting-host/src/test/resources/cache_import_script_1.body: -------------------------------------------------------------------------------- 1 | shared invocation counter: 1 2 | script invocation counter: 1 -------------------------------------------------------------------------------- /scripting-host/src/test/resources/cache_import_script_1.server.kts: -------------------------------------------------------------------------------- 1 | @file:Import("imports/invocation_counter") 2 | 3 | val invocation: Int = cache.updateOrSet("INVOCATION", { 1 }, 1::plus) 4 | 5 | writeOutput("script invocation counter: $invocation") 6 | -------------------------------------------------------------------------------- /scripting-host/src/test/resources/cache_import_script_2.body: -------------------------------------------------------------------------------- 1 | shared invocation counter: 2 2 | script invocation counter: 1 -------------------------------------------------------------------------------- /scripting-host/src/test/resources/cache_import_script_2.server.kts: -------------------------------------------------------------------------------- 1 | @file:Import("imports/invocation_counter") 2 | 3 | val invocation: Int = cache.updateOrSet("INVOCATION", { 1 }, 1::plus) 4 | 5 | writeOutput("script invocation counter: $invocation") 6 | -------------------------------------------------------------------------------- /scripting-host/src/test/resources/custom_headers.body: -------------------------------------------------------------------------------- 1 | OK -------------------------------------------------------------------------------- /scripting-host/src/test/resources/custom_headers.header: -------------------------------------------------------------------------------- 1 | X-TestHeader:foo.bar 2 | X-TestHeader:foo.bar.2 -------------------------------------------------------------------------------- /scripting-host/src/test/resources/custom_headers.server.kts: -------------------------------------------------------------------------------- 1 | setHeader("X-TestHeader", "foo.bar") 2 | addHeader("X-TestHeader", "foo.bar.2") 3 | 4 | writeOutput("OK") 5 | -------------------------------------------------------------------------------- /scripting-host/src/test/resources/dependency.body: -------------------------------------------------------------------------------- 1 | [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99] -------------------------------------------------------------------------------- /scripting-host/src/test/resources/dependency.server.kts: -------------------------------------------------------------------------------- 1 | @file:DependsOn("org.jetbrains.kotlinx:kotlinx-serialization-json:1.8.0") 2 | 3 | import kotlinx.serialization.* 4 | import kotlinx.serialization.json.* 5 | 6 | 7 | val list = List(100) { it } 8 | 9 | writeOutput(Json.encodeToString(list)) 10 | -------------------------------------------------------------------------------- /scripting-host/src/test/resources/dependency_repository.body: -------------------------------------------------------------------------------- 1 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99] -------------------------------------------------------------------------------- /scripting-host/src/test/resources/dependency_repository.server.kts: -------------------------------------------------------------------------------- 1 | @file:Repository("https://dl.google.com/dl/android/maven2/") 2 | @file:DependsOn("androidx.collection:collection:1.4.5") 3 | 4 | import androidx.collection.intListOf 5 | 6 | val list = intListOf(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99) 7 | 8 | writeOutput(list.toString()) 9 | -------------------------------------------------------------------------------- /scripting-host/src/test/resources/empty.server.kts: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EdwarDDay/kotlin-server-scripts/cdfed81c9ee3780333eb9e6feae8d10940dc1b84/scripting-host/src/test/resources/empty.server.kts -------------------------------------------------------------------------------- /scripting-host/src/test/resources/exception.header: -------------------------------------------------------------------------------- 1 | Status:500 Internal Server Error -------------------------------------------------------------------------------- /scripting-host/src/test/resources/exception.server.kts: -------------------------------------------------------------------------------- 1 | error("Noooooo") 2 | -------------------------------------------------------------------------------- /scripting-host/src/test/resources/exception.status: -------------------------------------------------------------------------------- 1 | 1 -------------------------------------------------------------------------------- /scripting-host/src/test/resources/import_function.body: -------------------------------------------------------------------------------- 1 | {"message":"OK","code":200} -------------------------------------------------------------------------------- /scripting-host/src/test/resources/import_function.server.kts: -------------------------------------------------------------------------------- 1 | @file:Import("imports/to_json") 2 | 3 | 4 | class Output(val message: String, val code: Int) 5 | 6 | val output = Output("OK", 200).toJson() 7 | 8 | writeOutput(output) 9 | -------------------------------------------------------------------------------- /scripting-host/src/test/resources/import_invalid.header: -------------------------------------------------------------------------------- 1 | Status:500 Internal Server Error -------------------------------------------------------------------------------- /scripting-host/src/test/resources/import_invalid.server.kts: -------------------------------------------------------------------------------- 1 | @file:Import("invalid") 2 | 3 | writeOutput("OK") 4 | -------------------------------------------------------------------------------- /scripting-host/src/test/resources/import_invalid.status: -------------------------------------------------------------------------------- 1 | 2 -------------------------------------------------------------------------------- /scripting-host/src/test/resources/import_not_existent.header: -------------------------------------------------------------------------------- 1 | Status:500 Internal Server Error -------------------------------------------------------------------------------- /scripting-host/src/test/resources/import_not_existent.server.kts: -------------------------------------------------------------------------------- 1 | @file:Import("imports/not_existent") 2 | 3 | writeOutput("OK") 4 | -------------------------------------------------------------------------------- /scripting-host/src/test/resources/import_not_existent.status: -------------------------------------------------------------------------------- 1 | 2 -------------------------------------------------------------------------------- /scripting-host/src/test/resources/import_script.body: -------------------------------------------------------------------------------- 1 | OK -------------------------------------------------------------------------------- /scripting-host/src/test/resources/import_script.header: -------------------------------------------------------------------------------- 1 | Cache-Control:no-cache -------------------------------------------------------------------------------- /scripting-host/src/test/resources/import_script.server.kts: -------------------------------------------------------------------------------- 1 | @file:Import("imports/no_cache_control") 2 | 3 | writeOutput("OK") 4 | -------------------------------------------------------------------------------- /scripting-host/src/test/resources/imports/invocation_counter.server.kts: -------------------------------------------------------------------------------- 1 | 2 | val sharedInvocation: Int = cache.updateOrSet("INVOCATION", { 1 }, 1::plus) 3 | 4 | writeOutput("shared invocation counter: $sharedInvocation\n") 5 | -------------------------------------------------------------------------------- /scripting-host/src/test/resources/imports/no_cache_control.server.kts: -------------------------------------------------------------------------------- 1 | addHeader("Cache-Control", "no-cache") 2 | -------------------------------------------------------------------------------- /scripting-host/src/test/resources/imports/to_json.server.kts: -------------------------------------------------------------------------------- 1 | @file:DependsOn("com.squareup.moshi:moshi-kotlin:1.15.2") 2 | 3 | import com.squareup.moshi.* 4 | import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory 5 | 6 | 7 | val moshi: Moshi = cache.getOrSet("MOSHI") { Moshi.Builder().addLast(KotlinJsonAdapterFactory()).build() } 8 | 9 | @OptIn(ExperimentalStdlibApi::class) 10 | inline fun T.toJson(): String = moshi.adapter().toJson(this) 11 | -------------------------------------------------------------------------------- /scripting-host/src/test/resources/invalid.header: -------------------------------------------------------------------------------- 1 | Status:500 Internal Server Error -------------------------------------------------------------------------------- /scripting-host/src/test/resources/invalid.server.kts: -------------------------------------------------------------------------------- 1 | writeOutput("OK 2 | -------------------------------------------------------------------------------- /scripting-host/src/test/resources/invalid.status: -------------------------------------------------------------------------------- 1 | 2 -------------------------------------------------------------------------------- /scripting-host/src/test/resources/known_status.body: -------------------------------------------------------------------------------- 1 | OK -------------------------------------------------------------------------------- /scripting-host/src/test/resources/known_status.header: -------------------------------------------------------------------------------- 1 | Status:418 I'm a teapot -------------------------------------------------------------------------------- /scripting-host/src/test/resources/known_status.server.kts: -------------------------------------------------------------------------------- 1 | status(418) 2 | 3 | writeOutput("OK") 4 | -------------------------------------------------------------------------------- /scripting-host/src/test/resources/multiple_output.body: -------------------------------------------------------------------------------- 1 | element: 0 2 | element: 1 3 | element: 2 4 | element: 3 5 | element: 4 6 | element: 5 7 | element: 6 8 | element: 7 9 | element: 8 10 | element: 9 11 | element: 10 12 | element: 11 13 | element: 12 14 | element: 13 15 | element: 14 16 | element: 15 17 | element: 16 18 | element: 17 19 | element: 18 20 | element: 19 21 | element: 20 22 | element: 21 23 | element: 22 24 | element: 23 25 | element: 24 26 | element: 25 27 | element: 26 28 | element: 27 29 | element: 28 30 | element: 29 31 | element: 30 32 | element: 31 33 | element: 32 34 | element: 33 35 | element: 34 36 | element: 35 37 | element: 36 38 | element: 37 39 | element: 38 40 | element: 39 41 | element: 40 42 | element: 41 43 | element: 42 44 | element: 43 45 | element: 44 46 | element: 45 47 | element: 46 48 | element: 47 49 | element: 48 50 | element: 49 51 | element: 50 52 | element: 51 53 | element: 52 54 | element: 53 55 | element: 54 56 | element: 55 57 | element: 56 58 | element: 57 59 | element: 58 60 | element: 59 61 | element: 60 62 | element: 61 63 | element: 62 64 | element: 63 65 | element: 64 66 | element: 65 67 | element: 66 68 | element: 67 69 | element: 68 70 | element: 69 71 | element: 70 72 | element: 71 73 | element: 72 74 | element: 73 75 | element: 74 76 | element: 75 77 | element: 76 78 | element: 77 79 | element: 78 80 | element: 79 81 | element: 80 82 | element: 81 83 | element: 82 84 | element: 83 85 | element: 84 86 | element: 85 87 | element: 86 88 | element: 87 89 | element: 88 90 | element: 89 91 | element: 90 92 | element: 91 93 | element: 92 94 | element: 93 95 | element: 94 96 | element: 95 97 | element: 96 98 | element: 97 99 | element: 98 100 | element: 99 101 | -------------------------------------------------------------------------------- /scripting-host/src/test/resources/multiple_output.server.kts: -------------------------------------------------------------------------------- 1 | 2 | repeat(100) { 3 | writeOutput("element: $it\n") 4 | } 5 | -------------------------------------------------------------------------------- /scripting-host/src/test/resources/simple_script.body: -------------------------------------------------------------------------------- 1 | OK -------------------------------------------------------------------------------- /scripting-host/src/test/resources/simple_script.server.kts: -------------------------------------------------------------------------------- 1 | writeOutput("OK") 2 | -------------------------------------------------------------------------------- /scripting-host/src/test/resources/unknown_status.body: -------------------------------------------------------------------------------- 1 | OK -------------------------------------------------------------------------------- /scripting-host/src/test/resources/unknown_status.header: -------------------------------------------------------------------------------- 1 | Status:600 -------------------------------------------------------------------------------- /scripting-host/src/test/resources/unknown_status.server.kts: -------------------------------------------------------------------------------- 1 | status(600) 2 | 3 | writeOutput("OK") 4 | -------------------------------------------------------------------------------- /settings.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2024 Eduard Wolf 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | rootProject.name = "KotlinServerScripts" 18 | 19 | include("scripting-definition") 20 | include("scripting-host") 21 | --------------------------------------------------------------------------------