-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.go
40 lines (34 loc) · 982 Bytes
/
api.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
package app
import (
"encoding/json"
chirender "github.com/go-chi/render"
"github.com/pkg/errors"
"net/http"
"time"
)
type CreatePasteRequest struct {
Text string `json:"text"`
Syntax string `json:"syntax"`
UserToken string `json:"token,omitempty"`
Ttl time.Duration
}
func ParseCreatePasteRequest(r *http.Request) (*CreatePasteRequest, error) {
if chirender.GetRequestContentType(r) == chirender.ContentTypeForm {
token := ""
uidCookie, err := r.Cookie("user_id")
if err == nil {
token = uidCookie.Value
}
return &CreatePasteRequest{
Text: r.FormValue("data"),
Syntax: r.FormValue("syntax"),
UserToken: token,
}, nil
}
if chirender.GetRequestContentType(r) == chirender.ContentTypeJSON {
parsedReq := &CreatePasteRequest{}
err := json.NewDecoder(r.Body).Decode(parsedReq)
return parsedReq, err
}
return nil, errors.Errorf("can't parse create request: unknown content type %q", r.Header.Get("Content-Type"))
}