Debian Codegraph @

server/quark.go

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