-
Notifications
You must be signed in to change notification settings - Fork 0
/
preview_service.go
212 lines (188 loc) · 6.5 KB
/
preview_service.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package document
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"reflect"
"strconv"
"strings"
"sync"
"time"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
apiProtobuf "github.com/kinneko-de/api-contract/golang/kinnekode/protobuf"
apiRestaurantDocument "github.com/kinneko-de/api-contract/golang/kinnekode/restaurant/document/v1"
"github.com/kinneko-de/restaurant-document-design-gateway/internal/app/operation/logger"
"github.com/kinneko-de/restaurant-document-design-gateway/internal/httpheader"
"golang.org/x/time/rate"
"google.golang.org/protobuf/types/known/timestamppb"
github "github.com/kinneko-de/restaurant-document-design-gateway/internal/app/github/oauth"
)
var (
documentServiceGateway DocumentServiceGateway = DocumentServiceGateKeeper{}
rateLimiters sync.Map
)
const FileDownloadTimeout = time.Duration(10 * time.Minute)
type GeneratePreviewRequest struct {
}
func GeneratePreview(ctx *gin.Context) {
var request GeneratePreviewRequest
if err := ctx.BindJSON(&request); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "request can not be parsed"})
return
}
tryToGeneratePreview(ctx)
}
func GeneratePreviewDemo(ctx *gin.Context) {
tryToGeneratePreview(ctx)
}
func tryToGeneratePreview(ctx *gin.Context) {
UserContextKeyValue := ctx.Keys[github.UserContextKey]
if UserContextKeyValue == nil {
ctx.JSON(http.StatusUnauthorized, gin.H{"error": "user email is not set"})
return
}
userEmail := UserContextKeyValue.(string)
if requestIsLimited(userEmail) {
ctx.JSON(http.StatusTooManyRequests, gin.H{"error": fmt.Sprintf("rate limit for %s exceeded. try again later", userEmail)})
return
}
previewRequest := generateTestDocument()
err := generatePreview(ctx, previewRequest)
if err != nil {
logger.Logger.Error().Err(err).
Str("apiDocumentServiceUrl", apiDocumentServiceUrl).
Msg("Failed to generate preview")
return
}
}
func requestIsLimited(userId string) bool {
rateLimiter, _ := rateLimiters.LoadOrStore(userId, createRateLimiter())
return !rateLimiter.(*rate.Limiter).Allow()
}
func createRateLimiter() *rate.Limiter {
rateLimiter := rate.NewLimiter(rate.Every(20*time.Minute), 3)
return rateLimiter
}
func generatePreview(ctx *gin.Context, previewRequest *apiRestaurantDocument.GeneratePreviewRequest) error {
fileName := strings.ReplaceAll(uuid.New().String(), "-", "")
client, err := documentServiceGateway.CreateDocumentServiceClient()
if err != nil {
ctx.AbortWithStatus(http.StatusServiceUnavailable)
return err
}
callContext, cancelFunc := context.WithTimeout(context.Background(), FileDownloadTimeout)
defer cancelFunc()
stream, err := client.GeneratePreview(callContext, previewRequest)
if err != nil {
ctx.AbortWithStatus(http.StatusServiceUnavailable)
return err
}
firstResponse, err := stream.Recv()
if err != nil {
ctx.AbortWithStatus(http.StatusInternalServerError)
return err
}
_, ok := firstResponse.File.(*apiRestaurantDocument.GeneratePreviewResponse_Metadata)
if !ok {
err = errors.New("FileCase of type 'apidocument.DownloadDocumentResponse_Metadata' expected. Actual value is " + reflect.TypeOf(firstResponse.File).String() + ".")
ctx.AbortWithStatus(http.StatusInternalServerError)
return err
}
var metadata = firstResponse.GetMetadata()
ctx.Header(httpheader.ContentType, metadata.MediaType)
ctx.Header(httpheader.ContentLength, strconv.FormatUint(metadata.Size, 10))
ctx.Header(httpheader.ContentDisposition, fmt.Sprintf("inline; filename=\"%s%s\"", fileName, metadata.Extension))
ctx.Status(http.StatusOK)
err = writeResponse(stream, ctx)
return err
}
func writeResponse(stream apiRestaurantDocument.DocumentService_GeneratePreviewClient, ctx *gin.Context) error {
for {
current, done, err := readNextResponse(stream)
if done {
break
}
if err != nil {
ctx.AbortWithStatus(http.StatusInternalServerError)
return err
}
if somethingElseThanChunkWasSent(current) {
ctx.AbortWithStatus(http.StatusInternalServerError)
return fmt.Errorf("FileCase of type 'apiDocumentService.GeneratePreviewResponse_Chunk' expected. Actual value is %s", reflect.TypeOf(current.File).String())
}
var chunk = current.GetChunk()
_, err = ctx.Writer.Write(chunk)
if err != nil {
ctx.AbortWithStatus(http.StatusInternalServerError)
return err
}
}
return nil
}
func generateTestDocument() *apiRestaurantDocument.GeneratePreviewRequest {
previewRequest := &apiRestaurantDocument.GeneratePreviewRequest{
RequestedDocument: &apiRestaurantDocument.RequestedDocument{
Type: &apiRestaurantDocument.RequestedDocument_Invoice{
Invoice: &apiRestaurantDocument.Invoice{
DeliveredOn: timestamppb.New(time.Date(2020, time.April, 13, 0, 0, 0, 0, time.UTC)),
CurrencyCode: "EUR",
Recipient: &apiRestaurantDocument.Invoice_Recipient{
Name: "Max Mustermann",
Street: "Musterstraße 17",
City: "Musterstadt",
PostCode: "12345",
Country: "DE",
},
Items: []*apiRestaurantDocument.Invoice_Item{
{
Description: "Spitzenunterwäsche\r\nANS 23054303053",
Quantity: 2,
NetAmount: &apiProtobuf.Decimal{Value: "3.35"},
Taxation: &apiProtobuf.Decimal{Value: "19"},
TotalAmount: &apiProtobuf.Decimal{Value: "3.99"},
Sum: &apiProtobuf.Decimal{Value: "7.98"},
},
{
Description: "Schlabberhose (10% reduziert)\r\nANS 606406540",
Quantity: 1,
NetAmount: &apiProtobuf.Decimal{Value: "9.07"},
Taxation: &apiProtobuf.Decimal{Value: "19"},
TotalAmount: &apiProtobuf.Decimal{Value: "10.79"},
Sum: &apiProtobuf.Decimal{Value: "10.79"},
},
{
Description: "Versandkosten",
Quantity: 1,
NetAmount: &apiProtobuf.Decimal{Value: "0.00"},
Taxation: &apiProtobuf.Decimal{Value: "0"},
TotalAmount: &apiProtobuf.Decimal{Value: "0.00"},
Sum: &apiProtobuf.Decimal{Value: "0.00"},
},
},
},
},
},
}
return previewRequest
}
func somethingElseThanChunkWasSent(current *apiRestaurantDocument.GeneratePreviewResponse) bool {
_, ok := current.File.(*apiRestaurantDocument.GeneratePreviewResponse_Chunk)
return !ok
}
func readNextResponse(stream apiRestaurantDocument.DocumentService_GeneratePreviewClient) (*apiRestaurantDocument.GeneratePreviewResponse, bool, error) {
current, err := stream.Recv()
if err == io.EOF {
err := stream.CloseSend()
if err != nil {
return nil, true, err
}
return nil, true, nil
}
if err != nil {
return nil, false, err
}
return current, false, nil
}