Documentation
¶
Index ¶
- Constants
- Variables
- func Bind[S any](r *http.Request, s *S) error
- func Get(path string, hr Handler, middleware ...Middleware)
- func IoReaderToString(ir io.Reader) string
- func Post(path string, hr Handler, middleware ...Middleware)
- func SetCommonAfterMiddleware(m ...Middleware)
- func SetCommonMiddleware(m ...Middleware)
- func SetInternalServerErrorResponse(contentType string, data []byte)
- func SetLogger(lg Logger)
- func SetNoMethodResponse(contentType string, data []byte)
- func SetResponse(w http.ResponseWriter, r *http.Request, contentType string, statusCode int, ...)
- func SetResponseAsJson(w http.ResponseWriter, r *http.Request, statusCode int, data any)
- func Shutdown()
- func StartServer(c context.Context, host string, port int)
- type ErrBind
- type ErrRequestFieldFormat
- type ErrRequestFormParse
- type ErrRequestJsonSomethingInvalid
- type ErrRequestJsonSyntaxError
- type Handler
- type Logger
- type Middleware
Constants ¶
const ( ContentTypeJSON = "application/json" ContentTypeXML = "application/xml" ContentTypePlainText = "text/plain" ContentTypeHTML = "text/html" ContentTypeFormURLEnc = "application/x-www-form-urlencoded" ContentTypeMultipart = "multipart/form-data" ContentTypeHTMLWithCharset = "text/html; charset=utf-8" )
Variables ¶
var (
PanicSameRoot = "there already route %s exists"
)
var ( // Graceful shutdown時にタイムアウトとして設定する秒数 ShutdownTimeoutSecond = 8 * time.Second )
設定情報 これらはサーバー起動前に設定されている想定
本パッケージはnet/httpパッケージのラッパーパッケージであり、 内部で実行されるhttp.Server.ListenAndServeはリクエストが来るたびに、 スレッド(Goroutine)が立ち上がる。 このパッケージから立ち上げるListenAndServeは1つであり、 複数のスレッドでListenAndServeを立ち上げることは想定していない。 そのため、各変数もスレッドセーフとはなっていない。
Functions ¶
func Bind ¶
リクエストデータを構造体へBindする。 構造体以外が指定された場合はpanicとなる。 構造体のタグには、"json", "query", "param", "form"を指定可能。 タグがないフィールドが存在する場合はpanicとなる。 "multipart/form-data"はサポートしていない。
本関数は値のバインドのみを行い、必須フィールドのチェックは含まれない。 対象のフィールドが含まれない場合は何もセットしない。 その場合は構造体はデフォルト値のままになる。
func Get ¶
func Get(path string, hr Handler, middleware ...Middleware)
GETメソッドのハンドラの設定 既に存在するパスかつメソッドを設定するとpanicになる。 ミドルウェアは先頭から順に実行されていく。
func IoReaderToString ¶
func Post ¶
func Post(path string, hr Handler, middleware ...Middleware)
POSTメソッドのハンドラの設定 既に存在するパスかつメソッドを設定するとpanicになる。 ミドルウェアは先頭から順に実行されていく。
func SetCommonAfterMiddleware ¶
func SetCommonAfterMiddleware(m ...Middleware)
共通の後続ミドルウェア 個別のミドルウェアの後に実行されるミドルウェアを登録する 共通のミドルウェア -> 個々のミドルウェア -> 共通の後続ミドルウェア -> ルーティング処理 -> ハンドラ処理 先頭から順に実行されていく このミドルウェアはルーティング処理の後に動作するため、ルートが確定する前に処理が終了した場合は実行されない。 例えば、no mothodの場合は実行されない。
func SetCommonMiddleware ¶
func SetCommonMiddleware(m ...Middleware)
共通のミドルウェア すべてのハンドラの前に実行されるミドルウェアで、先頭から順に実行されていく このミドルウェアはルーティング処理の前に動作する。 共通のミドルウェア -> 個々のミドルウェア -> 共通の後続ミドルウェア -> ルーティング処理 -> ハンドラ処理
func SetInternalServerErrorResponse ¶
500エラーの場合のレスポンスを設定する デフォルトはapplication/jsonで空のjson
func SetNoMethodResponse ¶
ルートが見つからない場合のレスポンスを設定する デフォルトはapplication/jsonで空のjson
func SetResponse ¶
func SetResponseAsJson ¶
"application/json"としてレスポンスを返す dataはjson.Marshalで変換を行ってレスポンスへセットする。 json.Marshalで変換に失敗した場合はpanicとなる。
Types ¶
type ErrRequestFieldFormat ¶
func (*ErrRequestFieldFormat) Error ¶
func (e *ErrRequestFieldFormat) Error() string
func (*ErrRequestFieldFormat) Unwrap ¶
func (e *ErrRequestFieldFormat) Unwrap() error
type ErrRequestFormParse ¶
type ErrRequestFormParse struct {
Err error
}
func (*ErrRequestFormParse) Error ¶
func (e *ErrRequestFormParse) Error() string
func (*ErrRequestFormParse) Unwrap ¶
func (e *ErrRequestFormParse) Unwrap() error
type ErrRequestJsonSomethingInvalid ¶
func (*ErrRequestJsonSomethingInvalid) Error ¶
func (e *ErrRequestJsonSomethingInvalid) Error() string
func (*ErrRequestJsonSomethingInvalid) Unwrap ¶
func (e *ErrRequestJsonSomethingInvalid) Unwrap() error
type ErrRequestJsonSyntaxError ¶
func (*ErrRequestJsonSyntaxError) Error ¶
func (e *ErrRequestJsonSyntaxError) Error() string
func (*ErrRequestJsonSyntaxError) Unwrap ¶
func (e *ErrRequestJsonSyntaxError) Unwrap() error