fasthttp

package module
v0.3.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 16, 2025 License: MIT Imports: 3 Imported by: 2 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

August 2021 | v1.0

Bofry/host-fasthttp 快速入門指南


目錄

簡介

⠿ Bofry/host-fasthttp 是使用 valyala/fasthttp 作為主體,專門提供 golang 開發者快速建置 WebAPI 應用服務為目的而製作的框架。主要特色如下:

  1. 非全功能的框架,主要目的是快速開發 WebAPI 服務,且目前並沒有計畫增加 WebServer 功能的相關設計,但仍可以參考 valyala/fasthttp 的方式來開發 WebServer 的應用程式。
  2. 框架遵循 RMR (Reource-Method-Representation) 語意來作為應用程式開發配置的原則。
  3. 應用程式使用自訂 HTTP method 來組織應用程式的請求呼叫,比如:PEEK、FETCH、COMMIT…等,而非傳統的 GET、POST、DELETE…等。
  4. Bofry/host-fasthttp 所支援的內建功能:
    1. 自動配置 Route Table。
    2. 通用的應用程式設定(config)處理;支援解析環境變數、JSON、YAML 以及命令列參數。部份類型支援設定值驗證。
    3. 通用的 HTTP querystring、body 等輸入的解析與驗證。
    4. 額外提供處理請求非標準 HTTP method 的相容性標頭。
    5. 提供額外的 Error Response Handler,處理失敗的 HTTP 請求回應。
    6. 提供額外的 Logging 支援。
    7. 內建安全停止(Graceful Shutdown)機制。
    8. 框架使用 DI 機制來減低耦合與簡化操作複雜度。

🔝回目錄


環境要求

  1. GO 1.14 以上

    下載及安裝指引:https://golang.org/doc/install

  2. Git

  3. 將 GOPATH 的 bin 目錄加入你的 system PATH 變數中

    • Linux 系統於 ~/.bashrc 中加入

      export PATH=<your_gopath_bin_dir>:$PATH
      
    • Mac 系統於 ~/.bash_profile 中加入

      export PATH=<your_gopath_bin_dir>:$PATH
      
    • Windows 系統於系統環境變數中

      1. 按 Windows Key + R 開啟執行對話框
      2. 輸入 control sysdm.cpl,,3
      3. 按 環境變數(N) 按鈕 或 Alt + N
      4. 在 系統變數(S) 方塊中,修改 Path 變數,加入 GOPATH 的 bin 目錄

    ⠿ 取得 GOPATH 的 bin 完整路徑,依下列操作步驟:

    使用 go env GOPATH 查看 GOPATH 後,在後面加上 /bin 即是 GOPATH 的 bin 目錄,例:

    $ go env GOPATH
    /Users/go
    

    因此推得 GOPATH bin 目錄的完整路徑為 /User/go/bin

  4. 安裝所需的 go tool

    • Go 1.15 含以前使用下面指令:
    go get -v github.com/Bofry/go-tools/rungo
    go get -v github.com/Bofry/go-tools/host-fasthttp
    go get -v github.com/Bofry/go-tools/gen-host-fasthttp-resource
    go get -v github.com/joho/godotenv/cmd/godotenv
    
    • Go 1.16 含以後使用下面指令:
    go install github.com/Bofry/go-tools/rungo
    go install github.com/Bofry/go-tools/host-fasthttp
    go install github.com/Bofry/go-tools/gen-host-fasthttp-resource
    go install github.com/joho/godotenv/cmd/godotenv
    
    • Go 1.16 含以後使用下面指令:
    go install github.com/Bofry/go-tools/rungo@latest
    go install github.com/Bofry/go-tools/host-fasthttp@latest
    go install github.com/Bofry/go-tools/gen-host-fasthttp-resource@latest
    go install github.com/joho/godotenv/cmd/godotenv@latest
    

    🐾 see Deprecation of 'go get' for installing executables 🔗

🔝回目錄


快速入門

⠿ 本節提供快速建構 myapp 應用程式的基本步驟。

  • 步驟一: 決定你的專案資料夾

    ⠿ 建立名稱為 myapp 的專案。

    mkdir myapp
    
    cd myapp
    
  • 步驟二: 初始化你的專案

    1. 使用 go mod init 初始化 golang 專案。

      go mod init apiservice
      
    2. 使用 host-fasthttp init 進行專案的基本配置。

      host-fasthttp init
      

      💬 host-fasthttp init 會產生 .env、app.go、config.yaml、.conf/、internal/appContext.go……等專案檔,並進行 http server 最基本的配置。

  • 步驟三: 加入預設的 http request handler

    1. 使用編輯器打開 app.go 檔。

    2. 找到第11行,

      type ResourceManager struct {}
      

      更改為:

      type ResourceManager struct {
        *DefaultResource `url:"/"`
      }
      

      💬 若你使用 IDE,比如 vscode 則可能會標示語法錯誤,請先忽略。

    3. 執行 go generate 來配置 http request handler 檔案。

      go generate
      

      💬 這個動作為幫你產生 defaultResouce.go 的檔案,並會放在專案內的 /resource 目錄中。

      💬 若使用 IDE 且支援 go generate 指令的話,可以在程式碼上方直接按下 run go generate 的按鈕,也會有相同的效果。

  • 步驟四: 啟動專案

    rungo
    

    💬 rungo 是一個結合 godotenv 與 go run 命令的工具。

    💬 使用 Ctrl + C 來關閉服務。

  • 步驟五: 檢查

    curl -XPING -sv http://127.0.0.1:10074/
    

    將會得到以下的輸出:

    PONG
    *   Trying 127.0.0.1:10074...
    * Connected to 127.0.0.1 (127.0.0.1) port 10074 (#0)
    > PING / HTTP/1.1
    > Host: 127.0.0.1:10074
    > User-Agent: curl/7.71.1
    > Accept: */*
    >
    * Mark bundle as not supporting multiuse
    < HTTP/1.1 200 OK
    < Server: WebAPI
    < Date: Mon, 23 Aug 2021 07:42:43 GMT
    < Content-Type: text/plain
    < Content-Length: 4
    < Connection: close
    <
    { [4 bytes data]
    * Closing connection 0
    

相關資源

  1. 有關 go mod 的名稱規範詳見 go mod init 的官方說明連結。

🔝回目錄

Documentation

Index

Constants

View Source
const (
	StatusContinue           = 100 // RFC 7231, 6.2.1
	StatusSwitchingProtocols = 101 // RFC 7231, 6.2.2
	StatusProcessing         = 102 // RFC 2518, 10.1
	StatusEarlyHints         = 103 // RFC 8297

	StatusOK                   = 200 // RFC 7231, 6.3.1
	StatusCreated              = 201 // RFC 7231, 6.3.2
	StatusAccepted             = 202 // RFC 7231, 6.3.3
	StatusNonAuthoritativeInfo = 203 // RFC 7231, 6.3.4
	StatusNoContent            = 204 // RFC 7231, 6.3.5
	StatusResetContent         = 205 // RFC 7231, 6.3.6
	StatusPartialContent       = 206 // RFC 7233, 4.1
	StatusMultiStatus          = 207 // RFC 4918, 11.1
	StatusAlreadyReported      = 208 // RFC 5842, 7.1
	StatusIMUsed               = 226 // RFC 3229, 10.4.1

	StatusMultipleChoices  = 300 // RFC 7231, 6.4.1
	StatusMovedPermanently = 301 // RFC 7231, 6.4.2
	StatusFound            = 302 // RFC 7231, 6.4.3
	StatusSeeOther         = 303 // RFC 7231, 6.4.4
	StatusNotModified      = 304 // RFC 7232, 4.1
	StatusUseProxy         = 305 // RFC 7231, 6.4.5

	StatusTemporaryRedirect = 307 // RFC 7231, 6.4.7
	StatusPermanentRedirect = 308 // RFC 7538, 3

	StatusBadRequest                   = 400 // RFC 7231, 6.5.1
	StatusUnauthorized                 = 401 // RFC 7235, 3.1
	StatusPaymentRequired              = 402 // RFC 7231, 6.5.2
	StatusForbidden                    = 403 // RFC 7231, 6.5.3
	StatusNotFound                     = 404 // RFC 7231, 6.5.4
	StatusMethodNotAllowed             = 405 // RFC 7231, 6.5.5
	StatusNotAcceptable                = 406 // RFC 7231, 6.5.6
	StatusProxyAuthRequired            = 407 // RFC 7235, 3.2
	StatusRequestTimeout               = 408 // RFC 7231, 6.5.7
	StatusConflict                     = 409 // RFC 7231, 6.5.8
	StatusGone                         = 410 // RFC 7231, 6.5.9
	StatusLengthRequired               = 411 // RFC 7231, 6.5.10
	StatusPreconditionFailed           = 412 // RFC 7232, 4.2
	StatusRequestEntityTooLarge        = 413 // RFC 7231, 6.5.11
	StatusRequestURITooLong            = 414 // RFC 7231, 6.5.12
	StatusUnsupportedMediaType         = 415 // RFC 7231, 6.5.13
	StatusRequestedRangeNotSatisfiable = 416 // RFC 7233, 4.4
	StatusExpectationFailed            = 417 // RFC 7231, 6.5.14
	StatusTeapot                       = 418 // RFC 7168, 2.3.3
	StatusMisdirectedRequest           = 421 // RFC 7540, 9.1.2
	StatusUnprocessableEntity          = 422 // RFC 4918, 11.2
	StatusLocked                       = 423 // RFC 4918, 11.3
	StatusFailedDependency             = 424 // RFC 4918, 11.4
	StatusUpgradeRequired              = 426 // RFC 7231, 6.5.15
	StatusPreconditionRequired         = 428 // RFC 6585, 3
	StatusTooManyRequests              = 429 // RFC 6585, 4
	StatusRequestHeaderFieldsTooLarge  = 431 // RFC 6585, 5
	StatusUnavailableForLegalReasons   = 451 // RFC 7725, 3

	StatusInternalServerError           = 500 // RFC 7231, 6.6.1
	StatusNotImplemented                = 501 // RFC 7231, 6.6.2
	StatusBadGateway                    = 502 // RFC 7231, 6.6.3
	StatusServiceUnavailable            = 503 // RFC 7231, 6.6.4
	StatusGatewayTimeout                = 504 // RFC 7231, 6.6.5
	StatusHTTPVersionNotSupported       = 505 // RFC 7231, 6.6.6
	StatusVariantAlsoNegotiates         = 506 // RFC 2295, 8.1
	StatusInsufficientStorage           = 507 // RFC 4918, 11.5
	StatusLoopDetected                  = 508 // RFC 5842, 7.2
	StatusNotExtended                   = 510 // RFC 2774, 7
	StatusNetworkAuthenticationRequired = 511 // RFC 6585, 6
)

HTTP status codes were stolen from net/http.

Variables

This section is empty.

Functions

func Startup

func Startup(app interface{}) *host.Starter

func UseCorsHeader added in v0.3.3

func UseCorsHeader() host.Middleware

func UseErrorHandler

func UseErrorHandler(handler ErrorHandler) host.Middleware

func UseLogging

func UseLogging(services ...LoggingService) host.Middleware

func UseRequestManager

func UseRequestManager(requestManager interface{}) host.Middleware

func UseRewriter

func UseRewriter(handler RewriteHandler) host.Middleware

func UseTracing

func UseTracing(enabled bool) host.Middleware

func UseUnhandledRequestHandler

func UseUnhandledRequestHandler(handler RequestHandler) host.Middleware

func UseXHttpMethodHeader

func UseXHttpMethodHeader(headers ...string) host.Middleware

Types

type ErrorHandler

type ErrorHandler = internal.ErrorHandler

function

type EventEvidence

type EventEvidence = middleware.EventEvidence

interface

type EventLog

type EventLog = middleware.EventLog

interface

type Host

type Host = internal.FasthttpHost

struct

type LoggingService

type LoggingService = middleware.LoggingService

interface

type RequestCtx

type RequestCtx = internal.RequestCtx

import

type RequestHandler

type RequestHandler = internal.RequestHandler

import

type RewriteHandler

type RewriteHandler = internal.RewriteHandler

function

type RoutePath

type RoutePath = internal.RoutePath

struct

type Server

type Server = internal.Server

import

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址