Skip to content

Commit d6b3b32

Browse files
committed
server: return snippet meta
1 parent 5e2e254 commit d6b3b32

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

pkg/goplay/methods.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ func ValidateContentLength(r lener) error {
2323
return nil
2424
}
2525

26-
func GetSnippet(ctx context.Context, snippetID string) ([]byte, error) {
27-
resp, err := getRequest(ctx, "p/"+snippetID+".go")
26+
func GetSnippet(ctx context.Context, snippetID string) (*Snippet, error) {
27+
fileName := snippetID + ".go"
28+
resp, err := getRequest(ctx, "p/"+fileName)
2829
if err != nil {
2930
return nil, err
3031
}
@@ -37,7 +38,10 @@ func GetSnippet(ctx context.Context, snippetID string) ([]byte, error) {
3738
return nil, err
3839
}
3940

40-
return snippet, nil
41+
return &Snippet{
42+
FileName: fileName,
43+
Contents: string(snippet),
44+
}, nil
4145
case http.StatusNotFound:
4246
return nil, ErrSnippetNotFound
4347
default:

pkg/goplay/types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ import (
55
"time"
66
)
77

8+
// Snippet represents shared snippet
9+
type Snippet struct {
10+
FileName string
11+
Contents string
12+
}
13+
814
// FmtResponse is the response returned from
915
// upstream play.golang.org/fmt request
1016
type FmtResponse struct {

pkg/langserver/request.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ import (
1313
"github.com/x1unix/go-playground/pkg/analyzer"
1414
)
1515

16+
type SnippetResponse struct {
17+
FileName string `json:"fileName"`
18+
Code string `json:"code"`
19+
}
20+
1621
type ShareResponse struct {
1722
SnippetID string `json:"snippetID"`
1823
}

pkg/langserver/server.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,10 @@ func (s *Service) GetSnippet(w http.ResponseWriter, r *http.Request) {
165165
return
166166
}
167167

168-
w.Write(snippet)
168+
WriteJSON(w, SnippetResponse{
169+
FileName: snippet.FileName,
170+
Code: snippet.Contents,
171+
})
169172
}
170173

171174
func (s *Service) Compile(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)