BeetleX之webapi使用入門

        BeetleX是TCP通訊應用組件,在它之上可以擴展任何基於TCP的應用通訊功能。FastHttpApi是組件擴展的一個Http/Https/Websocket服務組件,它提供的功能豐富,包括功能有:靜態文件,動態數據控制器和Websocket等相關功能支持;實際在上還在FastHttpApi基礎擴展了Http/Https/Websocket網關應用組件(更直觀上來說https://beetlex.io網站上的所有服務內容都是基於BeetleX構建)。

        在現有前後分離的Web開發應用模式中,編寫Webapi作爲數據服務通訊交互是比較普遍的。接下來介紹如何使用組件快速地構建一個Webapi服務。

控制器定義

        組件在定義Webapi控制器並沒有什麼特別的要求,只需要根據實際應用情況定義類和相關方法即可。

    [Controller]
    public class Webapi
    {
        public object Hello(string name)
        {
            return $"hello {name}";
        }
    }

以上定義一個對象並帶有Hello方法,只要在類上標記[Controller]即可被組件加載成Webapi服務訪問路徑爲/Hello ;方法可以通過post/get進行訪問,即使是傳統的form post或json post都同時兼容,無須在參數上添加任何描述型的標籤(這也是組件使用方便性的地方)。

啓動服務

        當Webapi控制器編寫完成後就可以寫服務啓動它。

class Program
{
    static void Main(string[] args)
    {
        HttpApiServer server = new HttpApiServer();
        server.Register(typeof(Webapi).Assembly);
        server.Options.Port = 80;
        server.Options.LogLevel = EventArgs.LogType.Info;
        server.Options.LogToConsole = true;
        server.Options.SSL = true;
        server.Options.CertificateFile = "ssl.pfx";
        server.Options.CertificatePassword = "123456";
        server.Open();
        System.Threading.Thread.Sleep(-1);
    }
}

只需要創建一個HttpApiServer對象,通過Register方法把存在控制器的程序集註冊即可,如果有多個程序集則可以傳入多個。接下來的工作就是配置端口和SSL信息;最後通過Open方法啓動對應的Http/Https服務。

通過以上日誌可以查看服務啓動情況。

服務訪問

服務啓動後就可以通過瀏覽器對它進行訪問

 

組件爲了更好配合自己有的js組件庫調用,所以默認返回一個針對性的結構體。在實際應用中可以需要制定自己的返回結構,這個時候可以制定自己的IResult返回對象

public object Hello(string name)
{
    return new JsonResult($"hello {name}");
}
//或者
[DefaultJsonResultFilter]
public object Hello(string name)
{
    return $"hello {name}";
}

以上兩種情況是定義一個默認返回的Json結構體,不附加任何其他信息成員。

 

實際應用中也可以針對自己的需求來制定不同的IResult.

請求上下文

在控制器中有時間不僅僅獲取請求數據,有時候還需要獲取和設置請求頭和Cookie等;這個時候就需要訪問組針對Http信息關聯的詳細信息。組件提供了一個IHttpContext的接口來訪問相關信息,這個對象只要參數中定義即可以由組件自動提供。

public object GetContext(IHttpContext context)
{
    return context.Request.Header.Copy();
}

可以訪問GetContext方法獲取當前請求的頭信息

配置文件

        有很多時候希望通過文件配置來更改監聽的端口和對應的SSL配置信息等;組件默認會讀取當前運行目錄下的HttpConfig.json配置文件,如果目錄下沒有這個文件則是組件內部默認配置。

{
  "HttpConfig": {
    "SameSite": null,
    "ServerTag": "beetlex.io",
    "OutputServerTag": true,
    "IPRpsLimit": 0,
    "IPRpsLimitDisableTime": 1800000,
    "MaxWaitQueue": 1000,
    "BufferPoolSize": 10,
    "BufferPoolGroups": 4,
    "IOQueues": 1,
    "SyncAccept": true,
    "ManageApiEnabled": true,
    "Statistical": true,
    "IOQueueEnabled": false,
    "CacheLogMaxSize": 1000,
    "CacheLogFilter": null,
    "MaxrpsSettings": [],
    "Settings": [],
    "AccessKey": null,
    "AutoGzip": false,
    "StaticResurceCacheTime": 0,
    "BufferPoolMaxMemory": 500,
    "SessionTimeOut": 600,
    "UseIPv6": true,
    "Virtuals": [],
    "PacketCombined": 0,
    "FileManager": false,
    "FileManagerPath": null,
    "LogToConsole": true,
    "NotLoadFolder": "\\Files;\\Images;\\Data",
    "CacheFiles": "html;htm;js;css",
    "CacheFileSize": 500,
    "LogLevel": 16,
    "WebSocketMaxRPS": 30,
    "BufferSize": 8192,
    "NoGzipFiles": "jpg;jpeg;png;gif;png;ico;zip;rar;bmp",
    "MaxConnections": 2000,
    "Manager": null,
    "ManagerPWD": null,
    "WriteLog": false,
    "Host": "",
    "Debug": false,
    "FixedConverter": false,
    "AgentRewrite": true,
    "RewriteIgnoreCase": true,
    "RewriteCachedSize": 500000,
    "Port": 80,
    "SSL": true,
    "SSLPort": 443,
    "CertificateFile": "beetlex.pfx",
    "CertificatePassword": "******",
    "MaxBodyLength": 2097152,
    "OutputStackTrace": true,
    "StaticResurceType": "woff2;js;rar;xml;woff;css;jpg;gif;map;zip;jpeg;svg;txt;ico;ttf;htm;png;html",
    "DefaultPage": "index.html;index.htm",
    "StaticResourcePath": null
  }
}

由於組件涉及到很多方面的配置,如:url重寫,線程隊列,緩衝區和靜態資源支持等等;一般情況下只需要針對以下幾項配置過行調整即可。

{
  "HttpConfig": {
    "SameSite": null,
    "ServerTag": "beetlex.io","Port": 80,
    "SSL": true,
    "SSLPort": 443,
    "CertificateFile": "beetlex.pfx",
    "CertificatePassword": "******",
    "MaxBodyLength": 2097152,
    "OutputStackTrace": true,
    "StaticResurceType": "woff2;js;rar;xml;woff;css;jpg;gif;map;zip;jpeg;svg;txt;ico;ttf;htm;png;html",
    "DefaultPage": "index.html;index.htm"
  }
}

下載示例 

鏈接:https://pan.baidu.com/s/10Ct0jJQfKRnc-jXI4JoGig

提取碼:xywc

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章