summaryrefslogblamecommitdiff
path: root/api/exampleReq.go
blob: 40bd55fea0df6dfb71c2f2f53206bd98bb0b5b14 (plain) (tree)





















                                      
                                              



































                                                                            
package api

import (
	"fmt"
	"log"
	"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
	Error  string `json:"error,omitempty"`
}

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)
	if err != nil {
		log.Println(err)
		return
	}

	err = a.resp.Bearer.VerifyAndUpdate(a.db, a.req.BearerToken)
	if err != nil {
		log.Println(err)
		a.resp.Error = err.Error()
		return
	}

	json, err := marshal(a.resp)
	if err != nil {
		log.Println(err)
		a.resp.Error = err.Error()
		return
	}

	fmt.Fprintf(rw, json)
}