add runner sample

This commit is contained in:
InsanusMokrassar 2023-05-11 01:25:11 +06:00
parent 1cc7441e54
commit d1c2dd079f
11 changed files with 210 additions and 4 deletions

View File

@ -0,0 +1,5 @@
{
"module_name": "Sample",
"module_package": "{{.module_name.lowercase()}}",
"module_path": "{{.module_package.replace('.', '/')}}"
}

View File

@ -0,0 +1,27 @@
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath libs.kotlin.gradle.plugin
classpath libs.kotlin.serialization.plugin
}
}
plugins {
alias libs.plugins.kotlin.jvm
alias libs.plugins.kotlin.serialization
}
repositories {
mavenCentral()
mavenLocal()
maven { url "https://git.inmo.dev/api/packages/InsanusMokrassar/maven" }
}
dependencies {
implementation libs.kotlin
api libs.plagubot.plugin
api libs.kslog
}

View File

@ -0,0 +1,60 @@
import dev.inmo.kslog.common.logger
import dev.inmo.kslog.common.w
import dev.inmo.plagubot.Plugin
import dev.inmo.tgbotapi.extensions.api.send.reply
import dev.inmo.tgbotapi.extensions.api.send.sendMessage
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onMyChatMemberUpdated
import dev.inmo.tgbotapi.types.chat.PrivateChat
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.*
import org.jetbrains.exposed.sql.Database
import org.koin.core.Koin
import org.koin.core.module.Module
/**
* This is template of plugin with preset [log]ger, [Config] and template configurations of [setupDI] and [setupBotPlugin].
* Replace [pluginConfigSectionName] value with your one to customize configuration section name
*/
@Serializable
class {{.module_name}}Plugin : Plugin {
/**
* Default logger of [WelcomePlugin] got with [logger]
*/
private val log = logger
/**
* Configuration class for current plugin
*
* See realization of [setupDI] to get know how this class will be deserialized from global config
*
* See realization of [setupBotPlugin] to get know how to get access to this class
*/
@Serializable
private class Config(
)
/**
* DI configuration of current plugin. Here we are decoding [Config] and put it into [Module] receiver
*/
override fun Module.setupDI(database: Database, params: JsonObject) {
single { get<Json>().decodeFromJsonElement(Config.serializer(), params[pluginConfigSectionName] ?: return@single null) }
}
/**
* Final configuration of bot. Here we are getting [Config] from [koin]
*/
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
val config = koin.getOrNull<Config>()
if (config == null) {
log.w("Plugin has been disabled due to absence of \"$pluginConfigSectionName\" field in config or some error during configuration loading")
return
}
}
companion object {
private const val pluginConfigSectionName = "new"
}
}

View File

@ -1,10 +1,10 @@
[versions] [versions]
kotlin = "1.8.20" kotlin = "1.8.21"
plagubot = "5.1.0" plagubot = "5.1.2"
kslog = "1.1.1" kslog = "1.1.1"
plagubot-commands = "0.11.0" plagubot-commands = "0.11.2"
tgbotapi-libraries = "0.11.0" tgbotapi-libraries = "0.11.2"
[libraries] [libraries]

8
runner/Dockerfile Normal file
View File

@ -0,0 +1,8 @@
FROM adoptopenjdk/openjdk11
USER 1000
ENTRYPOINT ["/runner/bin/runner", "/runner/local.config.json"]
ADD ./build/distributions/runner.tar /
ADD ./local.config.json /runner/

30
runner/build.gradle Normal file
View File

@ -0,0 +1,30 @@
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath libs.kotlin.gradle.plugin
classpath libs.kotlin.serialization.plugin
}
}
plugins {
alias libs.plugins.kotlin.jvm
alias libs.plugins.kotlin.serialization
id 'application'
}
application.mainClassName = 'dev.inmo.plagubot.AppKt'
repositories {
mavenCentral()
mavenLocal()
maven { url "https://git.inmo.dev/api/packages/InsanusMokrassar/maven" }
}
dependencies {
implementation libs.kotlin
api libs.plagubot.plugin
api libs.kslog
}

15
runner/config.json Normal file
View File

@ -0,0 +1,15 @@
{
"botToken": "1234567890:ABCDEFGHIJKLMNOP_qrstuvwxyz12345678",
"plugins": [
"dev.inmo.plagubot.plugins.commands.CommandsPlugin",
"IntroductionPlugin",
"WelcomePlugin"
],
"introduction": {
"onStartCommandMessage": "Hello World"
},
"database": {
"url": "jdbc:sqlite:file:/data/local.db"
}
}

25
runner/deploy Normal file
View File

@ -0,0 +1,25 @@
#!/bin/bash
function send_notification() {
echo "$1"
}
function assert_success() {
"${@}"
local status=${?}
if [ ${status} -ne 0 ]; then
send_notification "### Error ${status} at: ${BASH_LINENO[*]} ###"
exit ${status}
fi
}
app=runner
version=0.0.0
server="$USER"
assert_success ../gradlew build
assert_success sudo docker build -t $app:"$version" .
assert_success sudo docker tag $app:"$version" $server/$app:$version
assert_success sudo docker tag $app:"$version" $server/$app:latest
assert_success sudo docker push $server/$app:$version
assert_success sudo docker push $server/$app:latest

10
runner/docker-compose.yml Normal file
View File

@ -0,0 +1,10 @@
version: "3.4"
services:
adminbot:
image: user/runner
container_name: runner
restart: unless-stopped
volumes:
- "./data/:/data/"
- "./config.json:/config.json:ro"

25
runner/nonsudo_deploy Normal file
View File

@ -0,0 +1,25 @@
#!/bin/bash
function send_notification() {
echo "$1"
}
function assert_success() {
"${@}"
local status=${?}
if [ ${status} -ne 0 ]; then
send_notification "### Error ${status} at: ${BASH_LINENO[*]} ###"
exit ${status}
fi
}
app=runner
version=0.0.0
server="$USER"
assert_success ../gradlew build
assert_success docker build -t $app:"$version" .
assert_success docker tag $app:"$version" $server/$app:$version
assert_success docker tag $app:"$version" $server/$app:latest
assert_success docker push $server/$app:$version
assert_success docker push $server/$app:latest

View File

@ -2,3 +2,4 @@ rootProject.name = "tutorial"
include ":introduction" include ":introduction"
include ":welcome" include ":welcome"
include ":runner"