Files
podinfo/pkg/api/chunked.go
stefanprodan 2479134e78 Implement swagger support
- add swagger definitions for all API routes
- self-host the swagger UI on `/swagger/`
- serve swagger spec on `/swagger.json`
2019-08-07 14:17:35 +03:00

45 lines
1.0 KiB
Go

package api
import (
"math/rand"
"net/http"
"strconv"
"time"
"github.com/gorilla/mux"
)
// Chunked godoc
// @Summary Chunked transfer encoding
// @Description uses transfer-encoding type chunked to give a partial response and then waits for the specified period
// @Tags HTTP API
// @Accept json
// @Produce json
// @Router /chunked/{seconds} [get]
// @Success 200 {object} api.MapResponse
func (s *Server) chunkedHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
delay, err := strconv.Atoi(vars["wait"])
if err != nil {
delay = rand.Intn(int(s.config.HttpServerTimeout*time.Second)-10) + 10
}
flusher, ok := w.(http.Flusher)
if !ok {
s.ErrorResponse(w, r, "Streaming unsupported!", http.StatusInternalServerError)
return
}
w.Header().Set("Connection", "Keep-Alive")
w.Header().Set("Transfer-Encoding", "chunked")
w.Header().Set("X-Content-Type-Options", "nosniff")
flusher.Flush()
time.Sleep(time.Duration(delay) * time.Second)
s.JSONResponse(w, r, map[string]int{"delay": delay})
flusher.Flush()
}