HW15 WIP
parent
ddb1205fd3
commit
9c60336ef5
|
@ -16,4 +16,5 @@ linters:
|
|||
- gofumpt
|
||||
- gosec
|
||||
- nlreturn
|
||||
- exhaustive
|
||||
- exhaustive
|
||||
- exhaustivestruct
|
|
@ -12,11 +12,11 @@ import (
|
|||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/calendar"
|
||||
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/pkg/api/public"
|
||||
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/pkg/config"
|
||||
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/pkg/logger"
|
||||
"github.com/gorilla/mux"
|
||||
store "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/pkg/storage"
|
||||
)
|
||||
|
||||
|
@ -58,7 +58,6 @@ func main() {
|
|||
}
|
||||
}()
|
||||
|
||||
|
||||
_, cancel := context.WithCancel(context.Background())
|
||||
m := mux.NewRouter()
|
||||
|
||||
|
|
|
@ -20,5 +20,5 @@ func (s Service) buildEventList(evtMap map[event.ID]event.Event) ([]*Event, erro
|
|||
events[i] = &evt
|
||||
i++
|
||||
}
|
||||
return events, err
|
||||
return events, fmt.Errorf("can't convert types: %w", err)
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ func (s *Service) Start(conf Config) error {
|
|||
listnGrpc, err := net.Listen("tcp", net.JoinHostPort(conf.Address, conf.Port))
|
||||
RegisterGrpcServer(s.S, s)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("can't start private GRPC service: %w", err)
|
||||
}
|
||||
return s.S.Serve(listnGrpc)
|
||||
}
|
||||
|
|
|
@ -55,7 +55,7 @@ func (s Service) buildEventList(evtMap map[event.ID]event.Event) ([]*Event, erro
|
|||
events[i] = &evt
|
||||
i++
|
||||
}
|
||||
return events, err
|
||||
return events, fmt.Errorf("can't convert types: %w", err)
|
||||
}
|
||||
|
||||
func (s Service) buildTimeAndRange(e *GetByDateReq) (start time.Time, qrange string, err error) {
|
||||
|
|
|
@ -2,6 +2,7 @@ package public
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/golang/protobuf/ptypes/empty"
|
||||
|
@ -26,7 +27,7 @@ func (s *Service) Start(conf config.Calendar) error {
|
|||
s.App.Logger.Infof("public GRPC server starting")
|
||||
listnGrpc, err := net.Listen("tcp", net.JoinHostPort(conf.GRPC.Address, conf.GRPC.Port))
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("can't start public GRPC service: %w", err)
|
||||
}
|
||||
RegisterGrpcServer(s.S, s)
|
||||
return s.S.Serve(listnGrpc)
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package rest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/golang/protobuf/ptypes"
|
||||
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/pkg/api/public"
|
||||
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/pkg/storage/event"
|
||||
|
@ -8,42 +10,42 @@ import (
|
|||
|
||||
func createReq2Event(e public.CreateReq) (res event.Event, err error) {
|
||||
res = event.Event{
|
||||
Title: e.Title,
|
||||
Note: e.Note,
|
||||
UserID: e.UserID,
|
||||
Title: e.Title,
|
||||
Note: e.Note,
|
||||
UserID: e.UserID,
|
||||
}
|
||||
res.Date, err = ptypes.Timestamp(e.Date)
|
||||
if err != nil {
|
||||
return event.Event{}, err
|
||||
return event.Event{}, fmt.Errorf("can;t convert types: %w", err)
|
||||
}
|
||||
res.Latency, err = ptypes.Duration(e.Latency)
|
||||
if err != nil {
|
||||
return event.Event{}, err
|
||||
return event.Event{}, fmt.Errorf("can;t convert types: %w", err)
|
||||
}
|
||||
res.NotifyTime, err = ptypes.Duration(e.NotifyTime)
|
||||
if err != nil {
|
||||
return event.Event{}, err
|
||||
return event.Event{}, fmt.Errorf("can;t convert types: %w", err)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func pubEvent2Event(e public.Event) (res event.Event, err error) {
|
||||
res = event.Event{
|
||||
Title: e.Title,
|
||||
Note: e.Note,
|
||||
UserID: e.UserID,
|
||||
Title: e.Title,
|
||||
Note: e.Note,
|
||||
UserID: e.UserID,
|
||||
}
|
||||
res.Date, err = ptypes.Timestamp(e.Date)
|
||||
if err != nil {
|
||||
return event.Event{}, err
|
||||
return event.Event{}, fmt.Errorf("can;t convert types: %w", err)
|
||||
}
|
||||
res.Latency, err = ptypes.Duration(e.Latency)
|
||||
if err != nil {
|
||||
return event.Event{}, err
|
||||
return event.Event{}, fmt.Errorf("can;t convert types: %w", err)
|
||||
}
|
||||
res.NotifyTime, err = ptypes.Duration(e.NotifyTime)
|
||||
if err != nil {
|
||||
return event.Event{}, err
|
||||
return event.Event{}, fmt.Errorf("can;t convert types: %w", err)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
@ -51,33 +53,33 @@ func pubEvent2Event(e public.Event) (res event.Event, err error) {
|
|||
func event2pubEvent(e event.Event) (res public.Event, err error) {
|
||||
res = public.Event{
|
||||
Title: e.Title,
|
||||
Latency: ptypes.DurationProto(e.Latency),
|
||||
Latency: ptypes.DurationProto(e.Latency),
|
||||
Note: e.Note,
|
||||
UserID: e.UserID,
|
||||
NotifyTime: ptypes.DurationProto(e.NotifyTime),
|
||||
NotifyTime: ptypes.DurationProto(e.NotifyTime),
|
||||
}
|
||||
res.Date, err = ptypes.TimestampProto(e.Date)
|
||||
if err != nil {
|
||||
return public.Event{}, err
|
||||
return public.Event{}, fmt.Errorf("can;t convert types: %w", err)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func events2pubEvents(e map[event.ID]event.Event) (res []public.Event, err error) {
|
||||
for id,ev := range e {
|
||||
for id, ev := range e {
|
||||
r := public.Event{
|
||||
ID: int64(id),
|
||||
Title: ev.Title,
|
||||
Latency: ptypes.DurationProto(ev.Latency),
|
||||
Note: ev.Note,
|
||||
UserID: ev.UserID,
|
||||
NotifyTime: ptypes.DurationProto(ev.NotifyTime),
|
||||
ID: int64(id),
|
||||
Title: ev.Title,
|
||||
Latency: ptypes.DurationProto(ev.Latency),
|
||||
Note: ev.Note,
|
||||
UserID: ev.UserID,
|
||||
NotifyTime: ptypes.DurationProto(ev.NotifyTime),
|
||||
}
|
||||
r.Date, err = ptypes.TimestampProto(ev.Date)
|
||||
if err != nil {
|
||||
return []public.Event{}, err
|
||||
return []public.Event{}, fmt.Errorf("can;t convert types: %w", err)
|
||||
}
|
||||
res = append(res,r)
|
||||
res = append(res, r)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,16 +2,17 @@ package rest
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/calendar"
|
||||
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/pkg/api/public"
|
||||
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/pkg/logger"
|
||||
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/pkg/storage/event"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/calendar"
|
||||
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/pkg/api/public"
|
||||
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/pkg/logger"
|
||||
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/pkg/storage/event"
|
||||
)
|
||||
|
||||
func FromRESTCreate(calendar *calendar.App) http.HandlerFunc {
|
||||
|
@ -34,9 +35,15 @@ func FromRESTCreate(calendar *calendar.App) http.HandlerFunc {
|
|||
if err != nil {
|
||||
err503("can't create event through HTTP API", err, calendar.Logger, r)
|
||||
}
|
||||
bodyOut,err := json.Marshal(&public.CreateRsp{ID: int64(id)})
|
||||
bodyOut, err := json.Marshal(&public.CreateRsp{ID: int64(id)})
|
||||
if err != nil {
|
||||
err503("can't marshal request", err, calendar.Logger, r)
|
||||
}
|
||||
r.WriteHeader(201)
|
||||
r.Write(bodyOut)
|
||||
_, err = r.Write(bodyOut)
|
||||
if err != nil {
|
||||
calendar.Logger.Errorf("can't send response")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -92,9 +99,15 @@ func FromRESTList(calendar *calendar.App) http.HandlerFunc {
|
|||
if err != nil {
|
||||
err503("can't convert types", err, calendar.Logger, r)
|
||||
}
|
||||
bodyOut,err := json.Marshal(&events)
|
||||
bodyOut, err := json.Marshal(&events)
|
||||
if err != nil {
|
||||
err503("can't marshal request", err, calendar.Logger, r)
|
||||
}
|
||||
r.WriteHeader(200)
|
||||
r.Write(bodyOut)
|
||||
_, err = r.Write(bodyOut)
|
||||
if err != nil {
|
||||
calendar.Logger.Errorf("can't send response")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -105,14 +118,19 @@ func FromRESTGetByID(calendar *calendar.App) http.HandlerFunc {
|
|||
err503("can't get request parameter", err, calendar.Logger, r)
|
||||
}
|
||||
ev, _ := calendar.Storage.GetByID(event.ID(paramID))
|
||||
event, err := event2pubEvent(ev)
|
||||
evnt, err := event2pubEvent(ev)
|
||||
if err != nil {
|
||||
err503("can't convert types", err, calendar.Logger, r)
|
||||
}
|
||||
bodyOut,err := json.Marshal(&event)
|
||||
bodyOut, err := json.Marshal(&evnt)
|
||||
if err != nil {
|
||||
err503("can't marshal request", err, calendar.Logger, r)
|
||||
}
|
||||
r.WriteHeader(200)
|
||||
r.Write(bodyOut)
|
||||
|
||||
_, err = r.Write(bodyOut)
|
||||
if err != nil {
|
||||
calendar.Logger.Errorf("can't send response")
|
||||
}
|
||||
|
||||
log.Println(paramID)
|
||||
r.WriteHeader(555)
|
||||
|
@ -134,13 +152,19 @@ func FromRESTGetByDate(calendar *calendar.App) http.HandlerFunc {
|
|||
if err != nil {
|
||||
err503("can't convert types", err, calendar.Logger, r)
|
||||
}
|
||||
bodyOut,err := json.Marshal(&events)
|
||||
bodyOut, err := json.Marshal(&events)
|
||||
if err != nil {
|
||||
err503("can't marshal request", err, calendar.Logger, r)
|
||||
}
|
||||
r.WriteHeader(200)
|
||||
r.Write(bodyOut)
|
||||
_, err = r.Write(bodyOut)
|
||||
if err != nil {
|
||||
calendar.Logger.Errorf("can't send response")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func err503(s string, err error, l logger.Interface, r http.ResponseWriter) {
|
||||
l.Errorf(s,": ", err.Error())
|
||||
l.Errorf(s, ": ", err.Error())
|
||||
r.WriteHeader(503)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,8 +6,8 @@ import (
|
|||
"log"
|
||||
"time"
|
||||
|
||||
// Postgresql driver.
|
||||
_ "github.com/lib/pq"
|
||||
|
||||
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/pkg/storage/event"
|
||||
)
|
||||
|
||||
|
@ -28,7 +28,7 @@ type Storage struct {
|
|||
func New(config Config) *Storage {
|
||||
db, err := sql.Open("postgres", "user="+config.User+" password="+config.Pass+" host="+config.Host+" port="+config.Port+" dbname="+config.Dbase+" sslmode=disable")
|
||||
if err != nil {
|
||||
log.Fatalf("can't connect to db: %s",err.Error())
|
||||
log.Fatalf("can't connect to db: %s", err.Error())
|
||||
}
|
||||
return &Storage{db: db}
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ func (s *Storage) Close() error {
|
|||
}
|
||||
|
||||
func (s *Storage) Create(ev event.Event) (event.ID, error) {
|
||||
lastInsertId := -1
|
||||
lastInsertID := -1
|
||||
if err := s.db.QueryRow(
|
||||
`INSERT INTO events
|
||||
(title, date, latency, note, userID, notifyTime) VALUES
|
||||
|
@ -49,10 +49,10 @@ func (s *Storage) Create(ev event.Event) (event.ID, error) {
|
|||
ev.Note,
|
||||
ev.UserID,
|
||||
ev.NotifyTime,
|
||||
).Scan(&lastInsertId); err != nil {
|
||||
).Scan(&lastInsertID); err != nil {
|
||||
return -1, fmt.Errorf("can't create event in SQL DB: %w", err)
|
||||
}
|
||||
return event.ID(lastInsertId), nil
|
||||
return event.ID(lastInsertID), nil
|
||||
}
|
||||
|
||||
func (s *Storage) Update(id event.ID, event event.Event) error {
|
||||
|
|
|
@ -7,9 +7,9 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
type GRPCAPI struct{
|
||||
type GRPCAPI struct {
|
||||
Name string
|
||||
Ctx context.Context
|
||||
Ctx context.Context
|
||||
Host string
|
||||
Port string
|
||||
}
|
||||
|
@ -20,11 +20,11 @@ func (h GRPCAPI) GetName() string {
|
|||
|
||||
func (h GRPCAPI) Create(req *public.CreateReq) (*public.CreateRsp, error) {
|
||||
ctx, cliGRPC, err := getCli(h)
|
||||
if err != nil{
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := cliGRPC.Create(ctx, req)
|
||||
if err != nil{
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
|
@ -32,11 +32,11 @@ func (h GRPCAPI) Create(req *public.CreateReq) (*public.CreateRsp, error) {
|
|||
|
||||
func (h GRPCAPI) Update(req *public.UpdateReq) error {
|
||||
ctx, cliGRPC, err := getCli(h)
|
||||
if err != nil{
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = cliGRPC.Update(ctx, req)
|
||||
if err != nil{
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
@ -44,11 +44,11 @@ func (h GRPCAPI) Update(req *public.UpdateReq) error {
|
|||
|
||||
func (h GRPCAPI) Delete(req *public.DeleteReq) error {
|
||||
ctx, cliGRPC, err := getCli(h)
|
||||
if err != nil{
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = cliGRPC.Delete(ctx, req)
|
||||
if err != nil{
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
@ -56,11 +56,11 @@ func (h GRPCAPI) Delete(req *public.DeleteReq) error {
|
|||
|
||||
func (h GRPCAPI) GetByID(req *public.GetByIDReq) (*public.GetByIDResp, error) {
|
||||
ctx, cliGRPC, err := getCli(h)
|
||||
if err != nil{
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := cliGRPC.GetByID(ctx, req)
|
||||
if err != nil{
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
|
@ -68,11 +68,11 @@ func (h GRPCAPI) GetByID(req *public.GetByIDReq) (*public.GetByIDResp, error) {
|
|||
|
||||
func (h GRPCAPI) List() (*public.ListResp, error) {
|
||||
ctx, cliGRPC, err := getCli(h)
|
||||
if err != nil{
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := cliGRPC.List(ctx, &empty.Empty{})
|
||||
if err != nil{
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
|
@ -80,18 +80,18 @@ func (h GRPCAPI) List() (*public.ListResp, error) {
|
|||
|
||||
func (h GRPCAPI) GetByDate(req *public.GetByDateReq) (*public.GetByDateResp, error) {
|
||||
ctx, cliGRPC, err := getCli(h)
|
||||
if err != nil{
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := cliGRPC.GetByDate(ctx, req)
|
||||
if err != nil{
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func getCli(h GRPCAPI) (context.Context, public.GrpcClient, error) {
|
||||
ctx,_ := context.WithTimeout(h.Ctx, 15*time.Second)
|
||||
cliGRPC, err := public.NewClient(ctx,h.Host, h.Port)
|
||||
ctx, _ := context.WithTimeout(h.Ctx, 15*time.Second)
|
||||
cliGRPC, err := public.NewClient(ctx, h.Host, h.Port)
|
||||
return ctx, cliGRPC, err
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,8 +12,8 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
type HTTPAPI struct{
|
||||
Name string
|
||||
type HTTPAPI struct {
|
||||
Name string
|
||||
BaseURL string
|
||||
}
|
||||
|
||||
|
@ -22,19 +22,19 @@ func (h HTTPAPI) GetName() string {
|
|||
}
|
||||
|
||||
func (h HTTPAPI) Create(req *public.CreateReq) (*public.CreateRsp, error) {
|
||||
jreq,err:= json.Marshal(req)
|
||||
jreq, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return &public.CreateRsp{}, err
|
||||
}
|
||||
res, body, err := apiCall("POST",h.BaseURL+"/events", jreq)
|
||||
res, body, err := apiCall("POST", h.BaseURL+"/events", jreq)
|
||||
if err != nil {
|
||||
return &public.CreateRsp{}, err
|
||||
}
|
||||
if res.StatusCode!=201 {
|
||||
return &public.CreateRsp{}, fmt.Errorf("unexpected status code %d",res.StatusCode)
|
||||
if res.StatusCode != 201 {
|
||||
return &public.CreateRsp{}, fmt.Errorf("unexpected status code %d", res.StatusCode)
|
||||
}
|
||||
var createRsp public.CreateRsp
|
||||
err = json.Unmarshal(body,&createRsp)
|
||||
err = json.Unmarshal(body, &createRsp)
|
||||
if err != nil {
|
||||
return &public.CreateRsp{}, err
|
||||
}
|
||||
|
@ -42,85 +42,85 @@ func (h HTTPAPI) Create(req *public.CreateReq) (*public.CreateRsp, error) {
|
|||
}
|
||||
|
||||
func (h HTTPAPI) Update(req *public.UpdateReq) error {
|
||||
jreq, err:= json.Marshal(req)
|
||||
jreq, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
res, _, err := apiCall("PUT",h.BaseURL+"/events/"+strconv.Itoa(int(req.ID)), jreq)
|
||||
res, _, err := apiCall("PUT", h.BaseURL+"/events/"+strconv.Itoa(int(req.ID)), jreq)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if res.StatusCode!=200 {
|
||||
return fmt.Errorf("unexpected status code %d",res.StatusCode)
|
||||
if res.StatusCode != 200 {
|
||||
return fmt.Errorf("unexpected status code %d", res.StatusCode)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h HTTPAPI) Delete(req *public.DeleteReq) error {
|
||||
jreq, err:= json.Marshal(req)
|
||||
jreq, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
res, _, err := apiCall("DELETE",h.BaseURL+"/events/"+strconv.Itoa(int(req.ID)), jreq)
|
||||
res, _, err := apiCall("DELETE", h.BaseURL+"/events/"+strconv.Itoa(int(req.ID)), jreq)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if res.StatusCode!=200 {
|
||||
return fmt.Errorf("unexpected status code %d",res.StatusCode)
|
||||
if res.StatusCode != 200 {
|
||||
return fmt.Errorf("unexpected status code %d", res.StatusCode)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h HTTPAPI) GetByID(req *public.GetByIDReq) ( *public.GetByIDResp, error) {
|
||||
jreq, err:= json.Marshal(req)
|
||||
func (h HTTPAPI) GetByID(req *public.GetByIDReq) (*public.GetByIDResp, error) {
|
||||
jreq, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return &public.GetByIDResp{}, err
|
||||
}
|
||||
res, body, err := apiCall("GET",h.BaseURL+"/events/"+strconv.Itoa(int(req.ID)), jreq)
|
||||
res, body, err := apiCall("GET", h.BaseURL+"/events/"+strconv.Itoa(int(req.ID)), jreq)
|
||||
if err != nil {
|
||||
return &public.GetByIDResp{}, err
|
||||
}
|
||||
if res.StatusCode!=200 {
|
||||
return &public.GetByIDResp{}, fmt.Errorf("unexpected status code %d",res.StatusCode)
|
||||
if res.StatusCode != 200 {
|
||||
return &public.GetByIDResp{}, fmt.Errorf("unexpected status code %d", res.StatusCode)
|
||||
}
|
||||
var getByIDResp public.GetByIDResp
|
||||
err = json.Unmarshal(body,&getByIDResp)
|
||||
err = json.Unmarshal(body, &getByIDResp)
|
||||
if err != nil {
|
||||
return &public.GetByIDResp{}, err
|
||||
}
|
||||
return &getByIDResp, nil
|
||||
}
|
||||
|
||||
func (h HTTPAPI) List() ( *public.ListResp, error) {
|
||||
res, body, err := apiCall("GET",h.BaseURL+"/events", nil)
|
||||
func (h HTTPAPI) List() (*public.ListResp, error) {
|
||||
res, body, err := apiCall("GET", h.BaseURL+"/events", nil)
|
||||
if err != nil {
|
||||
return &public.ListResp{}, err
|
||||
}
|
||||
if res.StatusCode!=200 {
|
||||
return &public.ListResp{}, fmt.Errorf("unexpected status code %d",res.StatusCode)
|
||||
if res.StatusCode != 200 {
|
||||
return &public.ListResp{}, fmt.Errorf("unexpected status code %d", res.StatusCode)
|
||||
}
|
||||
var listResp public.ListResp
|
||||
err = json.Unmarshal(body,&listResp)
|
||||
err = json.Unmarshal(body, &listResp)
|
||||
if err != nil {
|
||||
return &public.ListResp{}, err
|
||||
}
|
||||
return &listResp, nil
|
||||
}
|
||||
|
||||
func (h HTTPAPI) GetByDate(req *public.GetByDateReq) ( *public.GetByDateResp, error) {
|
||||
jreq, err:= json.Marshal(req)
|
||||
func (h HTTPAPI) GetByDate(req *public.GetByDateReq) (*public.GetByDateResp, error) {
|
||||
jreq, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return &public.GetByDateResp{}, err
|
||||
}
|
||||
res, body, err := apiCall("GET",h.BaseURL+"/events/"+string(req.Range)+"/"+req.Date.String(), jreq)
|
||||
res, body, err := apiCall("GET", h.BaseURL+"/events/"+string(req.Range)+"/"+req.Date.String(), jreq)
|
||||
if err != nil {
|
||||
return &public.GetByDateResp{}, err
|
||||
}
|
||||
if res.StatusCode!=200 {
|
||||
return &public.GetByDateResp{}, fmt.Errorf("unexpected status code %d",res.StatusCode)
|
||||
if res.StatusCode != 200 {
|
||||
return &public.GetByDateResp{}, fmt.Errorf("unexpected status code %d", res.StatusCode)
|
||||
}
|
||||
var getByDateResp public.GetByDateResp
|
||||
err = json.Unmarshal(body,&getByDateResp)
|
||||
err = json.Unmarshal(body, &getByDateResp)
|
||||
if err != nil {
|
||||
return &public.GetByDateResp{}, err
|
||||
}
|
||||
|
@ -128,7 +128,7 @@ func (h HTTPAPI) GetByDate(req *public.GetByDateReq) ( *public.GetByDateResp, er
|
|||
}
|
||||
|
||||
func apiCall(method string, url string, payload []byte) (*http.Response, []byte, error) {
|
||||
client := &http.Client{Transport: &http.Transport{ DialContext: (&net.Dialer{ Timeout: 15*time.Second}).DialContext }}
|
||||
client := &http.Client{Transport: &http.Transport{DialContext: (&net.Dialer{Timeout: 15 * time.Second}).DialContext}}
|
||||
req, err := http.NewRequest(method, url, bytes.NewBuffer(payload))
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
|
|
|
@ -4,7 +4,6 @@ import (
|
|||
"log"
|
||||
)
|
||||
|
||||
|
||||
func main() {
|
||||
log.Fatalln("Nothing to do")
|
||||
}
|
||||
|
|
|
@ -14,28 +14,28 @@ import (
|
|||
|
||||
func TestPublicAPIEndpoints(t *testing.T) {
|
||||
cli := []client.Interface{
|
||||
client.GRPCAPI{Ctx: context.Background(), Host: "localhost", Port:"50051", Name: "GRPC API"},
|
||||
client.GRPCAPI{Ctx: context.Background(), Host: "localhost", Port: "50051", Name: "GRPC API"},
|
||||
client.HTTPAPI{BaseURL: "http://localhost:50052", Name: "HTTP REST API"},
|
||||
}
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(len(cli)*5)
|
||||
wg.Add(len(cli) * 5)
|
||||
|
||||
for _,c := range cli {
|
||||
for _, c := range cli {
|
||||
t.Run("test "+c.GetName()+" for Create, GetById and Delete", func(t *testing.T) {
|
||||
var ids []int64
|
||||
defer func() {
|
||||
wg.Done()
|
||||
clean(c,&ids)
|
||||
clean(c, &ids)
|
||||
}()
|
||||
|
||||
resp1, err := c.Create(&misc.TestEvent01)
|
||||
require.NoError(t, err)
|
||||
require.Greater(t, resp1.ID, int64(0))
|
||||
ids=append(ids,resp1.ID)
|
||||
ids = append(ids, resp1.ID)
|
||||
|
||||
resp2,err := c.GetByID(&public.GetByIDReq{ID:resp1.ID})
|
||||
resp2, err := c.GetByID(&public.GetByIDReq{ID: resp1.ID})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t,1, len(resp2.Events))
|
||||
require.Equal(t, 1, len(resp2.Events))
|
||||
require.Equal(t, misc.TestEvent01.Title, resp2.Events[0].Title)
|
||||
require.Equal(t, misc.TestEvent01.UserID, resp2.Events[0].UserID)
|
||||
require.Equal(t, misc.TestEvent01.Note, resp2.Events[0].Note)
|
||||
|
@ -45,20 +45,20 @@ func TestPublicAPIEndpoints(t *testing.T) {
|
|||
var ids []int64
|
||||
defer func() {
|
||||
wg.Done()
|
||||
clean(c,&ids)
|
||||
clean(c, &ids)
|
||||
}()
|
||||
|
||||
resp1, err := c.Create(&misc.TestEvent01)
|
||||
require.NoError(t, err)
|
||||
require.Greater(t, resp1.ID, int64(0))
|
||||
ids=append(ids,resp1.ID)
|
||||
ids = append(ids, resp1.ID)
|
||||
|
||||
err = c.Update(&public.UpdateReq{ID:resp1.ID, Event: &public.Event{ ID: resp1.ID, Title: misc.TestEvent02.Title, Date: misc.TestEvent02.Date, Latency: misc.TestEvent02.Latency, Note: misc.TestEvent02.Note, UserID: misc.TestEvent02.UserID, NotifyTime: misc.TestEvent02.NotifyTime }})
|
||||
err = c.Update(&public.UpdateReq{ID: resp1.ID, Event: &public.Event{ID: resp1.ID, Title: misc.TestEvent02.Title, Date: misc.TestEvent02.Date, Latency: misc.TestEvent02.Latency, Note: misc.TestEvent02.Note, UserID: misc.TestEvent02.UserID, NotifyTime: misc.TestEvent02.NotifyTime}})
|
||||
require.NoError(t, err)
|
||||
|
||||
resp2,err := c.GetByID(&public.GetByIDReq{ID:resp1.ID})
|
||||
resp2, err := c.GetByID(&public.GetByIDReq{ID: resp1.ID})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t,1, len(resp2.Events))
|
||||
require.Equal(t, 1, len(resp2.Events))
|
||||
require.Equal(t, misc.TestEvent02.Title, resp2.Events[0].Title)
|
||||
require.Equal(t, misc.TestEvent02.UserID, resp2.Events[0].UserID)
|
||||
require.Equal(t, misc.TestEvent02.Note, resp2.Events[0].Note)
|
||||
|
@ -71,10 +71,10 @@ func TestPublicAPIEndpoints(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
require.Greater(t, resp1.ID, int64(0))
|
||||
|
||||
err = c.Delete(&public.DeleteReq{ ID: resp1.ID })
|
||||
err = c.Delete(&public.DeleteReq{ID: resp1.ID})
|
||||
require.NoError(t, err)
|
||||
|
||||
resp2,err := c.GetByID(&public.GetByIDReq{ID:resp1.ID})
|
||||
resp2, err := c.GetByID(&public.GetByIDReq{ID: resp1.ID})
|
||||
require.Error(t, err)
|
||||
require.Nil(t, resp2)
|
||||
})
|
||||
|
@ -83,19 +83,18 @@ func TestPublicAPIEndpoints(t *testing.T) {
|
|||
var ids []int64
|
||||
defer func() {
|
||||
wg.Done()
|
||||
clean(c,&ids)
|
||||
clean(c, &ids)
|
||||
}()
|
||||
|
||||
resp1, err := c.Create(&misc.TestEvent01)
|
||||
require.NoError(t, err)
|
||||
require.Greater(t, resp1.ID, int64(0))
|
||||
ids=append(ids,resp1.ID)
|
||||
|
||||
ids = append(ids, resp1.ID)
|
||||
|
||||
resp2, err := c.Create(&misc.TestEvent02)
|
||||
require.NoError(t, err)
|
||||
require.Greater(t, resp2.ID, int64(0))
|
||||
ids=append(ids,resp2.ID)
|
||||
ids = append(ids, resp2.ID)
|
||||
|
||||
resp3, err := c.List()
|
||||
require.NoError(t, err)
|
||||
|
@ -112,10 +111,10 @@ func TestPublicAPIEndpoints(t *testing.T) {
|
|||
require.True(t, e1)
|
||||
require.True(t, e2)
|
||||
|
||||
err = c.Delete(&public.DeleteReq{ ID: resp1.ID })
|
||||
err = c.Delete(&public.DeleteReq{ID: resp1.ID})
|
||||
require.NoError(t, err)
|
||||
|
||||
err = c.Delete(&public.DeleteReq{ ID: resp2.ID })
|
||||
err = c.Delete(&public.DeleteReq{ID: resp2.ID})
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
|
@ -123,12 +122,12 @@ func TestPublicAPIEndpoints(t *testing.T) {
|
|||
var ids []int64
|
||||
defer func() {
|
||||
wg.Done()
|
||||
clean(c,&ids)
|
||||
clean(c, &ids)
|
||||
}()
|
||||
|
||||
startDate, err := time.Parse("2006-01-02T15:04:00","3100-01-01T12:00:00")
|
||||
startDate, err := time.Parse("2006-01-02T15:04:00", "3100-01-01T12:00:00")
|
||||
require.NoError(t, err)
|
||||
for i:=time.Hour;i<(60*24*time.Hour);i=i+48*time.Hour{
|
||||
for i := time.Hour; i < (60 * 24 * time.Hour); i = i + 48*time.Hour {
|
||||
resp1, err := c.Create(&public.CreateReq{
|
||||
Title: "Test event 02",
|
||||
Date: misc.Time2pbtimestamp(startDate.Add(i)),
|
||||
|
@ -139,29 +138,29 @@ func TestPublicAPIEndpoints(t *testing.T) {
|
|||
})
|
||||
require.NoError(t, err)
|
||||
require.Greater(t, resp1.ID, int64(0))
|
||||
ids=append(ids,resp1.ID)
|
||||
ids = append(ids, resp1.ID)
|
||||
}
|
||||
|
||||
resp2,err := c.GetByDate(&public.GetByDateReq{Date: misc.Time2pbtimestamp(startDate), Range: public.QueryRange_DAY})
|
||||
resp2, err := c.GetByDate(&public.GetByDateReq{Date: misc.Time2pbtimestamp(startDate), Range: public.QueryRange_DAY})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 1, len(resp2.Events) )
|
||||
require.Equal(t, 1, len(resp2.Events))
|
||||
|
||||
resp3,err := c.GetByDate(&public.GetByDateReq{Date: misc.Time2pbtimestamp(startDate), Range: public.QueryRange_WEEK})
|
||||
resp3, err := c.GetByDate(&public.GetByDateReq{Date: misc.Time2pbtimestamp(startDate), Range: public.QueryRange_WEEK})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 4, len(resp3.Events) )
|
||||
require.Equal(t, 4, len(resp3.Events))
|
||||
|
||||
resp4,err := c.GetByDate(&public.GetByDateReq{Date: misc.Time2pbtimestamp(startDate), Range: public.QueryRange_MONTH})
|
||||
resp4, err := c.GetByDate(&public.GetByDateReq{Date: misc.Time2pbtimestamp(startDate), Range: public.QueryRange_MONTH})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 16, len(resp4.Events) )
|
||||
require.Equal(t, 16, len(resp4.Events))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func clean(c client.Interface, ids *[]int64) {
|
||||
for _,m := range *ids {
|
||||
err := c.Delete(&public.DeleteReq{ID:m})
|
||||
for _, m := range *ids {
|
||||
err := c.Delete(&public.DeleteReq{ID: m})
|
||||
if err != nil {
|
||||
log.Println("error when try to clean DB: ", err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue