{"id":15138498,"url":"https://github.com/applesack/vertlin","last_synced_at":"2026-02-08T23:01:56.362Z","repository":{"id":134419183,"uuid":"523631754","full_name":"applesack/vertlin","owner":"applesack","description":"基于Vert.x开发的webdav服务端, 以及一个用于开发web项目的框架","archived":false,"fork":false,"pushed_at":"2022-08-21T01:13:59.000Z","size":290,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-31T14:27:51.266Z","etag":null,"topics":["coroutine","kotlin","vertx","webdav-server"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/applesack.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-08-11T07:47:24.000Z","updated_at":"2024-01-05T19:17:25.000Z","dependencies_parsed_at":null,"dependency_job_id":"372f5b0f-836c-49c9-8b94-5dd003534e88","html_url":"https://github.com/applesack/vertlin","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/applesack/vertlin","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/applesack%2Fvertlin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/applesack%2Fvertlin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/applesack%2Fvertlin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/applesack%2Fvertlin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/applesack","download_url":"https://codeload.github.com/applesack/vertlin/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/applesack%2Fvertlin/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29248487,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-08T22:49:53.206Z","status":"ssl_error","status_checked_at":"2026-02-08T22:49:51.384Z","response_time":57,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["coroutine","kotlin","vertx","webdav-server"],"created_at":"2024-09-26T07:40:20.891Z","updated_at":"2026-02-08T23:01:56.312Z","avatar_url":"https://github.com/applesack.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"## vertlin\n\n用于快速开发`vert.x`应用的脚手架, `springboot like`\n\n快速启动, 线程模型,`profile`, `eventbus`, 自动注入, 定时任务, 本地缓存\n\n\n\n## 线程模型\n\n事件循环单线程服务器有很高效的执行效率, 而且有效地解决了传统多线程下对象并发访问的问题. `vertx`在此基础上引入了上下文的概念, 通常情况下一个线程(`EventLoop`)对应了一个上下文\n\n- `HttpRouter`: 当有请求访问指定的路径时, 回调被触发\n- `EventbusConsumer`: 事件总线的接收者, 路径上有消息时回调被触发\n- `Crontab`: 定时任务, 每个任务可以动态地调整下一次调用的时机, 当满足调用条件则任务被执行\n\n考虑以上几种情况, 一个应用由若干个回调组成, 如果这样的回调很多, 且都部署在同一个上下文中时, 会使得请求的响应速度变慢, 为了提高执行效率需要把这些回调合理地部署在多个上下中.  所以这个脚手架的主要做的工作是更方便的将回调部署到特定的上下文, 且更流畅地跨上下文调用接口. \n\n在本框架中, 能提供功能的回调称为服务(`Service`), 一个服务只能存在于一个上下文中, 而创建上下文在`vertx`需要使用`verticle`. 如果你需要开发一个文件管理系统, 它有以下结构:\n\n- `MainVerticle`: 用于部署其他`verticle`, 读取配置, 管理状态, 管理用户\n- `ServerVerticle`: 用于处理`HTTP`请求并响应, 处理权限\n- `FileServiceVerticle`:  提供文件服务\n- `Worker`: 耗时任务, 例如请求体的序列化和反序列化\n\n你可能会这样写代码:\n\n```kotlin\nclass MainVerticle : CoroutineVerticle() {\n    override suspend fun start() {\n        // 在这里初始化或注册一些服务\n    }\n}\n\nvertx.deployVerticle(MainVerticle())\n\t.compose { vertx.deployVerticle(ServerVerticle()) }\n\t.compose { vertx.deployVerticle(FileServiceVerticle()) }\n```\n\n\n\n在本框架中, 没有`verticle`, 只有服务. 每个服务都是一个类, 而类上的注解会指定了类中的代码会在哪个上下文中执行, 而且可以使用统一的接口让一个服务在其他服务之间共享(类似于`springboot`的`autowire`)\n\n\n\u003e 得益于`kotlin`协程, 可以用同步的写法去写异步代码, 脚手架(框架)大量使用了协程\n\n\n\n\n## 从HelloWorld开始\n\n一个最简单的demo, 用于演示编写一个简单的`http`服务器, 对于任意请求都返回`hello world`\n\n引入依赖(`gradle`)\n\n`implementation \"xyz.scootaloo:vertlin-boot:0.1\" // 核心组件`\n\n`implementation \"xyz.scootaloo:vertlin-web:0.1\" // web支持`\n\n这行划掉, 短期内不考虑发布到中央仓库\n\n1. 编写启动类\n```kotlin\nimport xyz.scootaloo.vertlin.boot.BootManifest\n\nobject Launcher : BootManifest() {\n\t@JvmStatic\n\tfun main(args: Array\u003cString\u003e) {\n\t\trunApplication(args)\n\t}\n}\n```\n启动类继承于`BootManifest`, 这个类有一些默认方法, 可以帮助启动器确定要加载哪些资源, 默认情况下(不重写任何方法), 会递归扫描当前包下所有的class文件, 找到目标实现类并作为资源载入, 可以重写这个类的方法更改加载资源的逻辑.\n\n2. 编写路由器\n```kotlin\nimport io.vertx.ext.web.Router\nimport xyz.scootaloo.vertlin.web.HttpRouterRegister\n\n// 处理所有以\"/\"开头的路径\nclass HelloHttpRouter : HttpRouterRegister(\"/*\") {\n\toverride fun register(router: Router) = router.run {\n        // 处理使用get访问以\"/\"开头的请求\n\t\tget(\"/:name\") { // 这里可以使用协程\n\t\t\tval name = it.pathParam(\"name\")\n            it.end(\"hello $name\")\n\t\t}\n\t}\n}\n```\n这样一个路由器就写完了. 执行`Launcher`的main方法, 服务器启动, 回到浏览器输入`http://localhost:8080`可以看到输出的`hello\n\n\n## 创建HTTP服务器\n\n在`hello world`示例中编写了基本的`http`服务器, 这里是更详细地介绍一些其他功能\n\n1. 监听指定方法访问路径的请求\n\n```kotlin\n// HttpRouterRegister 实现类中\n// 提供了常用的方法get post head options delete trace connect put patch\nget(\"/test/:name\") { ctx -\u003e\n    val name = ctx.pathParam(\"name\")\n    delay(2000) // 在这里暂停2秒, 模拟一个异步调用\n    ctx.end(name) // 2秒后返回捕获到的路径参数\n}\n\npost(\"/test\") {\n    it.end(\"hello world\")\n}\n\nput(\"/test\") {\n    it.end(\"hello world\")\n}\n\n// 如果一个请求监听任意路径的get请求, 可以这样写\nget {\n    it.end(\"hello world\")\n}\n\n// 路径可以是正则表达式, 参考vertx文档\n```\n\n2. 编写过滤器/拦截器\n\n```kotlin\n// 实现一个简单的权限拦截器\n@Order(Ordered.HIGHEST) // 指定优先级, 这个路由可以优先被装配\nclass AuthInterceptor : HttpRouterRegister(\"/*\") {\n\n    override fun register(router: Router) = router.run {\n        any { // 任意路径的请求都会经过这个拦截器\n            val auth = it.request().getHeader(\"Authentication\")\n            val result = handle(ctx, auth).await() // 异步校验权限\n            if (result) {\n                // 如果检查通过则放行, 也可以在上下文中存放用户信息\n                it.next()\n            } else {\n                it.fail(401) // 返回一个错误状态码\n            }\n        }\n    }\n\n}\n```\n\n3. 修改服务器的参数\n\n服务器默认监听端口8080, 如果要修改这个配置, 在`resources`目录下创建一个`config.toml`文件, 文件内容\n\n```toml\n[http]\nport = 9090\nprefix = \"/test\" // 指定应用前缀\n```\n\n这样再次启动, 端口就监听到9090了\n\n框架为`http`服务器准备了一些默认参数, 完整配置如下\n\n```\n[http]\nport = 8080\nbodyLimit = -1\ndeleteUploadedFilesOnEnd = true\nuploadsDirectory = \"uploads\"\n\n[websocket]\nmaxWebSocketFrameSize = 1024\nmaxWebSocketMessageSize = 4918\n```\n\n\n\n## WebSocket\n\ntodo\n\n\n\n## 使用配置\n\n默认配置文件格式为`toml`, 在`resources`目录下创建文件`config.toml`, 内容为:\n\n```toml\n[hello]\nname = \"abc\"\nage = 1990\n```\n\n然后写一个配置类\n\n```kotlin\nimport xyz.scootaloo.vertlin.boot.config.Config\nimport xyz.scootaloo.vertlin.boot.config.Prefix\n\n@Prefix(\"hello\")\ndata class HelloConfig(\n    val name: String,\n    val age: Int\n) : Config\n```\n\n使用时用`inject`操作符将配置类注入, 可以拿到`HelloConfig`的实例, 对象属性即配置文件中的内容\n\n```kotlin\nobject Hello {\n    val helloConfig by inject(Hello::class)\n}\n```\n\n### 使用`Profile`\n\n假如你有多个配置文件, 你可以决定什么时候使用什么配置文件\n\n现在有3个配置文件\n\n```toml\n# 在config.toml中\n[profiles]\nactive = \"dev\"\n\n[hello]\nage = 17\n\n# 在config-dev.toml中\n[hello]\nname = \"dev\"\n\n# 在config-prod.toml中\n[hello]\nname = \"prod\"\n```\n\n由于在默认配置中指定了`dev`作为配置文件, 所以此时访问配置项`hello.name`时输出`prod`,在`dev`和`prod`配置中没有指定`hello.age`配置项, 但是仍然输出`17`, 因为所有附加配置文件都继承于默认配置, 所以你可以在`config.toml`中存放公共的配置项\n\n另外可以通过命令行注入配置, 比如使用如下命令运行一个`jar`时, `prod` 被启用\n\n`java -jar my-server.jar --profiles.avtive=prod`\n\n\n\n## 自动注入\n\ntodo\n\n\n\n\n\n## Eventbus\n\ntodo\n\n\n\n## 本地缓存\n\ntodo\n\n\n\n## 高级主题\n\n### 服务与服务清单\n\ntodo\n\n\n\n### 服务解析器\n\ntodo\n\n\n\n### 服务的生命周期\n\ntodo\n\n\n\n### 命令行\n\ntodo\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapplesack%2Fvertlin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fapplesack%2Fvertlin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapplesack%2Fvertlin/lists"}