caicloud / nirvana

Golang Restful API Framework for Productivity

Home Page:https://caicloud.github.io/nirvana/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

unsupport head method

zjj2wry opened this issue · comments

Is this a BUG REPORT or FEATURE REQUEST?:

Uncomment only one, leave it on its own line:

/kind bug
/kind feature
/cc @kdada

const (
	// Head binds to http.MethodHead and code http.StatusOK(200)
	Head definition.Method = "Head"
)

// ConsumeNoneForHTTPHead adds definition.MIMENone to consumes for head definitions.
// Then you don't need to manually write the consume to every head definitions.
// The head is http head rather than definition.Head.
func ConsumeNoneForHTTPHead() service.DefinitionModifier {
	return func(d *definition.Definition) {
		if service.HTTPMethodFor(d.Method) == http.MethodHead {
			found := false
			for _, v := range d.Consumes {
				if v == definition.MIMENone {
					found = true
					break
				}
			}
			if !found {
				d.Consumes = append(d.Consumes, definition.MIMENone)
			}
		}
	}
}

What happened:

What you expected to happen:

How to reproduce it (as minimally and precisely as possible):

Anything else we need to know?:

Why is it necessary?

Used in the implementation of chunk download. current registered the head method by this function.

service.RegisterMethod(udef.Head, http.MethodHead, http.StatusOK)

Actually you wanna add a new definition.Method(Head) to Nirvana. So you need to do these things:

  1. Add the method to definition
  2. Add a http mapping of the method to service
  3. Add ConsumeNoneForHTTPHead and ProduceNoneForHTTPHead to service
  4. Add the two functions to Nirvana project template cmd/nirvana/project