1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
package server
import (
"encoding/json"
"html/template"
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
// TODO: This is a disgusting hack to match the output of the internal quark command.
// They should return JSON instead of HTML!
func (s *Server) handleQuarkCommand() gin.HandlerFunc {
return func(c *gin.Context) {
res, err := s.b.RunQuarkCommand(c.Param("command"), strings.Split(c.Query("strInput"), " ")...)
if err != nil {
_ = c.AbortWithError(http.StatusInternalServerError, err)
return
}
var out string
switch res := res.(type) {
case string:
out = res
default:
b, err := json.MarshalIndent(res, "", " ")
if err != nil {
_ = c.AbortWithError(http.StatusInternalServerError, err)
return
}
out = string(b)
}
tmp, err := template.New("quarkCommand").Parse(`<html><body><div class="content">{{.Content}}</div></body></html>`)
if err != nil {
_ = c.AbortWithError(http.StatusInternalServerError, err)
return
}
if err := tmp.Execute(c.Writer, map[string]string{
"Content": template.HTMLEscapeString(out),
}); err != nil {
_ = c.AbortWithError(http.StatusInternalServerError, err)
return
}
}
}
|