server

package
v0.0.0-...-ed573af Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
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

View Source
var (
	PanicSameRoot = "there already route %s exists"
)
View Source
var (

	// Graceful shutdown時にタイムアウトとして設定する秒数
	ShutdownTimeoutSecond = 8 * time.Second
)

設定情報 これらはサーバー起動前に設定されている想定

本パッケージはnet/httpパッケージのラッパーパッケージであり、 内部で実行されるhttp.Server.ListenAndServeはリクエストが来るたびに、 スレッド(Goroutine)が立ち上がる。 このパッケージから立ち上げるListenAndServeは1つであり、 複数のスレッドでListenAndServeを立ち上げることは想定していない。 そのため、各変数もスレッドセーフとはなっていない。

Functions

func Bind

func Bind[S any](r *http.Request, s *S) error

リクエストデータを構造体へ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 IoReaderToString(ir io.Reader) string

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

func SetInternalServerErrorResponse(contentType string, data []byte)

500エラーの場合のレスポンスを設定する デフォルトはapplication/jsonで空のjson

func SetLogger

func SetLogger(lg Logger)

func SetNoMethodResponse

func SetNoMethodResponse(contentType string, data []byte)

ルートが見つからない場合のレスポンスを設定する デフォルトはapplication/jsonで空のjson

func SetResponse

func SetResponse(w http.ResponseWriter, r *http.Request, contentType string, statusCode int, data []byte)

func SetResponseAsJson

func SetResponseAsJson(w http.ResponseWriter, r *http.Request, statusCode int, data any)

"application/json"としてレスポンスを返す dataはjson.Marshalで変換を行ってレスポンスへセットする。 json.Marshalで変換に失敗した場合はpanicとなる。

func Shutdown

func Shutdown()

テスト用。 shutdownチャネルに送信され、 StartForTest関数の方に書いているチャネル受信処理(select)で受け取り、 後続のシャットダウン処理が走る。

func StartServer

func StartServer(c context.Context, host string, port int)

サーバーを起動する この関数を実行する前に、各ハンドラの設定を行う必要がある。 シャットダウンはGraceful shutdownとなる。

Types

type ErrBind

type ErrBind struct {
	Err error
}

func (*ErrBind) Error

func (e *ErrBind) Error() string

func (*ErrBind) Unwrap

func (e *ErrBind) Unwrap() error

type ErrRequestFieldFormat

type ErrRequestFieldFormat struct {
	Field string
	Err   error
}

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

type ErrRequestJsonSomethingInvalid struct {
	Json string
	Err  error
}

func (*ErrRequestJsonSomethingInvalid) Error

func (*ErrRequestJsonSomethingInvalid) Unwrap

type ErrRequestJsonSyntaxError

type ErrRequestJsonSyntaxError struct {
	Json string
	Err  error
}

func (*ErrRequestJsonSyntaxError) Error

func (e *ErrRequestJsonSyntaxError) Error() string

func (*ErrRequestJsonSyntaxError) Unwrap

func (e *ErrRequestJsonSyntaxError) Unwrap() error

type Handler

type Handler func(http.ResponseWriter, *http.Request)

type Logger

type Logger interface {
	Info(c context.Context, args ...any)
	Debug(c context.Context, args ...any)
	Warn(c context.Context, args ...any)
	Error(c context.Context, args ...any)
}

type Middleware

type Middleware func(h http.Handler) http.Handler

Jump to

Keyboard shortcuts

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