From 3a0c4d02ed4936c4ff489e9ebb25f4a04e991e65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=80=E4=B8=83=E5=B9=B4=E5=A4=8F?= <2098833867@qq.com> Date: Thu, 25 Apr 2024 23:23:34 +0800 Subject: [PATCH] =?UTF-8?q?Dockerfile=EF=BC=8C=E6=B7=BB=E5=8A=A0=E4=B8=80?= =?UTF-8?q?=E4=B8=AAhello=20world=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 19 +++++++++ docker_shell.sh | 42 +++++++++++++++++++ .../lolosia/web/controller/HomeController.kt | 20 +++++++-- .../web/model/session/SessionEntity.kt | 6 +-- .../lolosia/web/model/system/SystemModel.kt | 6 +-- src/main/resources/application.yaml | 25 +++-------- 6 files changed, 88 insertions(+), 30 deletions(-) create mode 100644 Dockerfile create mode 100644 docker_shell.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..8ee2dc3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +#制定java镜像的版本 +FROM java:17 + +#声明作者 +LABEL org.opencontainers.image.authors="Lolosia" + +#进入到镜像内app目录下面,类似cd +WORKDIR /app/ + +#复制fat.jar到镜像内app目录下 +ADD ./build/libs/*-fat.jar /app/ + +RUN mv *-fat.jar app.jar + +#对外暴露的端口 +EXPOSE 58801 + +#程序启动脚本 +CMD java -jar app.jar diff --git a/docker_shell.sh b/docker_shell.sh new file mode 100644 index 0000000..5fd72d5 --- /dev/null +++ b/docker_shell.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash + +declare name='lolosia-backend' + + +echo '停止容器:' ${name} +docker stop ${name} + +echo '删除容器:' ${name} +docker rm ${name} + + +decare imageId=$(docker images -q --filter "reference=$name") + +# 判断旧镜像是否存在 +if [ -n "$imageId" ];then + + decare imageId2=$(docker images -q --filter "reference=$name:old") + + # 判断之前是否还有其他的旧镜像 + if [ -n "$imageId2" ];then + # 删除这个旧镜像 + docker rmi "$imageId2" + fi + + # 修改旧镜像的名字为 名字:old + docker tag "$imageId" "$name:old" +fi + +echo "构建新版镜像: $name..." +docker build -t "$name" . + +if [ -n "$imageId" ];then + #删除旧的镜像 + echo "删除镜像: $name:old" + docker rmi "$imageId" +fi + +echo "启动新版容器: $name..." + +mkdir -p /home/lolosia-web/work +docker run -p 58801:58801 -d --restart=always -v -v /home/lolosia-web/work:/app/work --name ${name} ${name} \ No newline at end of file diff --git a/src/main/kotlin/top/lolosia/web/controller/HomeController.kt b/src/main/kotlin/top/lolosia/web/controller/HomeController.kt index 3d471f9..b7a38a3 100644 --- a/src/main/kotlin/top/lolosia/web/controller/HomeController.kt +++ b/src/main/kotlin/top/lolosia/web/controller/HomeController.kt @@ -3,12 +3,15 @@ package top.lolosia.web.controller import top.lolosia.web.service.HomeService import top.lolosia.web.util.bundle.Bundle import org.springframework.beans.factory.annotation.Autowired +import org.springframework.core.io.buffer.DefaultDataBufferFactory import org.springframework.http.HttpStatus +import org.springframework.http.MediaType import org.springframework.http.server.reactive.ServerHttpRequest import org.springframework.http.server.reactive.ServerHttpResponse import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RestController +import reactor.core.publisher.Mono import java.net.URI @RestController @@ -17,10 +20,19 @@ class HomeController { @Autowired lateinit var service: HomeService - @GetMapping("/") - fun home(resp: ServerHttpResponse) { - resp.statusCode = HttpStatus.FOUND - resp.headers.location = URI.create("/oAuth/") + @GetMapping(path = ["/", "/api/"]) + fun home(resp: ServerHttpResponse): Mono { + // resp.statusCode = HttpStatus.FOUND + // resp.headers.location = URI.create("/oAuth/") + resp.statusCode = HttpStatus.OK + resp.headers.contentType = MediaType.parseMediaType("text/plain;charset=UTF-8") + return resp.writeWith( + Mono.just( + DefaultDataBufferFactory.sharedInstance.wrap( + "你好,这里是Lolosia的后端".toByteArray(Charsets.UTF_8) + ) + ) + ) } private val ServerHttpRequest.sub: String diff --git a/src/main/kotlin/top/lolosia/web/model/session/SessionEntity.kt b/src/main/kotlin/top/lolosia/web/model/session/SessionEntity.kt index 0ef07c8..4b8a401 100644 --- a/src/main/kotlin/top/lolosia/web/model/session/SessionEntity.kt +++ b/src/main/kotlin/top/lolosia/web/model/session/SessionEntity.kt @@ -14,14 +14,14 @@ import jakarta.persistence.Entity import jakarta.persistence.Table import jakarta.persistence.Id -@DbName("session") +@DbName("db") @Entity @Table(name = "Sessions") -class SessionEntity : Model("session") { +class SessionEntity : Model("db") { companion object : ModelCompanion { @JvmStatic - override val database get() = DB.byName("session") + override val database get() = DB.byName("db") } @Id diff --git a/src/main/kotlin/top/lolosia/web/model/system/SystemModel.kt b/src/main/kotlin/top/lolosia/web/model/system/SystemModel.kt index d7673d5..dffce29 100644 --- a/src/main/kotlin/top/lolosia/web/model/system/SystemModel.kt +++ b/src/main/kotlin/top/lolosia/web/model/system/SystemModel.kt @@ -12,12 +12,12 @@ import java.sql.Timestamp import java.util.UUID import jakarta.persistence.MappedSuperclass -@DbName("system") +@DbName("db") @MappedSuperclass -abstract class SystemModel : Model("system") { +abstract class SystemModel : Model("db") { companion object : ModelCompanion { @JvmStatic - override val database get() = DB.byName("system") + override val database get() = DB.byName("db") } var createdBy: UUID? = null diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index bcdf3c9..6418ebc 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -1,27 +1,12 @@ datasource: db: - username: "root" - password: "123456" - driver: "org.sqlite.JDBC" - url: "jdbc:sqlite:work/database/db.sqlite" - decision: - username: "root" - password: "123456" - driver: "org.sqlite.JDBC" - url: "jdbc:sqlite:work/database/decision.sqlite" - session: - username: "root" - password: "123456" - driver: "org.sqlite.JDBC" - url: "jdbc:sqlite:work/database/session.sqlite" - system: - username: "root" - password: "123456" - driver: "org.sqlite.JDBC" - url: "jdbc:sqlite:work/database/system.sqlite" + username: "" + password: "" + driver: "org.mariadb.jdbc.Driver" + url: "jdbc:mariadb://172.17.0.1:3306/lolosia?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8" server: - port: 8002 + port: 58801 spring: devtools: