-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
inspection.go
47 lines (44 loc) · 1.14 KB
/
inspection.go
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
package server
import (
"encoding/json"
"net/http"
"github.com/emicklei/melrose/core"
"github.com/emicklei/melrose/notify"
)
func (l *LanguageServer) inspectHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
notify.Console.Warnf("HTTP method not allowed:%s", r.Method)
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
debug := r.URL.Query().Get("debug") == "true" || core.IsDebug()
if debug {
notify.Debugf("service.http: %s", r.URL.String())
}
// get token
defer r.Body.Close()
type tokenHolder struct {
Token string
}
th := new(tokenHolder)
if err := json.NewDecoder(r.Body).Decode(th); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
if debug {
notify.Debugf("http.request.body %#v", th)
}
type markdownHolder struct {
MarkdownString string
}
w.Header().Set("content-type", "application/json")
enc := json.NewEncoder(w)
msg := l.service.CommandHover(th.Token)
err := enc.Encode(markdownHolder{MarkdownString: msg})
if debug {
notify.Debugf("service.http.response.MarkdownString: %s", msg)
}
if err != nil {
notify.Console.Errorf("inspect failed:%v\n", err)
}
}