{"id":16903816,"url":"https://github.com/looly/loserver","last_synced_at":"2025-04-05T23:05:16.929Z","repository":{"id":26363284,"uuid":"29812424","full_name":"looly/loServer","owner":"looly","description":"基于Netty的Http应用服务器","archived":false,"fork":false,"pushed_at":"2024-05-17T03:58:33.000Z","size":83,"stargazers_count":312,"open_issues_count":0,"forks_count":109,"subscribers_count":29,"default_branch":"master","last_synced_at":"2024-10-14T18:28:20.962Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","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/looly.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":"2015-01-25T11:53:41.000Z","updated_at":"2024-10-14T07:37:55.000Z","dependencies_parsed_at":"2024-10-25T18:43:18.053Z","dependency_job_id":"86bd9bda-6117-40c8-b6c6-a49fd108c35d","html_url":"https://github.com/looly/loServer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/looly%2FloServer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/looly%2FloServer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/looly%2FloServer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/looly%2FloServer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/looly","download_url":"https://codeload.github.com/looly/loServer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247411226,"owners_count":20934653,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":[],"created_at":"2024-10-13T18:28:52.144Z","updated_at":"2025-04-05T23:05:16.899Z","avatar_url":"https://github.com/looly.png","language":"Java","readme":"## loServer\n\n基于Netty的Http应用服务器\n\n### 介绍\n在之前公司的时候有一些小任务是这样的：写一个小Http接口处理一些任务（这个任务常常只需要几行代码）。然后我就开始写个简单的Servlet，挂在Tomcat上。随着时间的推移，Tomcat上的这种小web项目越来越多，最后要更新一个项目就要重新启动Tomcat（没有设置热启动），非常笨重。领导意思是不要写啥都挂在Tomcat上，最好写的这个功能可以独立运行提供服务。于是需求就来了，找个嵌入式的Servlet容器（记得当时年纪小，摆脱不了Servlet容器了……），貌似满足要求的就是Jetty了，也没折腾，后来就不了了之了。\n\n现在想想面对一些压力并不大的请求，可以自己实现一个Http应用服务器来处理简单的Get和Post请求（就是请求文本，响应的也是文本或json），了解了下Http协议，发现解析起来写的东西很多，而且自己写性能也是大问题（性能这个问题为未来考虑），于是考虑用Netty，发现它有Http相关的实现，便按照Example的指示，自己实现了一个应用服务器。思想很简单，在ServerHandler中拦截请求，把Netty的Request对象转换成我自己实现的Request对象，经过用户的Action对象后，生成自己的Response，最后转换为Netty的Response返回给用户。\n\n这个项目中使用到的Netty版本是4.X，毕竟这个版本比较稳定，而且最近也在不停更新，于是采用了。之后如果有时间，会在项目中添加更多功能，希望它最终成为一个完善的高性能的Http服务器。\n\n### 使用方法\n1. 新建一个类实现Action接口，例如我新建了一个ExampleAction\n2. 调用ServerSetting.addAction(\"/example\", ExampleAction.class);增加请求路径和Action的映射关系\n3. ServerSetting.setPort(8090);设置监听端口\n4. LoServer.start();启动服务\n5. 在浏览器中访问http://localhost:8090/example既可\n\n### 代码\n\n\tpackage com.xiaoleilu.loServer.example;\n\n\timport com.xiaoleilu.loServer.LoServer;\n\timport com.xiaoleilu.loServer.Request;\n\timport com.xiaoleilu.loServer.Response;\n\timport com.xiaoleilu.loServer.ServerSetting;\n\n\t/**\n\t * loServer样例程序\u003cbr\u003e\n\t * Action对象用于处理业务流程，类似于Servlet对象\u003cbr\u003e\n\t * 在启动服务器前必须将path和此Action加入到ServerSetting的ActionMap中\u003cbr\u003e\n\t * 使用ServerSetting.setPort方法设置监听端口，此处设置为8090（如果不设置则使用默认的8090端口）\n\t * 然后调用LoServer.start()启动服务\u003cbr\u003e\n\t * 在浏览器中访问http://localhost:8090/example?a=b既可在页面上显示response a: b\n\t * @author Looly\n\t *\n\t */\n\tpublic class ExampleAction implements Action{\n\n\t\t@Override\n\t\tpublic void doAction(Request request, Response response) {\n\t\t\tString a = request.getParam(\"a\");\n\t\t\tresponse.setContent(\"response a: \" + a);\n\t\t}\n\n\t\tpublic static void main(String[] args) {\n\t\t\tServerSetting.addAction(\"/example\", ExampleAction.class);\n\t\t\tServerSetting.setPort(8090);\n\t\t\tLoServer.start();\n\t\t}\n\t}\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flooly%2Floserver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flooly%2Floserver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flooly%2Floserver/lists"}