dotweb框架之旅 [三] - 經常使用對象-HttpServer

dotweb屬於一個Web框架,但願經過框架行爲,幫助開發人員快速構建Web應用,提高開發效率,減小沒必要要的代碼臃腫。git

框架地址:https://github.com/devfeel/dotwebgithub

dotweb包含如下幾個經常使用對象:web

  • App(dotweb) App容器,爲Web請求處理提供必要的容器類功能。
  • HttpServer 用於真正處理Web請求的服務模塊。
  • HttpContext 用於提供單次請求處理中請求信息與響應信息的快捷處理與惟一入口。
  • Response 用於從服務器向用戶發送輸出的結果。
  • Request 用於從用戶那裏取得信息。
  • Session 用於存儲關於某個鏈接會話的信息,或者修改相關的設置。目前支持存儲本機內存與Redis分佈式。

本章主要對HttpServer對象展開介紹。服務器

HttpServer對象,在框架中,主要承擔用於真正處理Web請求的服務模塊,好比處理Web監聽、設置路由、設置Module、管理Session、設置Render、設置Binder等,同時也承擔一系列特性設置。session

主要屬性:app

屬性 描述 默認值
Router
當前HttpServer路由,經過Router()訪問
NewRouter(server)
Modules
全局HttpModule,用於路由前的集中處理
make([]*HttpModule, 0)
DotApp
所屬的App容器指針
-
SessionManager
當前HttpServer的Session管理單元
InitSessionManager()
Binder
請求參數綁定器
newBinder()
Features
當前HttpServer特性集合
Feature{}

 

 

 

 

 

 

 

 

 

 

主要方法框架

方法 描述
ListenAndServe()\ListenAndServeTLS()
 指定host:port,根據配置啓動Web端口監聽
ServeHTTP(w http.ResponseWriter, req *http.Request)
 http請求處理入口
InitSessionManager()
 初始化Session單元
Router()\GET()\POST()....WebSocket()
路由訪問入口及快捷操做入口
Binder()
 提供HttpServer對象綁定器入口
Renderer()
 提供HttpServer對象模板處理入口
SetEnabledAutoHEAD()
 設置是否自動支持Head(默認爲false)
SetEnabledListDir()
 設置是否容許目錄瀏覽(默認爲false)
SetEnabledSession()
 設置是否容許啓用Session(默認爲false)
SetEnabledGzip()
  設置是否容許啓用GZip壓縮(默認爲false)
SetEnabledIgnoreFavicon()
 設置是否忽略Favicon請求(默認爲false)
SetEnabledTLS()
 設置是否支持TLS(默認爲false)
SetEnabledDetailRequestData()
 設置是否啓用詳細請求數據統計(默認爲false)
RegisterModule()
 添加處理模塊

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

常規使用代碼:分佈式

func main() {

    //初始化DotServer
    app := dotweb.New()

    //設置gzip開關
    app.HttpServer.SetEnabledGzip(true)

    //設置Session開關
    app.HttpServer.SetEnabledSession(true)

    //設置Session配置
    //runtime mode
    app.HttpServer.SetSessionConfig(session.NewDefaultRuntimeConfig())

    //設置啓用詳細請求數據統計
    app.HttpServer.SetEnabledDetailRequestData(true)

    //設置啓用GZIP壓縮,默認壓縮級別
    app.HttpServer.SetEnabledGzip(true)

    //設置啓用自動忽略Favicon請求
    app.HttpServer.SetEnabledIgnoreFavicon(true)

    //設置啓用目錄瀏覽
    app.HttpServer.SetEnabledListDir(true)

    //設置路由
    InitRoute(app.HttpServer)
    
    // 開始服務
    port := 8080
    fmt.Println("dotweb.StartServer => " + strconv.Itoa(port))
    err := app.StartServer(port)
    fmt.Println("dotweb.StartServer error => ", err)
}

如代碼所示,經過在啓動Server正式監聽前設置一系列參數,能夠啓用所支持的特性。spa

 

更多代碼可參考 https://github.com/devfeel/dotweb-example指針

歡迎各位加入咱們的go語言QQ羣:193409346

相關文章
相關標籤/搜索