| 174 | const ContentTypeDownload = "application/download" |
| 175 | |
| 176 | func (e *KubePiServer) setResultHandler() { |
| 177 | e.rootRoute.Use(func(ctx *context.Context) { |
| 178 | ctx.Next() |
| 179 | contentType := ctx.ResponseWriter().Header().Get("Content-Type") |
| 180 | if contentType == ContentTypeDownload { |
| 181 | return |
| 182 | } |
| 183 | isProxyPath := func() bool { |
| 184 | p := ctx.GetCurrentRoute().Path() |
| 185 | ss := strings.Split(p, "/") |
| 186 | if len(ss) > 0 { |
| 187 | if ss[0] == "webkubectl" { |
| 188 | return true |
| 189 | } |
| 190 | } |
| 191 | if len(ss) >= 3 { |
| 192 | for i := range ss { |
| 193 | if ss[i] == "proxy" || ss[i] == "ws" { |
| 194 | return true |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | return false |
| 199 | }() |
| 200 | if !isProxyPath { |
| 201 | if ctx.GetStatusCode() >= iris.StatusOK && ctx.GetStatusCode() < iris.StatusBadRequest { |
| 202 | if ctx.Values().Get("token") != nil { |
| 203 | _, _ = ctx.Write(ctx.Values().Get("token").([]uint8)) |
| 204 | } else { |
| 205 | resp := iris.Map{ |
| 206 | "success": true, |
| 207 | "data": ctx.Values().Get("data"), |
| 208 | } |
| 209 | _ = ctx.JSON(resp) |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | }) |
| 214 | } |
| 215 | |
| 216 | func (e *KubePiServer) setUpErrHandler() { |
| 217 | e.rootRoute.OnAnyErrorCode(func(ctx iris.Context) { |