summaryrefslogtreecommitdiff
path: root/api/exampleReq.go
blob: 54454a127f4d1c589364d13a2a6dae3a416fcd7b (plain) (blame)
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
52
53
54
55
56
57
package api

import (
	"encoding/json"
	"fmt"
	"net/http"

	redqdb "sinanmohd.com/redq/db"
)

type examplApiName struct {
	db   *redqdb.SafeDB
	req  *RequestApiName
	resp *ResponseApiName
}

type RequestApiName struct {
	BearerToken string
}

type ResponseApiName struct {
	Bearer *redqdb.Bearer
}

func newExamplApiName(db *redqdb.SafeDB) *examplApiName {
	a := &examplApiName{}
	a.db = db

	return a
}

func (a *examplApiName) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
	a.req = &RequestApiName{}
	a.resp = &ResponseApiName{}
	a.resp.Bearer = &redqdb.Bearer{}

	err := unmarshal(r.Body, a.req)
	fmt.Println(a.req)
	if err != nil {
		handleError(err, rw, http.StatusUnprocessableEntity)
		return
	}

	err = a.resp.Bearer.VerifyAndUpdate(a.db, a.req.BearerToken)
	if err != nil {
		handleError(err, rw, http.StatusUnauthorized)
		return
	}

	json, err := json.Marshal(a.resp)
	if err != nil {
		handleError(err, rw, http.StatusInternalServerError)
		return
	}

	rw.Write(json)
}