Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "flag" |
| 6 | |
| 7 | "github.com/golang/glog" |
| 8 | "google.golang.org/grpc/codes" |
| 9 | "google.golang.org/grpc/status" |
| 10 | |
Sergiusz Bazanski | 1446e98 | 2019-07-21 16:04:23 +0200 | [diff] [blame] | 11 | pb "code.hackerspace.pl/hscloud/bgpwtf/invoice/proto" |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 12 | "code.hackerspace.pl/hscloud/go/mirko" |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 13 | ) |
| 14 | |
| 15 | var ( |
| 16 | flagDatabasePath string |
| 17 | flagInit bool |
| 18 | flagDisablePKI bool |
| 19 | ) |
| 20 | |
| 21 | type service struct { |
| 22 | m *model |
| 23 | } |
| 24 | |
| 25 | func (s *service) CreateInvoice(ctx context.Context, req *pb.CreateInvoiceRequest) (*pb.CreateInvoiceResponse, error) { |
Serge Bazanski | 220c5d2 | 2020-11-16 22:04:23 +0100 | [diff] [blame] | 26 | if err := validateInvoiceData(req.InvoiceData); err != nil { |
| 27 | return nil, status.Errorf(codes.InvalidArgument, "invoice data: %v", err) |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 28 | } |
| 29 | |
Sergiusz Bazanski | 3976e3c | 2019-05-01 15:27:49 +0200 | [diff] [blame] | 30 | uid, err := s.m.createInvoice(ctx, req.InvoiceData) |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 31 | if err != nil { |
| 32 | if _, ok := status.FromError(err); ok { |
| 33 | return nil, err |
| 34 | } |
| 35 | glog.Errorf("createInvoice(_, _): %v", err) |
| 36 | return nil, status.Error(codes.Unavailable, "could not create invoice") |
| 37 | } |
| 38 | return &pb.CreateInvoiceResponse{ |
| 39 | Uid: uid, |
| 40 | }, nil |
| 41 | } |
| 42 | |
| 43 | func (s *service) GetInvoice(ctx context.Context, req *pb.GetInvoiceRequest) (*pb.GetInvoiceResponse, error) { |
| 44 | invoice, err := s.m.getInvoice(ctx, req.Uid) |
| 45 | if err != nil { |
| 46 | if _, ok := status.FromError(err); ok { |
| 47 | return nil, err |
| 48 | } |
| 49 | glog.Errorf("getInvoice(_, %q): %v", req.Uid, err) |
| 50 | return nil, status.Error(codes.Unavailable, "internal server error") |
| 51 | } |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 52 | res := &pb.GetInvoiceResponse{ |
| 53 | Invoice: invoice, |
| 54 | } |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 55 | return res, nil |
| 56 | } |
| 57 | |
| 58 | func newService(m *model) *service { |
| 59 | return &service{ |
| 60 | m: m, |
| 61 | } |
| 62 | } |
| 63 | |
Sergiusz Bazanski | a818ef2 | 2019-06-07 10:37:22 +0200 | [diff] [blame] | 64 | func (s *service) invoicePDF(ctx context.Context, uid, language string) ([]byte, error) { |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 65 | sealed, err := s.m.getSealedUid(ctx, uid) |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 66 | if err != nil { |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 67 | return nil, err |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | var rendered []byte |
| 71 | if sealed != "" { |
| 72 | // Invoice is sealed, return stored PDF. |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 73 | rendered, err = s.m.getRendered(ctx, uid) |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 74 | if err != nil { |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 75 | return nil, err |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 76 | } |
| 77 | } else { |
| 78 | // Invoice is proforma, render. |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 79 | invoice, err := s.m.getInvoice(ctx, uid) |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 80 | if err != nil { |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 81 | return nil, err |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Sergiusz Bazanski | a818ef2 | 2019-06-07 10:37:22 +0200 | [diff] [blame] | 84 | rendered, err = renderInvoicePDF(invoice, language) |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 85 | if err != nil { |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 86 | return nil, err |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 87 | } |
| 88 | } |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 89 | return rendered, nil |
| 90 | } |
| 91 | |
| 92 | func (s *service) RenderInvoice(req *pb.RenderInvoiceRequest, srv pb.Invoicer_RenderInvoiceServer) error { |
Sergiusz Bazanski | a818ef2 | 2019-06-07 10:37:22 +0200 | [diff] [blame] | 93 | rendered, err := s.invoicePDF(srv.Context(), req.Uid, req.Language) |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 94 | if err != nil { |
| 95 | if _, ok := status.FromError(err); ok { |
| 96 | return err |
| 97 | } |
| 98 | glog.Errorf("invoicePDF(_, %q): %v", req.Uid, err) |
| 99 | return status.Error(codes.Unavailable, "internal server error") |
| 100 | } |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 101 | |
| 102 | chunkSize := 16 * 1024 |
| 103 | chunk := &pb.RenderInvoiceResponse{} |
| 104 | for i := 0; i < len(rendered); i += chunkSize { |
| 105 | if i+chunkSize > len(rendered) { |
| 106 | chunk.Data = rendered[i:len(rendered)] |
| 107 | } else { |
| 108 | chunk.Data = rendered[i : i+chunkSize] |
| 109 | } |
| 110 | if err := srv.Send(chunk); err != nil { |
| 111 | glog.Errorf("srv.Send: %v", err) |
| 112 | return status.Error(codes.Unavailable, "stream broken") |
| 113 | } |
| 114 | } |
| 115 | return nil |
| 116 | } |
| 117 | |
| 118 | func (s *service) SealInvoice(ctx context.Context, req *pb.SealInvoiceRequest) (*pb.SealInvoiceResponse, error) { |
Sergiusz Bazanski | a818ef2 | 2019-06-07 10:37:22 +0200 | [diff] [blame] | 119 | useProformaTime := false |
| 120 | if req.DateSource == pb.SealInvoiceRequest_DATE_SOURCE_PROFORMA { |
| 121 | useProformaTime = true |
| 122 | } |
| 123 | if err := s.m.sealInvoice(ctx, req.Uid, req.Language, useProformaTime); err != nil { |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 124 | if _, ok := status.FromError(err); ok { |
| 125 | return nil, err |
| 126 | } |
| 127 | glog.Errorf("sealInvoice(_, %q): %v", req.Uid, err) |
| 128 | return nil, status.Error(codes.Unavailable, "internal server error") |
| 129 | } |
| 130 | return &pb.SealInvoiceResponse{}, nil |
| 131 | } |
| 132 | |
Serge Bazanski | 1ecf22d | 2020-11-16 22:59:33 +0100 | [diff] [blame] | 133 | func (s *service) GetInvoices(req *pb.GetInvoicesRequest, srv pb.Invoicer_GetInvoicesServer) error { |
| 134 | return status.Error(codes.Unimplemented, "unimplemented") |
| 135 | } |
| 136 | |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 137 | func init() { |
| 138 | flag.Set("logtostderr", "true") |
| 139 | } |
| 140 | |
| 141 | func main() { |
| 142 | flag.StringVar(&flagDatabasePath, "db_path", "./foo.db", "path to sqlite database") |
| 143 | flag.BoolVar(&flagInit, "init_db", false, "init database and exit") |
| 144 | flag.Parse() |
| 145 | |
| 146 | m, err := newModel(flagDatabasePath) |
| 147 | if err != nil { |
| 148 | glog.Exitf("newModel: %v", err) |
| 149 | } |
| 150 | if flagInit { |
| 151 | glog.Exit(m.init()) |
| 152 | } |
| 153 | |
| 154 | mi := mirko.New() |
| 155 | if err := mi.Listen(); err != nil { |
| 156 | glog.Exitf("Listen failed: %v", err) |
| 157 | } |
| 158 | |
| 159 | s := newService(m) |
Sergiusz Bazanski | 57ef6b0 | 2019-05-01 14:08:29 +0200 | [diff] [blame] | 160 | s.setupStatusz(mi) |
Sergiusz Bazanski | fb18c99 | 2019-05-01 12:27:03 +0200 | [diff] [blame] | 161 | pb.RegisterInvoicerServer(mi.GRPC(), s) |
| 162 | |
| 163 | if err := mi.Serve(); err != nil { |
| 164 | glog.Exitf("Serve failed: %v", err) |
| 165 | } |
| 166 | |
| 167 | <-mi.Done() |
| 168 | } |