mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Vast commit, includes: * Introduces the Trigger domain entity. * Introduces the Fns domain entity. * V2 of the API for interacting with the new entities in swaggerv2.yml * Adds v2 end points for Apps to support PUT updates. * Rewrites the datastore level tests into a new pattern. * V2 routes use entity ID over name as the path parameter.
76 lines
1.6 KiB
Go
76 lines
1.6 KiB
Go
package server
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"net/http"
|
|
|
|
"errors"
|
|
"strings"
|
|
|
|
"github.com/fnproject/fn/api"
|
|
"github.com/fnproject/fn/api/models"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// note: for backward compatibility, will go away later
|
|
type callLogResponse struct {
|
|
Message string `json:"message"`
|
|
Log *CallLog `json:"log"`
|
|
}
|
|
|
|
type CallLog struct {
|
|
CallID string `json:"call_id" db:"id"`
|
|
Log string `json:"log" db:"log"`
|
|
}
|
|
|
|
func writeJSON(c *gin.Context, callID string, logReader io.Reader) {
|
|
var b bytes.Buffer
|
|
b.ReadFrom(logReader)
|
|
c.JSON(http.StatusOK, callLogResponse{"Successfully loaded log",
|
|
&CallLog{
|
|
CallID: callID,
|
|
Log: b.String(),
|
|
}})
|
|
}
|
|
|
|
func (s *Server) handleCallLogGet(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
|
|
appID := c.MustGet(api.AppID).(string)
|
|
callID := c.Param(api.ParamCallID)
|
|
|
|
logReader, err := s.logstore.GetLog(ctx, appID, callID)
|
|
if err != nil {
|
|
handleV1ErrorResponse(c, err)
|
|
return
|
|
}
|
|
|
|
mimeTypes, _ := c.Request.Header["Accept"]
|
|
|
|
if len(mimeTypes) == 0 {
|
|
writeJSON(c, callID, logReader)
|
|
return
|
|
}
|
|
|
|
for _, mimeType := range mimeTypes {
|
|
if strings.Contains(mimeType, "application/json") {
|
|
writeJSON(c, callID, logReader)
|
|
return
|
|
}
|
|
if strings.Contains(mimeType, "text/plain") {
|
|
io.Copy(c.Writer, logReader)
|
|
return
|
|
|
|
}
|
|
if strings.Contains(mimeType, "*/*") {
|
|
writeJSON(c, callID, logReader)
|
|
return
|
|
}
|
|
}
|
|
|
|
// if we've reached this point it means that Fn didn't recognize Accepted content type
|
|
handleV1ErrorResponse(c, models.NewAPIError(http.StatusNotAcceptable,
|
|
errors.New("unable to respond within acceptable response content types")))
|
|
}
|