2021-02-02 16:13:39 +03:00
|
|
|
package http
|
|
|
|
|
2021-02-07 13:27:28 +03:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/unistack-org/micro/v3/server"
|
|
|
|
)
|
2021-02-02 16:13:39 +03:00
|
|
|
|
|
|
|
type rspCodeKey struct{}
|
|
|
|
type rspCodeVal struct {
|
|
|
|
code int
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetRspCode saves response code in context, must be used by handler to specify http code
|
|
|
|
func SetRspCode(ctx context.Context, code int) {
|
|
|
|
if rsp, ok := ctx.Value(rspCodeKey{}).(*rspCodeVal); ok {
|
|
|
|
rsp.code = code
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRspCode used internally by generated http server handler
|
|
|
|
func GetRspCode(ctx context.Context) int {
|
|
|
|
var code int
|
|
|
|
if rsp, ok := ctx.Value(rspCodeKey{}).(*rspCodeVal); ok {
|
|
|
|
code = rsp.code
|
|
|
|
}
|
|
|
|
return code
|
|
|
|
}
|
2021-02-07 13:27:28 +03:00
|
|
|
|
|
|
|
type middlewareKey struct{}
|
|
|
|
|
|
|
|
func Middleware(mw ...func(http.Handler) http.Handler) server.Option {
|
|
|
|
return server.SetOption(middlewareKey{}, mw)
|
|
|
|
}
|
2021-02-07 19:03:38 +03:00
|
|
|
|
|
|
|
type serverKey struct{}
|
|
|
|
|
|
|
|
func Server(hs *http.Server) server.Option {
|
|
|
|
return server.SetOption(serverKey{}, hs)
|
|
|
|
}
|