Skip to content

Commit

Permalink
Merge pull request #18 from mrhuo/add-settingini-generator
Browse files Browse the repository at this point in the history
add: setting.ini generator #17
  • Loading branch information
mrhuo authored Aug 13, 2024
2 parents 1de7aa3 + 04eccc8 commit 01bb39c
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 39 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- **0.0.4** `2024-7-21` Add `Oracle` Database Support
- **0.0.5** `2024-8-11` Delete `jar` File From Repository, Add Query At `list/paged` API
- **0.0.6** `2024-8-12` Add `GET API` Cache Support
- **0.0.7** `2024-8-14` Add `gen-setting` Support. [#17](https://github.com/mrhuo/database2api/issues/17)

## I. Introduction ⚡
**database2api** can intelligently parse the database structure and automatically generate the corresponding `API` interfaces according to the user's requirements and configuration. It enables you to easily achieve the interaction between the database and external applications without the cumbersome manual coding.
Expand Down Expand Up @@ -87,6 +88,8 @@ docker run -d -p 8989:8080 -v ./data:/usr/app/data database2api:0.0.4
└─ setting.ini <-- Configuration file (required)
```
- Sample configuration file `setting.ini`
> Using the command line tools can quickly generate configuration files, see [#17](https://github.com/mrhuo/database2api/issues/17)
```text
# Default port for API
API_PORT=8080
Expand Down
3 changes: 3 additions & 0 deletions README.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- **0.0.4** `2024-7-21` 增加 `Oracle` 数据库支持
- **0.0.5** `2024-8-11` 从仓库中删除 `jar` 文件,在 `list/paged` API 中增加查询
- **0.0.6** `2024-8-12` 增加 `GET``API` 缓存支持
- **0.0.7** `2024-8-14` 增加 `gen-setting` 功能. [#17](https://github.com/mrhuo/database2api/issues/17)

## 一、功能介绍 ⚡
**database2api** 能够智能地解析数据库结构,并根据用户的需求和配置,自动生成相应的 `API` 接口,让您无需繁琐的手动编码,即可轻松实现数据库与外部应用的交互。
Expand Down Expand Up @@ -86,6 +87,8 @@ docker run -d -p 8989:8080 -v ./data:/usr/app/data database2api:0.0.4
└─ setting.ini <-- 配置文件(必选)
```
- 配置文件 `setting.ini` 样例
> 使用命令行工具可快速生成配置文件,查看 [#17](https://github.com/mrhuo/database2api/issues/17)
```text
# API 默认端口
API_PORT=8080
Expand Down
2 changes: 1 addition & 1 deletion source/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.mrhuo</groupId>
<artifactId>database2api</artifactId>
<version>0.0.6</version>
<version>0.0.7</version>
<name>database2api</name>
<description>database2api</description>
<properties>
Expand Down
74 changes: 73 additions & 1 deletion source/src/main/kotlin/com/mrhuo/Application.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
package com.mrhuo

import cn.hutool.core.io.FileUtil
import cn.hutool.core.util.CharsetUtil
import cn.hutool.setting.Setting
import com.mrhuo.plugins.*
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import org.omg.CORBA.Environment
import java.io.File
import java.util.*
import kotlin.system.exitProcess

fun main() {
fun main(args: Array<String>) {
if (handleArgs(args)) {
return
}
Database2Api.init()
embeddedServer(
factory = Netty,
Expand All @@ -15,6 +25,68 @@ fun main() {
).start(wait = true)
}

fun handleArgs(args: Array<String>): Boolean {
if (args.isNotEmpty()) {
println("[database2api]")
println("------------------------------------------------")
println("DataBase to API, use database, generate open API")
println("https://github.com/mrhuo/database2api")
println("------------------------------------------------")
if (args[0] == "--help") {
println("用法:")
println("\tjava -jar database2api.jar\t\t\t\t\t启动 database2api")
println("\tjava -jar database2api.jar --help\t\t\t查看此帮助")
println("\tjava -jar database2api.jar --gen-setting\t快速生成数据库配置")
return true
}
if (args[0] == "--gen-setting") {
val root = getRootPath()
val mDataDir = File(root, "data")
println("-> 正在生成数据库配置...")
val settingFile = File(mDataDir, "setting.ini")
val scanner = Scanner(System.`in`)
if (settingFile.exists()) {
println("-> 警告:配置文件 ${settingFile.absolutePath} 已存在,是否删除?(Y/N)")
while (true) {
val line = scanner.nextLine()
if (line.isEmpty()) {
continue
}
if ("y".equals(line, ignoreCase = true)) {
settingFile.delete()
println("-> 警告:配置文件 ${settingFile.absolutePath} 已删除")
break
} else {
exitProcess(-1)
}
}
}
FileUtil.appendLines(
Database2Api.DEFAULT_SETTING_FILE_LINES,
settingFile,
CharsetUtil.CHARSET_UTF_8
)
val settings = Setting(settingFile.absolutePath, CharsetUtil.CHARSET_UTF_8, true)
var connectionString = ""
while (connectionString.isEmpty()) {
println("-> 请输入数据库连接字符串:")
connectionString = scanner.nextLine()
}
println("-> 请输入数据库用户名:")
val dbUser = scanner.nextLine()
println("-> 请输入数据库密码:")
val dbPass = scanner.nextLine()
println("-> 生成配置成功,保存路径 ${settingFile.absolutePath}")
settings.set("DB_URL", connectionString)
settings.set("DB_USER", dbUser)
settings.set("DB_PWD", dbPass)
settings.store()
return true
}
}
return false
}

fun Application.module() {
configureHTTP()
configureMonitoring()
Expand Down
76 changes: 39 additions & 37 deletions source/src/main/kotlin/com/mrhuo/Database2Api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,44 @@ object Database2Api {
const val AUTH_TYPE_JWT = "JWT"
const val AUTH_TYPE_BEARER = "Bearer"

val DEFAULT_SETTING_FILE_LINES = listOf(
"## DEFAULT CONFIG FOR database2api",
"# API 默认端口",
"API_PORT=8080",
"# 生成API的前缀,如设置 api/v1 后,则API变为:http://localhost:{PORT}/api/v1/xxxxxx",
"API_PREFIX=api",
"# 是否启用 API 文档,地址 http://localhost:{PORT}",
"API_INDEX_ENABLED=true",
"# 是否启用接口授权访问功能,默认不启用",
"API_AUTH_ENABLED=false",
"# 接口授权访问,支持:Basic, JWT, Bearer。为空不启用",
"API_AUTH_TYPE=",
"# 接口允许访问的用户名密码列表,用户名和密码之间英文冒号隔开,多用户英文逗号分隔",
"API_AUTH_USERS=admin:123456,user:1234",
"# Bearer 授权时应配置为[tag:token],tag表示这个token的归属,可为空(冒号不能省略)。",
"# API_AUTH_USERS=A公司:123,B公司:456,:789",
"# 数据库默认链接地址",
"DB_URL=jdbc:mysql://localhost:3306/db?useSSL=false&serverTimezone=UTC&charset=utf8mb4",
"# 数据库用户名",
"DB_USER=root",
"# 数据库密码",
"DB_PWD=",
"# 生成API的数据表名称,为空则所有的表都生成API,多个使用英文逗号分割",
"INCLUDE_TABLES=",
"# 需要忽略的数据表名称,如果不为空,则指定的表名被过滤,多个使用英文逗号分割",
"IGNORED_TABLES=",
"# 是否启用静态网站,启用后,则创建web目录,放入静态资源即可访问",
"STATIC_WEB_ENABLED=true",
"# 是否开启扩展API,允许用户使用JS代码使用自定义SQL查询数据库",
"EXT_API_ENABLED=true",
"# 是否开启表结构API,默认为false",
"SCHEMA_API_ENABLED=false",
"# 是否开启GET类API缓存,默认为true。对表的CREATE,UPDATE,DELETE操作都会使缓存失效",
"GET_API_CACHE=true",
"# GET类API缓存时间,默认30s",
"GET_API_CACHE_TIMEOUT=30000",
)

val JWT_SECRET: String = RandomUtil.randomString(32)
val JWT_ISSUER: String = "Database2Api"
val JWT_EXPIRED_AT: Long = Duration.ofDays(2).toMillis()
Expand Down Expand Up @@ -76,43 +114,7 @@ object Database2Api {
*/
private fun generateDefaultSetting() {
FileUtil.appendLines(
listOf(
"## DEFAULT CONFIG FOR database2api",
"# API 默认端口",
"API_PORT=8080",
"# 生成API的前缀,如设置 api/v1 后,则API变为:http://localhost:{PORT}/api/v1/xxxxxx",
"API_PREFIX=api",
"# 是否启用 API 文档,地址 http://localhost:{PORT}",
"API_INDEX_ENABLED=true",
"# 是否启用接口授权访问功能,默认不启用",
"API_AUTH_ENABLED=false",
"# 接口授权访问,支持:Basic, JWT, Bearer。为空不启用",
"API_AUTH_TYPE=",
"# 接口允许访问的用户名密码列表,用户名和密码之间英文冒号隔开,多用户英文逗号分隔",
"API_AUTH_USERS=admin:123456,user:1234",
"# Bearer 授权时应配置为[tag:token],tag表示这个token的归属,可为空(冒号不能省略)。",
"# API_AUTH_USERS=A公司:123,B公司:456,:789",
"# 数据库默认链接地址",
"DB_URL=jdbc:mysql://localhost:3306/db?useSSL=false&serverTimezone=UTC&charset=utf8mb4",
"# 数据库用户名",
"DB_USER=root",
"# 数据库密码",
"DB_PWD=",
"# 生成API的数据表名称,为空则所有的表都生成API,多个使用英文逗号分割",
"INCLUDE_TABLES=",
"# 需要忽略的数据表名称,如果不为空,则指定的表名被过滤,多个使用英文逗号分割",
"IGNORED_TABLES=",
"# 是否启用静态网站,启用后,则创建web目录,放入静态资源即可访问",
"STATIC_WEB_ENABLED=true",
"# 是否开启扩展API,允许用户使用JS代码使用自定义SQL查询数据库",
"EXT_API_ENABLED=true",
"# 是否开启表结构API,默认为false",
"SCHEMA_API_ENABLED=false",
"# 是否开启GET类API缓存,默认为true。对表的CREATE,UPDATE,DELETE操作都会使缓存失效",
"GET_API_CACHE=true",
"# GET类API缓存时间,默认30s",
"GET_API_CACHE_TIMEOUT=30000",
),
DEFAULT_SETTING_FILE_LINES,
mSettingFile,
CharsetUtil.CHARSET_UTF_8
)
Expand Down

0 comments on commit 01bb39c

Please sign in to comment.