pull/14/head
Andrey Ivanov 2020-09-30 17:09:25 +03:00 committed by tiburon
parent 34a017b088
commit 1c48e18aed
14 changed files with 623 additions and 372 deletions

View File

@ -1,3 +1,5 @@
cdir = $(shell pwd)
build:
go build -o ./bin/calendar ./cmd/calendar/main.go
@ -15,4 +17,10 @@ install-lint-deps:
generate:
protoc --go_out=grpc:internal grpc/calendar.proto
gen2:
protoc -I /usr/local/include -I ${cdir}/grpc --go_out=plugins=grpc:${cdir}/internal/grpc \
--grpc-gateway_out=logtostderr=true:${cdir}/internal/grpc \
${cdir}/grpc/grpc.proto
.PHONY: build run test lint

View File

@ -2,6 +2,8 @@ package main
import (
"flag"
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/grpc"
googrpc "google.golang.org/grpc"
oslog "log"
"os"
"os/signal"
@ -10,6 +12,7 @@ import (
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/app"
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/config"
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/logger"
internalgrpc "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/server/grpc"
internalhttp "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/server/http"
store "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/storage"
)
@ -42,7 +45,9 @@ func main() {
calendar := app.New(log, st)
server := internalhttp.NewServer(calendar, conf.Server.Address, conf.Server.Port)
serverHTTP := internalhttp.NewServer(calendar, conf.Server.Address, conf.Server.Port)
serverGRPC := googrpc.NewServer()
grpc.RegisterGrpcServer(serverGRPC, internalgrpc.Service{})
go func() {
signals := make(chan os.Signal, 1)
@ -51,12 +56,12 @@ func main() {
<-signals
signal.Stop(signals)
if err := server.Stop(); err != nil {
if err := serverHTTP.Stop(); err != nil {
log.Errorf("failed to stop http server: " + err.Error())
}
}()
if err := server.Start(); err != nil {
if err := serverHTTP.Start(); err != nil {
log.Errorf("failed to start http server: " + err.Error())
os.Exit(1)
}

View File

@ -6,10 +6,13 @@ require (
github.com/BurntSushi/toml v0.3.1
github.com/amitrai48/logger v0.0.0-20190214092904-448001c055ec
github.com/go-sql-driver/mysql v1.5.0
github.com/golang/protobuf v1.4.2 // indirect
github.com/mattn/go-shellwords v1.0.10 // indirect
github.com/golang/protobuf v1.4.2
github.com/grpc-ecosystem/grpc-gateway v1.15.0
github.com/stretchr/testify v1.4.0
go.uber.org/zap v1.15.0 // indirect
golang.org/x/net v0.0.0-20191002035440-2ec189313ef0
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013
google.golang.org/grpc v1.32.0
google.golang.org/protobuf v1.25.0 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
)

View File

@ -1,47 +0,0 @@
syntax = "proto3";
package event;
option go_package = "event";
import "google/protobuf/timestamp.proto";
import "google/protobuf/duration.proto";
import "google/protobuf/empty.proto";
service CalendarRPC {
rpc Create(event) returns (eventID);
rpc Update(eventWithID) returns (google.protobuf.Empty);
rpc Delete(eventID) returns (google.protobuf.Empty);
rpc List(google.protobuf.Empty) returns (eventList);
rpc GetByID(eventID) returns (eventList);
rpc GetByDay(date) returns (eventList);
rpc GetByWeek(date) returns (eventList);
rpc GetByMonth(date) returns (eventList);
}
message event {
string ID = 1;
string title = 2;
google.protobuf.Timestamp date = 3;
google.protobuf.Duration latency = 4;
string note = 5;
string UserID = 6;
google.protobuf.Duration notyfyTime = 7;
}
message eventList {
repeated event events = 1;
}
message eventID {
string ID = 1;
}
message eventWithID {
string ID = 1;
event event = 2;
}
message date {
google.protobuf.Timestamp date = 1;
}

View File

@ -0,0 +1,31 @@
// Copyright (c) 2015, Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
syntax = "proto3";
package google.api;
import "google/api/http.proto";
import "google/protobuf/descriptor.proto";
option go_package = "google.golang.org/genproto/googleapis/api/annotations;annotations";
option java_multiple_files = true;
option java_outer_classname = "AnnotationsProto";
option java_package = "com.google.api";
option objc_class_prefix = "GAPI";
extend google.protobuf.MethodOptions {
// See `HttpRule`.
HttpRule http = 72295728;
}

View File

@ -0,0 +1,318 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
syntax = "proto3";
package google.api;
option cc_enable_arenas = true;
option go_package = "google.golang.org/genproto/googleapis/api/annotations;annotations";
option java_multiple_files = true;
option java_outer_classname = "HttpProto";
option java_package = "com.google.api";
option objc_class_prefix = "GAPI";
// Defines the HTTP configuration for an API service. It contains a list of
// [HttpRule][google.api.HttpRule], each specifying the mapping of an RPC method
// to one or more HTTP REST API methods.
message Http {
// A list of HTTP configuration rules that apply to individual API methods.
//
// **NOTE:** All service configuration rules follow "last one wins" order.
repeated HttpRule rules = 1;
// When set to true, URL path parmeters will be fully URI-decoded except in
// cases of single segment matches in reserved expansion, where "%2F" will be
// left encoded.
//
// The default behavior is to not decode RFC 6570 reserved characters in multi
// segment matches.
bool fully_decode_reserved_expansion = 2;
}
// `HttpRule` defines the mapping of an RPC method to one or more HTTP
// REST API methods. The mapping specifies how different portions of the RPC
// request message are mapped to URL path, URL query parameters, and
// HTTP request body. The mapping is typically specified as an
// `google.api.http` annotation on the RPC method,
// see "google/api/annotations.proto" for details.
//
// The mapping consists of a field specifying the path template and
// method kind. The path template can refer to fields in the request
// message, as in the example below which describes a REST GET
// operation on a resource collection of messages:
//
//
// service Messaging {
// rpc GetMessage(GetMessageRequest) returns (Message) {
// option (google.api.http).get = "/v1/messages/{message_id}/{sub.subfield}";
// }
// }
// message GetMessageRequest {
// message SubMessage {
// string subfield = 1;
// }
// string message_id = 1; // mapped to the URL
// SubMessage sub = 2; // `sub.subfield` is url-mapped
// }
// message Message {
// string text = 1; // content of the resource
// }
//
// The same http annotation can alternatively be expressed inside the
// `GRPC API Configuration` YAML file.
//
// http:
// rules:
// - selector: <proto_package_name>.Messaging.GetMessage
// get: /v1/messages/{message_id}/{sub.subfield}
//
// This definition enables an automatic, bidrectional mapping of HTTP
// JSON to RPC. Example:
//
// HTTP | RPC
// -----|-----
// `GET /v1/messages/123456/foo` | `GetMessage(message_id: "123456" sub: SubMessage(subfield: "foo"))`
//
// In general, not only fields but also field paths can be referenced
// from a path pattern. Fields mapped to the path pattern cannot be
// repeated and must have a primitive (non-message) type.
//
// Any fields in the request message which are not bound by the path
// pattern automatically become (optional) HTTP query
// parameters. Assume the following definition of the request message:
//
//
// service Messaging {
// rpc GetMessage(GetMessageRequest) returns (Message) {
// option (google.api.http).get = "/v1/messages/{message_id}";
// }
// }
// message GetMessageRequest {
// message SubMessage {
// string subfield = 1;
// }
// string message_id = 1; // mapped to the URL
// int64 revision = 2; // becomes a parameter
// SubMessage sub = 3; // `sub.subfield` becomes a parameter
// }
//
//
// This enables a HTTP JSON to RPC mapping as below:
//
// HTTP | RPC
// -----|-----
// `GET /v1/messages/123456?revision=2&sub.subfield=foo` | `GetMessage(message_id: "123456" revision: 2 sub: SubMessage(subfield: "foo"))`
//
// Note that fields which are mapped to HTTP parameters must have a
// primitive type or a repeated primitive type. Message types are not
// allowed. In the case of a repeated type, the parameter can be
// repeated in the URL, as in `...?param=A&param=B`.
//
// For HTTP method kinds which allow a request body, the `body` field
// specifies the mapping. Consider a REST update method on the
// message resource collection:
//
//
// service Messaging {
// rpc UpdateMessage(UpdateMessageRequest) returns (Message) {
// option (google.api.http) = {
// put: "/v1/messages/{message_id}"
// body: "message"
// };
// }
// }
// message UpdateMessageRequest {
// string message_id = 1; // mapped to the URL
// Message message = 2; // mapped to the body
// }
//
//
// The following HTTP JSON to RPC mapping is enabled, where the
// representation of the JSON in the request body is determined by
// protos JSON encoding:
//
// HTTP | RPC
// -----|-----
// `PUT /v1/messages/123456 { "text": "Hi!" }` | `UpdateMessage(message_id: "123456" message { text: "Hi!" })`
//
// The special name `*` can be used in the body mapping to define that
// every field not bound by the path template should be mapped to the
// request body. This enables the following alternative definition of
// the update method:
//
// service Messaging {
// rpc UpdateMessage(Message) returns (Message) {
// option (google.api.http) = {
// put: "/v1/messages/{message_id}"
// body: "*"
// };
// }
// }
// message Message {
// string message_id = 1;
// string text = 2;
// }
//
//
// The following HTTP JSON to RPC mapping is enabled:
//
// HTTP | RPC
// -----|-----
// `PUT /v1/messages/123456 { "text": "Hi!" }` | `UpdateMessage(message_id: "123456" text: "Hi!")`
//
// Note that when using `*` in the body mapping, it is not possible to
// have HTTP parameters, as all fields not bound by the path end in
// the body. This makes this option more rarely used in practice of
// defining REST APIs. The common usage of `*` is in custom methods
// which don't use the URL at all for transferring data.
//
// It is possible to define multiple HTTP methods for one RPC by using
// the `additional_bindings` option. Example:
//
// service Messaging {
// rpc GetMessage(GetMessageRequest) returns (Message) {
// option (google.api.http) = {
// get: "/v1/messages/{message_id}"
// additional_bindings {
// get: "/v1/users/{user_id}/messages/{message_id}"
// }
// };
// }
// }
// message GetMessageRequest {
// string message_id = 1;
// string user_id = 2;
// }
//
//
// This enables the following two alternative HTTP JSON to RPC
// mappings:
//
// HTTP | RPC
// -----|-----
// `GET /v1/messages/123456` | `GetMessage(message_id: "123456")`
// `GET /v1/users/me/messages/123456` | `GetMessage(user_id: "me" message_id: "123456")`
//
// # Rules for HTTP mapping
//
// The rules for mapping HTTP path, query parameters, and body fields
// to the request message are as follows:
//
// 1. The `body` field specifies either `*` or a field path, or is
// omitted. If omitted, it indicates there is no HTTP request body.
// 2. Leaf fields (recursive expansion of nested messages in the
// request) can be classified into three types:
// (a) Matched in the URL template.
// (b) Covered by body (if body is `*`, everything except (a) fields;
// else everything under the body field)
// (c) All other fields.
// 3. URL query parameters found in the HTTP request are mapped to (c) fields.
// 4. Any body sent with an HTTP request can contain only (b) fields.
//
// The syntax of the path template is as follows:
//
// Template = "/" Segments [ Verb ] ;
// Segments = Segment { "/" Segment } ;
// Segment = "*" | "**" | LITERAL | Variable ;
// Variable = "{" FieldPath [ "=" Segments ] "}" ;
// FieldPath = IDENT { "." IDENT } ;
// Verb = ":" LITERAL ;
//
// The syntax `*` matches a single path segment. The syntax `**` matches zero
// or more path segments, which must be the last part of the path except the
// `Verb`. The syntax `LITERAL` matches literal text in the path.
//
// The syntax `Variable` matches part of the URL path as specified by its
// template. A variable template must not contain other variables. If a variable
// matches a single path segment, its template may be omitted, e.g. `{var}`
// is equivalent to `{var=*}`.
//
// If a variable contains exactly one path segment, such as `"{var}"` or
// `"{var=*}"`, when such a variable is expanded into a URL path, all characters
// except `[-_.~0-9a-zA-Z]` are percent-encoded. Such variables show up in the
// Discovery Document as `{var}`.
//
// If a variable contains one or more path segments, such as `"{var=foo/*}"`
// or `"{var=**}"`, when such a variable is expanded into a URL path, all
// characters except `[-_.~/0-9a-zA-Z]` are percent-encoded. Such variables
// show up in the Discovery Document as `{+var}`.
//
// NOTE: While the single segment variable matches the semantics of
// [RFC 6570](https://tools.ietf.org/html/rfc6570) Section 3.2.2
// Simple String Expansion, the multi segment variable **does not** match
// RFC 6570 Reserved Expansion. The reason is that the Reserved Expansion
// does not expand special characters like `?` and `#`, which would lead
// to invalid URLs.
//
// NOTE: the field paths in variables and in the `body` must not refer to
// repeated fields or map fields.
message HttpRule {
// Selects methods to which this rule applies.
//
// Refer to [selector][google.api.DocumentationRule.selector] for syntax details.
string selector = 1;
// Determines the URL pattern is matched by this rules. This pattern can be
// used with any of the {get|put|post|delete|patch} methods. A custom method
// can be defined using the 'custom' field.
oneof pattern {
// Used for listing and getting information about resources.
string get = 2;
// Used for updating a resource.
string put = 3;
// Used for creating a resource.
string post = 4;
// Used for deleting a resource.
string delete = 5;
// Used for updating a resource.
string patch = 6;
// The custom pattern is used for specifying an HTTP method that is not
// included in the `pattern` field, such as HEAD, or "*" to leave the
// HTTP method unspecified for this rule. The wild-card rule is useful
// for services that provide content to Web (HTML) clients.
CustomHttpPattern custom = 8;
}
// The name of the request field whose value is mapped to the HTTP body, or
// `*` for mapping all fields not captured by the path pattern to the HTTP
// body. NOTE: the referred field must not be a repeated field and must be
// present at the top-level of request message type.
string body = 7;
// Optional. The name of the response field whose value is mapped to the HTTP
// body of response. Other response fields are ignored. When
// not set, the response message will be used as HTTP body of response.
string response_body = 12;
// Additional HTTP bindings for the selector. Nested bindings must
// not contain an `additional_bindings` field themselves (that is,
// the nesting may only be one level deep).
repeated HttpRule additional_bindings = 11;
}
// A custom pattern is used for defining custom HTTP verb.
message CustomHttpPattern {
// The name of this custom HTTP verb.
string kind = 1;
// The path matched by this custom verb.
string path = 2;
}

View File

@ -0,0 +1,78 @@
// Copyright 2018 Google LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
syntax = "proto3";
package google.api;
import "google/protobuf/any.proto";
option cc_enable_arenas = true;
option go_package = "google.golang.org/genproto/googleapis/api/httpbody;httpbody";
option java_multiple_files = true;
option java_outer_classname = "HttpBodyProto";
option java_package = "com.google.api";
option objc_class_prefix = "GAPI";
// Message that represents an arbitrary HTTP body. It should only be used for
// payload formats that can't be represented as JSON, such as raw binary or
// an HTML page.
//
//
// This message can be used both in streaming and non-streaming API methods in
// the request as well as the response.
//
// It can be used as a top-level request field, which is convenient if one
// wants to extract parameters from either the URL or HTTP template into the
// request fields and also want access to the raw HTTP body.
//
// Example:
//
// message GetResourceRequest {
// // A unique request id.
// string request_id = 1;
//
// // The raw HTTP body is bound to this field.
// google.api.HttpBody http_body = 2;
// }
//
// service ResourceService {
// rpc GetResource(GetResourceRequest) returns (google.api.HttpBody);
// rpc UpdateResource(google.api.HttpBody) returns
// (google.protobuf.Empty);
// }
//
// Example with streaming methods:
//
// service CaldavService {
// rpc GetCalendar(stream google.api.HttpBody)
// returns (stream google.api.HttpBody);
// rpc UpdateCalendar(stream google.api.HttpBody)
// returns (stream google.api.HttpBody);
// }
//
// Use of this type only changes how the request and response bodies are
// handled, all other features will continue to work unchanged.
message HttpBody {
// The HTTP Content-Type header value specifying the content type of the body.
string content_type = 1;
// The HTTP request/response body as raw binary.
bytes data = 2;
// Application specific response metadata. Must be set in the first response
// for streaming APIs.
repeated google.protobuf.Any extensions = 3;
}

View File

@ -0,0 +1,76 @@
syntax = "proto3";
import "google/protobuf/timestamp.proto";
import "google/protobuf/duration.proto";
import "google/protobuf/empty.proto";
import "google/api/annotations.proto";
message Event {
string ID = 1;
string Title = 2;
google.protobuf.Timestamp Date = 3;
google.protobuf.Duration Latency = 4;
string Note = 5;
string UserID = 6;
google.protobuf.Duration NotifyTime = 7;
}
message EventList {
repeated Event Events = 1;
}
message EventID {
string ID = 1;
}
message EventWthID {
string ID = 1;
Event Event = 2;
}
enum QueryRange {
DAY = 0;
WEEK = 1;
MONTH = 2;
}
message Date {
google.protobuf.Timestamp Date = 1;
QueryRange Range = 2;
}
service grpc {
rpc Create(Event) returns (EventID) {
option (google.api.http) = {
post: "",
body: "*",
};
}
rpc Update(EventWthID) returns (google.protobuf.Empty) {
option (google.api.http) = {
put: "/{ID}",
body: "*",
};
}
rpc Delete(EventID) returns (google.protobuf.Empty) {
option (google.api.http) = {
delete: "/{ID}"
};
}
rpc List(google.protobuf.Empty) returns (EventList) {
option (google.api.http) = {
get: "/event"
};
}
rpc GetByID(EventID) returns (EventList) {
option (google.api.http) = {
get: "/{ID}"
};
}
rpc GetByDate(Date) returns (EventList) {
option (google.api.http) = {
get: "/{Date}/{Range}"
};
}
}

View File

@ -1,316 +0,0 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// source: grpc/calendar.proto
package event
import (
fmt "fmt"
proto "github.com/golang/protobuf/proto"
duration "github.com/golang/protobuf/ptypes/duration"
_ "github.com/golang/protobuf/ptypes/empty"
timestamp "github.com/golang/protobuf/ptypes/timestamp"
math "math"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
type Event struct {
ID string `protobuf:"bytes,1,opt,name=ID,proto3" json:"ID,omitempty"`
Title string `protobuf:"bytes,2,opt,name=title,proto3" json:"title,omitempty"`
Date *timestamp.Timestamp `protobuf:"bytes,3,opt,name=date,proto3" json:"date,omitempty"`
Latency *duration.Duration `protobuf:"bytes,4,opt,name=latency,proto3" json:"latency,omitempty"`
Note string `protobuf:"bytes,5,opt,name=note,proto3" json:"note,omitempty"`
UserID string `protobuf:"bytes,6,opt,name=UserID,proto3" json:"UserID,omitempty"`
NotyfyTime *duration.Duration `protobuf:"bytes,7,opt,name=notyfyTime,proto3" json:"notyfyTime,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *Event) Reset() { *m = Event{} }
func (m *Event) String() string { return proto.CompactTextString(m) }
func (*Event) ProtoMessage() {}
func (*Event) Descriptor() ([]byte, []int) {
return fileDescriptor_b5d285f4b6d41dfd, []int{0}
}
func (m *Event) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_Event.Unmarshal(m, b)
}
func (m *Event) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_Event.Marshal(b, m, deterministic)
}
func (m *Event) XXX_Merge(src proto.Message) {
xxx_messageInfo_Event.Merge(m, src)
}
func (m *Event) XXX_Size() int {
return xxx_messageInfo_Event.Size(m)
}
func (m *Event) XXX_DiscardUnknown() {
xxx_messageInfo_Event.DiscardUnknown(m)
}
var xxx_messageInfo_Event proto.InternalMessageInfo
func (m *Event) GetID() string {
if m != nil {
return m.ID
}
return ""
}
func (m *Event) GetTitle() string {
if m != nil {
return m.Title
}
return ""
}
func (m *Event) GetDate() *timestamp.Timestamp {
if m != nil {
return m.Date
}
return nil
}
func (m *Event) GetLatency() *duration.Duration {
if m != nil {
return m.Latency
}
return nil
}
func (m *Event) GetNote() string {
if m != nil {
return m.Note
}
return ""
}
func (m *Event) GetUserID() string {
if m != nil {
return m.UserID
}
return ""
}
func (m *Event) GetNotyfyTime() *duration.Duration {
if m != nil {
return m.NotyfyTime
}
return nil
}
type EventList struct {
Events []*Event `protobuf:"bytes,1,rep,name=events,proto3" json:"events,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *EventList) Reset() { *m = EventList{} }
func (m *EventList) String() string { return proto.CompactTextString(m) }
func (*EventList) ProtoMessage() {}
func (*EventList) Descriptor() ([]byte, []int) {
return fileDescriptor_b5d285f4b6d41dfd, []int{1}
}
func (m *EventList) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_EventList.Unmarshal(m, b)
}
func (m *EventList) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_EventList.Marshal(b, m, deterministic)
}
func (m *EventList) XXX_Merge(src proto.Message) {
xxx_messageInfo_EventList.Merge(m, src)
}
func (m *EventList) XXX_Size() int {
return xxx_messageInfo_EventList.Size(m)
}
func (m *EventList) XXX_DiscardUnknown() {
xxx_messageInfo_EventList.DiscardUnknown(m)
}
var xxx_messageInfo_EventList proto.InternalMessageInfo
func (m *EventList) GetEvents() []*Event {
if m != nil {
return m.Events
}
return nil
}
type EventID struct {
ID string `protobuf:"bytes,1,opt,name=ID,proto3" json:"ID,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *EventID) Reset() { *m = EventID{} }
func (m *EventID) String() string { return proto.CompactTextString(m) }
func (*EventID) ProtoMessage() {}
func (*EventID) Descriptor() ([]byte, []int) {
return fileDescriptor_b5d285f4b6d41dfd, []int{2}
}
func (m *EventID) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_EventID.Unmarshal(m, b)
}
func (m *EventID) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_EventID.Marshal(b, m, deterministic)
}
func (m *EventID) XXX_Merge(src proto.Message) {
xxx_messageInfo_EventID.Merge(m, src)
}
func (m *EventID) XXX_Size() int {
return xxx_messageInfo_EventID.Size(m)
}
func (m *EventID) XXX_DiscardUnknown() {
xxx_messageInfo_EventID.DiscardUnknown(m)
}
var xxx_messageInfo_EventID proto.InternalMessageInfo
func (m *EventID) GetID() string {
if m != nil {
return m.ID
}
return ""
}
type EventWithID struct {
ID string `protobuf:"bytes,1,opt,name=ID,proto3" json:"ID,omitempty"`
Event *Event `protobuf:"bytes,2,opt,name=event,proto3" json:"event,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *EventWithID) Reset() { *m = EventWithID{} }
func (m *EventWithID) String() string { return proto.CompactTextString(m) }
func (*EventWithID) ProtoMessage() {}
func (*EventWithID) Descriptor() ([]byte, []int) {
return fileDescriptor_b5d285f4b6d41dfd, []int{3}
}
func (m *EventWithID) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_EventWithID.Unmarshal(m, b)
}
func (m *EventWithID) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_EventWithID.Marshal(b, m, deterministic)
}
func (m *EventWithID) XXX_Merge(src proto.Message) {
xxx_messageInfo_EventWithID.Merge(m, src)
}
func (m *EventWithID) XXX_Size() int {
return xxx_messageInfo_EventWithID.Size(m)
}
func (m *EventWithID) XXX_DiscardUnknown() {
xxx_messageInfo_EventWithID.DiscardUnknown(m)
}
var xxx_messageInfo_EventWithID proto.InternalMessageInfo
func (m *EventWithID) GetID() string {
if m != nil {
return m.ID
}
return ""
}
func (m *EventWithID) GetEvent() *Event {
if m != nil {
return m.Event
}
return nil
}
type Date struct {
Date *timestamp.Timestamp `protobuf:"bytes,1,opt,name=date,proto3" json:"date,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *Date) Reset() { *m = Date{} }
func (m *Date) String() string { return proto.CompactTextString(m) }
func (*Date) ProtoMessage() {}
func (*Date) Descriptor() ([]byte, []int) {
return fileDescriptor_b5d285f4b6d41dfd, []int{4}
}
func (m *Date) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_Date.Unmarshal(m, b)
}
func (m *Date) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_Date.Marshal(b, m, deterministic)
}
func (m *Date) XXX_Merge(src proto.Message) {
xxx_messageInfo_Date.Merge(m, src)
}
func (m *Date) XXX_Size() int {
return xxx_messageInfo_Date.Size(m)
}
func (m *Date) XXX_DiscardUnknown() {
xxx_messageInfo_Date.DiscardUnknown(m)
}
var xxx_messageInfo_Date proto.InternalMessageInfo
func (m *Date) GetDate() *timestamp.Timestamp {
if m != nil {
return m.Date
}
return nil
}
func init() {
proto.RegisterType((*Event)(nil), "event.event")
proto.RegisterType((*EventList)(nil), "event.eventList")
proto.RegisterType((*EventID)(nil), "event.eventID")
proto.RegisterType((*EventWithID)(nil), "event.eventWithID")
proto.RegisterType((*Date)(nil), "event.date")
}
func init() { proto.RegisterFile("grpc/calendar.proto", fileDescriptor_b5d285f4b6d41dfd) }
var fileDescriptor_b5d285f4b6d41dfd = []byte{
// 429 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x92, 0xdf, 0x8b, 0xd3, 0x40,
0x10, 0xc7, 0x49, 0x7f, 0x24, 0x76, 0x22, 0x87, 0x8c, 0x72, 0xec, 0x45, 0xd0, 0x12, 0x44, 0xaa,
0x07, 0xa9, 0xf6, 0x44, 0xf0, 0xd1, 0xeb, 0x8a, 0x04, 0x14, 0x24, 0x78, 0x1c, 0xf8, 0x96, 0x6b,
0xe7, 0x7a, 0xc1, 0x34, 0x1b, 0xd2, 0x39, 0x21, 0x6f, 0xfe, 0xdb, 0xbe, 0x49, 0x66, 0x53, 0xa9,
0xa9, 0x5a, 0x5f, 0xc2, 0xce, 0x7e, 0x3f, 0x3b, 0xdf, 0x9d, 0x6f, 0x16, 0xee, 0xaf, 0xaa, 0x72,
0x31, 0x5d, 0xa4, 0x39, 0x15, 0xcb, 0xb4, 0x8a, 0xca, 0xca, 0xb0, 0xc1, 0x21, 0x7d, 0xa3, 0x82,
0x83, 0xc7, 0x2b, 0x63, 0x56, 0x39, 0x4d, 0x65, 0xf3, 0xea, 0xf6, 0x7a, 0xca, 0xd9, 0x9a, 0x36,
0x9c, 0xae, 0x4b, 0xcb, 0x05, 0x8f, 0xba, 0xc0, 0xf2, 0xb6, 0x4a, 0x39, 0x33, 0x45, 0xab, 0x3f,
0xec, 0xea, 0xb4, 0x2e, 0xb9, 0xb6, 0x62, 0xf8, 0xc3, 0x01, 0xeb, 0x83, 0x47, 0xd0, 0x8b, 0xb5,
0x72, 0xc6, 0xce, 0x64, 0x94, 0xf4, 0x62, 0x8d, 0x0f, 0x60, 0xc8, 0x19, 0xe7, 0xa4, 0x7a, 0xb2,
0x65, 0x0b, 0x8c, 0x60, 0xb0, 0x4c, 0x99, 0x54, 0x7f, 0xec, 0x4c, 0xfc, 0x59, 0x10, 0xd9, 0xde,
0xd1, 0xb6, 0x77, 0xf4, 0x79, 0x7b, 0xb9, 0x44, 0x38, 0x3c, 0x03, 0x2f, 0x4f, 0x99, 0x8a, 0x45,
0xad, 0x06, 0x72, 0xe4, 0x64, 0xef, 0x88, 0x6e, 0xaf, 0x9b, 0x6c, 0x49, 0x44, 0x18, 0x14, 0x86,
0x49, 0x0d, 0xc5, 0x59, 0xd6, 0x78, 0x0c, 0xee, 0xc5, 0x86, 0xaa, 0x58, 0x2b, 0x57, 0x76, 0xdb,
0x0a, 0xdf, 0x00, 0x14, 0x86, 0xeb, 0xeb, 0xba, 0x71, 0x56, 0xde, 0x21, 0x8f, 0x1d, 0x38, 0x7c,
0x09, 0x23, 0x19, 0xfd, 0x43, 0xb6, 0x61, 0x7c, 0x02, 0xae, 0x14, 0x1b, 0xe5, 0x8c, 0xfb, 0x13,
0x7f, 0x76, 0x37, 0x92, 0xd2, 0x7e, 0x93, 0x56, 0x0b, 0x4f, 0xc0, 0x93, 0x55, 0xac, 0xbb, 0x79,
0x85, 0x6f, 0xc1, 0x17, 0xe9, 0x32, 0xe3, 0x9b, 0x7d, 0x19, 0xc3, 0x36, 0x67, 0x89, 0xb3, 0xdb,
0xde, 0x4a, 0xe1, 0x6b, 0x1b, 0xee, 0xaf, 0x90, 0x9d, 0xff, 0x0b, 0x79, 0xf6, 0xbd, 0x0f, 0xfe,
0xbc, 0x7d, 0x3c, 0xc9, 0xa7, 0x39, 0x3e, 0x05, 0x77, 0x5e, 0x51, 0xd3, 0xe9, 0x37, 0x9b, 0xe0,
0x68, 0xb7, 0x8a, 0x35, 0xbe, 0x02, 0xf7, 0xa2, 0x14, 0x47, 0xdc, 0x55, 0xec, 0x04, 0xc1, 0xf1,
0x9e, 0xef, 0xbb, 0xe6, 0xe1, 0xe0, 0x0b, 0x70, 0x35, 0xe5, 0xc4, 0x84, 0x9d, 0x7e, 0xff, 0x38,
0x31, 0x90, 0x8c, 0xff, 0xa2, 0x07, 0xf7, 0x76, 0xfb, 0x08, 0x79, 0x0a, 0xde, 0x7b, 0xe2, 0xf3,
0xba, 0x09, 0xb2, 0x63, 0xb2, 0x0f, 0x3f, 0x83, 0x3b, 0x02, 0xeb, 0xb4, 0x46, 0xbf, 0x55, 0x9b,
0xa9, 0xfe, 0x80, 0x3e, 0x87, 0x91, 0xa0, 0x97, 0x44, 0x5f, 0x0f, 0xb1, 0xa7, 0x00, 0xc2, 0x7e,
0x34, 0x05, 0xdf, 0x1c, 0x80, 0xcf, 0xbd, 0x2f, 0xf6, 0x1f, 0x5e, 0xb9, 0x32, 0xdb, 0xd9, 0xcf,
0x00, 0x00, 0x00, 0xff, 0xff, 0xb3, 0xa8, 0x5f, 0x40, 0xd3, 0x03, 0x00, 0x00,
}

View File

@ -0,0 +1,16 @@
package grpc
import (
"context"
"github.com/golang/protobuf/ptypes/empty"
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/grpc"
)
type Service struct{}
func (s Service) Create(context.Context, *grpc.Event) (*grpc.EventID, error) {}
func (s Service) Update(context.Context, *grpc.EventWthID) (*empty.Empty, error) {}
func (s Service) Delete(context.Context, *grpc.EventID) (*empty.Empty, error) {}
func (s Service) List(context.Context, *empty.Empty) (*grpc.EventList, error) {}
func (s Service) GetByID(context.Context, *grpc.EventID) (*grpc.EventList, error) {}
func (s Service) GetByDate(context.Context, *grpc.Date) (*grpc.EventList, error) {}

View File

@ -2,6 +2,7 @@ package memory
import (
"sync"
"time"
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/storage/event"
)
@ -48,3 +49,31 @@ func (s *Storage) GetByID(id event.ID) (event.Event, bool) {
}
return s.Events[id], true
}
func (s *Storage) GetByDate(startDate time.Time, rng string) (map[event.ID]event.Event, error) {
endDate := getEndDate(startDate, rng)
s.Mu.Lock()
defer s.Mu.Unlock()
res := make(map[event.ID]event.Event)
for k, v := range s.Events {
if (v.Date.After(startDate) && v.Date.Before(endDate)) ||
(v.Date.Add(v.Latency).Before(v.Date) && v.Date.Add(v.Latency).After(v.Date)) ||
(v.Date.Before(startDate)) && (v.Date.Add(v.Latency).After(endDate)) {
res[k] = v
}
}
return res, nil
}
func getEndDate(startDate time.Time, rng string) time.Time {
switch rng {
case "DAY":
return startDate.AddDate(0, 0, 1)
case "WEEK":
return startDate.AddDate(0, 0, 7)
case "MONTH":
return startDate.AddDate(0, 1, 0)
default:
return startDate
}
}

View File

@ -123,3 +123,51 @@ func (s *Storage) GetByID(id event.ID) (event.Event, bool) {
res.Date = dateParced
return res, true
}
func (s *Storage) GetByDate(startDate time.Time, rng string) (map[event.ID]event.Event, error) {
res := make(map[event.ID]event.Event)
results, err := s.db.Query(
`SELECT (id,title,date,latency,note,userID,notifyTime)
from events
where (date>$1 AND date<$2) OR
(date+latency>$1 AND date+latency<$2) OR
(date<$1 AND date+latency>$2)
ORDER BY id`,
startDate,
getEndDate(startDate, rng))
if err != nil {
return nil, err
}
defer results.Close()
for results.Next() {
var id event.ID
var evt event.Event
var dateRaw string
err = results.Scan(&id, &evt.Title, &dateRaw, &evt.Latency, &evt.Note, &evt.UserID, &evt.NotifyTime)
if err != nil {
return nil, err
}
evt.Date, err = time.Parse(dateTimeLayout, dateRaw)
if err != nil {
return nil, err
}
res[id] = evt
}
if results.Err() != nil {
return nil, results.Err()
}
return res, nil
}
func getEndDate(startDate time.Time, rng string) time.Time {
switch rng {
case "DAY":
return startDate.AddDate(0, 0, 1)
case "WEEK":
return startDate.AddDate(0, 0, 7)
case "MONTH":
return startDate.AddDate(0, 1, 0)
default:
return startDate
}
}

View File

@ -4,6 +4,7 @@ import (
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/storage/event"
memorystorage "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/storage/memory"
sqlstorage "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/storage/sql"
"time"
)
type Config struct {
@ -16,11 +17,12 @@ type Config struct {
}
type StorageInterface interface {
Create(event event.Event) (event.ID, error)
Update(id event.ID, event event.Event) error
Delete(id event.ID) error
Create(event.Event) (event.ID, error)
Update(event.ID, event.Event) error
Delete(event.ID) error
List() (map[event.ID]event.Event, error)
GetByID(id event.ID) (event.Event, bool)
GetByID(event.ID) (event.Event, bool)
GetByDate(time.Time, string) (map[event.ID]event.Event, error)
}
func NewStore(conf Config) StorageInterface {