{"id":19991946,"url":"https://github.com/beetlex-io/FastHttpApi","last_synced_at":"2025-05-04T11:30:30.571Z","repository":{"id":38178832,"uuid":"148727943","full_name":"beetlex-io/FastHttpApi","owner":"beetlex-io","description":"a lightweight and high-performance http/websocket service component in the dotnet core platform that supports TLS.","archived":false,"fork":false,"pushed_at":"2024-08-05T14:56:24.000Z","size":51040,"stargazers_count":655,"open_issues_count":16,"forks_count":173,"subscribers_count":58,"default_branch":"master","last_synced_at":"2025-04-30T04:36:48.538Z","etag":null,"topics":["dotnet","dotnetcore","fasthttp","fasthttpapi","http","https-server","rpc-api","rpc-service","ssl","webapi","websocket","websockets-server"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/beetlex-io.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","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},"funding":{"open_collective":"beetlex","custom":["https://www.paypal.me/henryfancn","http://beetlex.io"]}},"created_at":"2018-09-14T03:00:26.000Z","updated_at":"2025-04-14T05:42:36.000Z","dependencies_parsed_at":"2024-11-29T23:16:17.038Z","dependency_job_id":null,"html_url":"https://github.com/beetlex-io/FastHttpApi","commit_stats":{"total_commits":261,"total_committers":1,"mean_commits":261.0,"dds":0.0,"last_synced_commit":"cb08994ae14e9be9de1f83fb3d23942c7bbd6214"},"previous_names":["ikende/fasthttpapi"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beetlex-io%2FFastHttpApi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beetlex-io%2FFastHttpApi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beetlex-io%2FFastHttpApi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beetlex-io%2FFastHttpApi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/beetlex-io","download_url":"https://codeload.github.com/beetlex-io/FastHttpApi/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252006740,"owners_count":21679249,"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":["dotnet","dotnetcore","fasthttp","fasthttpapi","http","https-server","rpc-api","rpc-service","ssl","webapi","websocket","websockets-server"],"created_at":"2024-11-13T04:51:58.754Z","updated_at":"2025-05-04T11:30:29.200Z","avatar_url":"https://github.com/beetlex-io.png","language":"C#","readme":"`BeetleX.FastHttpApi` is a lightweight and high-performance HTTP service component in the dotnet core platform that supports WebSocket and SSL.\n\n\u003ca href=\"https://www.nuget.org/packages/BeetleX.FastHttpApi/\" target=\"_blank\"\u003e \n    \u003cimg src=\"https://img.shields.io/nuget/vpre/beetlex.fasthttpapi?label=FastHttpApi\"\u003e \n\t\t\t\t\t\t\t  \u003cimg src=\"https://img.shields.io/nuget/dt/BeetleX.FastHttpApi\"\u003e\n\t\t\t\t\t\t\t  \u003c/a\u003e\n                              \n## samples\n[**[https://github.com/beetlex-io/BeetleX-Samples](https://github.com/beetlex-io/BeetleX-Samples)**]\n\n\n## Web Framework Benchmarks\n[Round 20](https://www.techempower.com/benchmarks/#section=data-r20\u0026hw=ph\u0026test=composite)\n![benchmarks-round20](https://user-images.githubusercontent.com/2564178/107942248-eec41380-6fc5-11eb-94e4-410cadc8ae13.png)\n\n## Using\n\n### Install BeetleX.FastHttpApi\n`Install-Package BeetleX.FastHttpApi`\n\n### Base sample code\n``` csharp\n    [Controller]\n    class Program\n    {\n        private static BeetleX.FastHttpApi.HttpApiServer mApiServer;\n        static void Main(string[] args)\n        {\n            mApiServer = new BeetleX.FastHttpApi.HttpApiServer();\n            mApiServer.Options.LogLevel = BeetleX.EventArgs.LogType.Trace;\n            mApiServer.Options.LogToConsole = true;\n            mApiServer.Debug();//set view path with vs project folder\n            mApiServer.Register(typeof(Program).Assembly);\n            //mApiServer.Options.Port=80; set listen port to 80\n            mApiServer.Open();//default listen port 9090  \n            Console.Write(mApiServer.BaseServer);\n            Console.Read();\n        }\n        // Get /hello?name=henry \n        // or\n        // Get /hello/henry\n        [Get(Route=\"{name}\")]\n        public object Hello(string name)\n        {\n            return $\"hello {name} {DateTime.Now}\";\n        }\n        // Get /GetTime  \n        public object GetTime()\n        {\n            return DateTime.Now;\n        }\n    }\n```\n### Url Map\n``` csharp\nmApiServer.Map(\"/\", (ctx) =\u003e\n{\n    ctx.Result(new TextResult(\"map /\"));\n});\n\nmApiServer.Map(\"/user/{id}\", async (ctx) =\u003e\n{\n    ctx.Result(new TextResult((string)ctx.Data[\"id\"]));\n});\n```\n\n### Url rewrite\n``` csharp\nmApiServer.UrlRewrite.Add(\"/api/PostStream/{code}/{datacode}\", \"/api/PostStream\");\nmApiServer.UrlRewrite.Add(\"/api/PostStream/{code}\", \"/api/PostStream\");\nmApiServer.UrlRewrite.Add(null, \"/gettime\", \"/time\", null);\n```\n\n### Action route\n``` csharp\n[RouteMap(\"/map/{code}\")]\n[RouteMap(\"/map/{code}/{customer}\")]\npublic object Map(string code, string customer)\n{\n    return new { code, customer };\n}\n```\n\n### Hosting and DI services\n`Install-Package BeetleX.FastHttpApi.Hosting`\n``` csharp\n    public class Program\n    {\n        static void Main(string[] args)\n        {\n            HttpServer host = new HttpServer(80);\n            host.UseTLS(\"test.pfx\", \"123456\");\n            host.Setting((service, option) =\u003e\n            {\n                service.AddTransient\u003cUserInfo\u003e();\n                option.LogToConsole = true;\n                option.LogLevel = BeetleX.EventArgs.LogType.Info;\n            });\n            host.Completed(server =\u003e\n            {\n\n            });\n            host.RegisterComponent\u003cProgram\u003e();\n            host.Run();\n        }\n    }\n\n    [Controller]\n    public class Home\n    {\n        public Home(UserInfo user)\n        {\n            mUser = user;\n        }\n\n        public object Hello()\n        {\n            return mUser.Name;\n        }\n\n        private UserInfo mUser;\n    }\n\n    public class UserInfo\n    {\n        public string Name { get; set; } = \"admin\";\n    }\n```\n### Windows service\n``` csharp\nclass Program\n{\n    private static HttpServer mServer;\n\n    static void Main(string[] args)\n    {\n        mServer = new HttpServer(80);\n        mServer.IsWindowsServices = true;\n        mServer.Setting((service, option) =\u003e\n        {\n            option.LogToConsole = true;\n            option.WriteLog = true;\n            option.LogLevel = BeetleX.EventArgs.LogType.Info;\n        });\n        mServer.RegisterComponent\u003cHome\u003e();\n        mServer.Run();\n    }\n}\n[Controller]\npublic class Home\n{\n    public object Hello(string name)\n    {\n        return $\"hello {name}\";\n    }\n}\n```\n### EntityFrameworkCore extensions\n`BeetleX.FastHttpApi.EFCore.Extension `\n``` csharp\nclass Program\n{\n    static void Main(string[] args)\n    {\n        HttpApiServer server = new HttpApiServer();\n        server.AddEFCoreDB\u003cNorthwindEFCoreSqlite.NorthwindContext\u003e();\n        server.Options.Port = 80;\n        server.Options.LogToConsole = true;\n        server.Options.LogLevel = EventArgs.LogType.Info;\n        server.Options.SetDebug();\n        server.Register(typeof(Program).Assembly);\n        server.AddExts(\"woff\");\n        server.Open();\n        Console.Read();\n    }\n}\n[Controller]\npublic class Webapi\n{\n    public DBObjectList\u003cCustomer\u003e Customers(string name, string country, EFCoreDB\u003cNorthwindContext\u003e db)\n    {\n        Select\u003cCustomer\u003e select = new Select\u003cCustomer\u003e();\n        if (!string.IsNullOrEmpty(name))\n            select \u0026= c =\u003e c.CompanyName.StartsWith(name);\n        if (!string.IsNullOrEmpty(country))\n            select \u0026= c =\u003e c.Country == country;\n        select.OrderBy(c =\u003e c.CompanyName.ASC());\n        return (db.DBContext, select);\n    }\n\n    [Transaction]\n    public void DeleteCustomer(string customer, EFCoreDB\u003cNorthwindContext\u003e db)\n    {\n        db.DBContext.Orders.Where(o =\u003e o.CustomerID == customer).Delete();\n        db.DBContext.Customers.Where(c =\u003e c.CustomerID == customer).Delete();\n    }\n\n    public DBValueList\u003cstring\u003e CustomerCountry(EFCoreDB\u003cNorthwindContext\u003e db)\n    {\n        SQL sql = \"select distinct country from customers\";\n        return (db.DBContext, sql);\n    }\n}\n```\n\n\n## Setting https\n- HttpConfig.json\n```\n \"SSL\": true,\n \"CertificateFile\": \"you.com.pfx\",\n \"CertificatePassword\": \"******\",\n```\n- Code\n``` csharp\nmApiServer.ServerConfig.SSL=true;\nmApiServer.ServerConfig.CertificateFile=\"you.com.pfx\";\nmApiServer.ServerConfig.CertificatePassword=\"******\";\n```\n## Defined result\n- Text result\n``` csharp\n    public class TextResult : ResultBase\n    {\n        public TextResult(string text)\n        {\n            Text = text == null ? \"\" : text;\n        }\n        public string Text { get; set; }\n        public override bool HasBody =\u003e true;\n        public override void Write(PipeStream stream, HttpResponse response)\n        {\n            stream.Write(Text);\n        }\n    }\n```\n## Download result\n``` csharp\n        public object DownloadImport(string id)\n        {\n           \n            Dictionary\u003cstring, object\u003e result = new Dictionary\u003cstring, object\u003e();\n            return new DownLoadResult(Newtonsoft.Json.JsonConvert.SerializeObject(result), $\"test.json\");\n        }\n```\n- Use\n``` csharp\n        public object plaintext()\n        {\n            return new TextResult(\"Hello, World!\");\n        }\n```\n## Cookies \n``` csharp\n        public object SetCookie(string name, string value, IHttpContext context)\n        {\n            Console.WriteLine(context.Data);\n            context.Response.SetCookie(name, value);\n            return $\"{DateTime.Now}{name}={value}\";\n        }\n\n        public string GetCookie(string name, IHttpContext context)\n        {\n            Console.WriteLine(context.Data);\n            return $\"{DateTime.Now} {name}= {context.Request.Cookies[name]}\";\n        }\n```\n## Header\n``` csharp\n        public void SetHeader(string token,IHttpContext context)\n        {\n            context.Response.Header[\"Token\"]=token;\n        }\n\n        public string GetHeader(string name, IHttpContext context)\n        {\n            return  context.Request.Header[name];\n        }\n```\n## Data bind\n- Url\n\n`/hello?name=xxx`or`/hello/henry`\n``` csharp\n        [Get(Route = \"{name}\")]\n        public object Hello(string name, IHttpContext context)\n        {\n            return $\"hello {name} {DateTime.Now}\";\n        }\n```\n`/SetValue?id=xxx\u0026value=xxxx`or`/SetValue/xxx-xxx`\n``` csharp\n        [Get(Route = \"{id}-{value}\")]\n        public object SetValue(string id, string value, IHttpContext context)\n        {\n            return $\"{id}={value} {DateTime.Now}\";\n        }\n```\n- Json\n\n`{\"name\":\"xxxx\",\"value\":\"xxx\"}`\n``` csharp\n        [Post]\n        [JsonDataConvert]\n        public object Post(string name, string value, IHttpContext context)\n        {\n            Console.WriteLine(context.Data);\n            return $\"{name}={value}\";\n        }\n```\nor\n``` csharp\n        [Post]\n        [JsonDataConvert]\n        public object Post(Property body, IHttpContext context)\n        {\n            Console.WriteLine(context.Data);\n            return $\"{body.name}={body.value}\";\n        }\n```\n- x-www-form-urlencoded\n\n`name=aaa\u0026value=aaa`\n``` csharp\n        [Post]\n        [FormUrlDataConvert]\n        public object PostForm(string name, string value, IHttpContext context)\n        {\n            Console.WriteLine(context.Data);\n            return $\"{name}={value}\";\n        }\n```\n- multipart/form-data\n``` csharp\n        [Post]\n        [MultiDataConvert]\n        public object UploadFile(string remark, IHttpContext context)\n        {\n            foreach (var file in context.Request.Files)\n                using (System.IO.Stream stream = System.IO.File.Create(file.FileName))\n                {\n                    file.Data.CopyTo(stream);\n                }\n            return $\"{DateTime.Now} {remark} {string.Join(\",\", (from fs in context.Request.Files select fs.FileName).ToArray())}\";\n        }\n```\n- Read stream\n``` csharp\n        [Post]\n        [NoDataConvert]\n        public object PostStream(IHttpContext context)\n        {\n            Console.WriteLine(context.Data);\n            string value = context.Request.Stream.ReadString(context.Request.Length);\n            return value;\n        }\n```\n## Filter\n- Defined filter\n``` csharp\n    public class GlobalFilter : FilterAttribute\n    {\n        public override bool Executing(ActionContext context)\n        {\n            Console.WriteLine(DateTime.Now + \" globalFilter execting...\");\n            return base.Executing(context);\n        }\n        public override void Executed(ActionContext context)\n        {\n            base.Executed(context);\n            Console.WriteLine(DateTime.Now + \" globalFilter executed\");\n        }\n    }\n```\n- Use\n``` csharp\n        [CustomFilter]\n        public string Hello(string name)\n        {\n            return DateTime.Now + \" hello \" + name;\n        }\n```\nor\n``` csharp\n    [Controller]\n    [CustomFilter]\n    public class ControllerTest\n    {\n    \n    }\n```\n- Skip filter\n``` csharp\n        [SkipFilter(typeof(GlobalFilter))]\n        public string Hello(string name)\n        {\n            return DateTime.Now + \" hello \" + name;\n        }\n```\n## Parameters validation\n``` csharp\npublic bool Register(\n      [StringRegion(Min = 5)]string name,\n      [StringRegion(Min = 5)]string pwd,\n      [DateRegion(Min = \"2019-1-1\", Max = \"2019-2-1\")]DateTime dateTime,\n      [EmailFormater]string email,\n      [IPFormater]string ipaddress,\n      [NumberRegion(Min = 18, Max = 56)]int age,\n      [DoubleRegion(Min = 10)]double memory\n                  )\n        {\n           return true;\n        }\n```\n## Async action\n``` csharp\n        [Get(Route = \"{name}\")]\n        public Task\u003cString\u003e Hello(string name)\n        {\n            string result = $\"hello {name} {DateTime.Now}\";\n            return Task.FromResult(result);\n        }\n\n        public async Task\u003cString\u003e Wait()\n        {\n            await Task.Delay(2000);\n            return $\"{DateTime.Now}\";\n        }\n```\n## Cross domain\n``` csharp\n        [Options(AllowOrigin = \"www.ikende.com\")]\n        public string GetTime(IHttpContext context)\n        {\n            return DateTime.Now.ToShortDateString();\n        }\n```\n\n## Websocket\n- Server\n``` csharp\n[Controller]\n    class Program\n    {\n        private static BeetleX.FastHttpApi.HttpApiServer mApiServer;\n        static void Main(string[] args)\n        {\n            mApiServer = new BeetleX.FastHttpApi.HttpApiServer();\n            mApiServer.Debug();\n            mApiServer.Register(typeof(Program).Assembly);\n            mApiServer.Open();\n            Console.Write(mApiServer.BaseServer);\n            Console.Read();\n        }\n        // Get /hello?name=henry \n        // or\n        // Get /hello/henry\n        [Get(R\"{name}\")]\n        public object Hello(string name)\n        {\n            return $\"hello {name} {DateTime.Now}\";\n        }\n        // Get /GetTime  \n        public object GetTime()\n        {\n            return DateTime.Now;\n        }\n    }\n```\n- Hello\n\nRequest json\n``` javascript\n{\n      url: '/Hello', \n      params: { name: 'test' }\n}\n```\n- GetTime\n\nRequest json\n``` javascript\n{\n      url: '/GetTime', \n      params: { }\n}\n```\n\n","funding_links":["https://opencollective.com/beetlex","https://www.paypal.me/henryfancn","http://beetlex.io"],"categories":["C\\#"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeetlex-io%2FFastHttpApi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbeetlex-io%2FFastHttpApi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeetlex-io%2FFastHttpApi/lists"}