MCPcopy Index your code
hub / github.com/wavetermdev/waveterm / WebFnWrap

Function WebFnWrap

pkg/web/web.go:383–419  ·  view source on GitHub ↗
(opts WebFnOpts, fn WebFnType)

Source from the content-addressed store, hash-verified

381}
382
383func WebFnWrap(opts WebFnOpts, fn WebFnType) WebFnType {
384 return func(w http.ResponseWriter, r *http.Request) {
385 defer func() {
386 recErr := panichandler.PanicHandler("WebFnWrap", recover())
387 if recErr == nil {
388 return
389 }
390 if opts.JsonErrors {
391 jsonRtn := marshalReturnValue(nil, recErr)
392 w.Header().Set(ContentTypeHeaderKey, ContentTypeJson)
393 w.Header().Set(ContentLengthHeaderKey, fmt.Sprintf("%d", len(jsonRtn)))
394 w.WriteHeader(http.StatusOK)
395 w.Write(jsonRtn)
396 } else {
397 http.Error(w, recErr.Error(), http.StatusInternalServerError)
398 }
399 }()
400 if !opts.AllowCaching {
401 w.Header().Set(CacheControlHeaderKey, CacheControlHeaderNoCache)
402 }
403 w.Header().Set("Access-Control-Expose-Headers", "X-ZoneFileInfo")
404
405 // Handle CORS preflight OPTIONS requests without auth validation
406 if r.Method == http.MethodOptions {
407 w.WriteHeader(http.StatusOK)
408 return
409 }
410
411 err := authkey.ValidateIncomingRequest(r)
412 if err != nil {
413 w.WriteHeader(http.StatusUnauthorized)
414 w.Write([]byte(fmt.Sprintf("error validating authkey: %v", err)))
415 return
416 }
417 fn(w, r)
418 }
419}
420
421func MakeTCPListener(serviceName string) (net.Listener, error) {
422 serverAddr := "127.0.0.1:"

Callers 1

RunWebServerFunction · 0.85

Calls 8

PanicHandlerFunction · 0.92
ValidateIncomingRequestFunction · 0.92
marshalReturnValueFunction · 0.85
WriteMethod · 0.65
SetMethod · 0.45
HeaderMethod · 0.45
WriteHeaderMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected