package middleware import ( "bytes" "fmt" "net/http" "path" "text/template" ) func SwaggerUIOAuth2Callback(opts SwaggerUIOpts, next http.Handler) http.Handler { opts.EnsureDefaults() pth := opts.OAuthCallbackURL tmpl := template.Must(template.New("swaggeroauth").Parse(swaggerOAuthTemplate)) buf := bytes.NewBuffer(nil) _ = tmpl.Execute(buf, &opts) b := buf.Bytes() return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { if path.Join(r.URL.Path) == pth { rw.Header().Set("Content-Type", "text/html; charset=utf-8") rw.WriteHeader(http.StatusOK) _, _ = rw.Write(b) return } if next == nil { rw.Header().Set("Content-Type", "text/plain") rw.WriteHeader(http.StatusNotFound) _, _ = rw.Write([]byte(fmt.Sprintf("%q not found", pth))) return } next.ServeHTTP(rw, r) }) } const ( swaggerOAuthTemplate = ` {{ .Title }} ` )