diff --git a/.gitignore b/.gitignore index ddd48d8..895c6aa 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ cred.txt dist/ .air.toml tmp/ +invoices/ diff --git a/Makefile b/Makefile index ce92185..9980e91 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ API_PORT=4001 DSN=vinchent:secret@tcp(localhost:3306)/widgets?parseTime=true&tls=false ## build: builds all binaries -build: clean build_front build_back +build: clean build_front build_back build_invoice @printf "All binaries built!\n" ## clean: cleans all binaries and runs go clean @@ -15,6 +15,12 @@ clean: @go clean @echo "Cleaned!" +## build_invoice: builds the invoice microservice +build_invoice: + @echo "Building invoice microservice..." + @go build -o dist/invoice ./cmd/micro/invoice + @echo "Invoice microservice built!" + ## build_front: builds the front end build_front: @echo "Building front end..." @@ -28,8 +34,14 @@ build_back: @echo "Back end built!" ## start: starts front and back end -start: start_front start_back +start: start_front start_back start_invoice +## start_invoice: starts the invoice microservice +start_invoice: build_invoice + @echo "Starting the invoice microservice..." + @./dist/invoice & + @echo "invoice microservice running!" + ## start_front: starts the front end start_front: build_front @echo "Starting the front end..." @@ -43,9 +55,15 @@ start_back: build_back @echo "Back end running!" ## stop: stops the front and back end -stop: stop_front stop_back +stop: stop_front stop_back stop_invoice @echo "All applications stopped" +## stop_invoice: stops the invoice microservice +stop_invoice: + @echo "Stopping the invoice microservice..." + @-pkill -SIGTERM -f "invoice" + @echo "Stopped invoice microservice" + ## stop_front: stops the front end stop_front: @echo "Stopping the front end..." diff --git a/cmd/api/handlers-api.go b/cmd/api/handlers-api.go index 2994062..ed304d1 100644 --- a/cmd/api/handlers-api.go +++ b/cmd/api/handlers-api.go @@ -33,7 +33,7 @@ type stripePayload struct { LastName string `json:"last_name"` } -type jsonResponse struct { +type JSONResponse struct { OK bool `json:"ok"` Message string `json:"message,omitempty"` Content string `json:"content,omitempty"` @@ -63,7 +63,7 @@ func (app *application) GetPaymentIntent(w http.ResponseWriter, r *http.Request) pi, msg, err := card.Charge(payload.Currency, amount) if err != nil { - j := jsonResponse{ + j := JSONResponse{ OK: false, Message: msg, Content: "", @@ -190,7 +190,7 @@ func (app *application) CreateCustomerAndSubscribeToPlan(w http.ResponseWriter, } } - resp := jsonResponse{ + resp := JSONResponse{ OK: okay, Message: txnMsg, } @@ -340,7 +340,7 @@ func (app *application) CheckAuthentication(w http.ResponseWriter, r *http.Reque } // valid user - var payload jsonResponse + var payload JSONResponse payload.OK = true payload.Message = fmt.Sprintf("authenticated user %s", user.Email) app.writeJSON(w, http.StatusOK, payload) @@ -423,7 +423,7 @@ func (app *application) SendPasswordResetEmail(w http.ResponseWriter, r *http.Re // verify that email exists _, err = app.DB.GetUserByEmail(payload.Email) if err != nil { - resp := jsonResponse{ + resp := JSONResponse{ OK: false, Message: "No matching email found on our system", } @@ -458,7 +458,7 @@ func (app *application) SendPasswordResetEmail(w http.ResponseWriter, r *http.Re return } - resp := jsonResponse{ + resp := JSONResponse{ OK: true, } @@ -509,7 +509,7 @@ func (app *application) ResetPassword(w http.ResponseWriter, r *http.Request) { return } - resp := jsonResponse{ + resp := JSONResponse{ OK: true, Message: "Password reset.", } @@ -649,7 +649,7 @@ func (app *application) RefundCharge(w http.ResponseWriter, r *http.Request) { return } - var resp jsonResponse + var resp JSONResponse resp.OK = true resp.Message = "Charge refunded" @@ -696,7 +696,7 @@ func (app *application) CancelSubscription(w http.ResponseWriter, r *http.Reques return } - var resp jsonResponse + var resp JSONResponse resp.OK = true resp.Message = "Subscription canceled" @@ -775,7 +775,7 @@ func (app *application) EditUser(w http.ResponseWriter, r *http.Request) { } } - var resp jsonResponse + var resp JSONResponse resp.OK = true app.writeJSON(w, http.StatusOK, resp) } @@ -789,7 +789,7 @@ func (app *application) DeleteUser(w http.ResponseWriter, r *http.Request) { app.badRequest(w, r, err) return } - var resp jsonResponse + var resp JSONResponse resp.OK = true app.writeJSON(w, http.StatusOK, resp) } diff --git a/cmd/micro/invoice/helpers.go b/cmd/micro/invoice/helpers.go new file mode 100644 index 0000000..cb02703 --- /dev/null +++ b/cmd/micro/invoice/helpers.go @@ -0,0 +1,86 @@ +package main + +import ( + "encoding/json" + "errors" + "io" + "net/http" + "os" +) + +type JSONResponse struct { + OK bool `json:"ok"` + Message string `json:"message,omitempty"` +} + +func (app *application) readJSON(w http.ResponseWriter, r *http.Request, data interface{}) error { + maxBytes := 1048576 + + r.Body = http.MaxBytesReader(w, r.Body, int64(maxBytes)) + + dec := json.NewDecoder(r.Body) + err := dec.Decode(data) + if err != nil { + return err + } + + // Make sure there is only one entry. + err = dec.Decode(&struct{}{}) + if err != io.EOF { + return errors.New("body must only have a single JSON value") + } + + return nil +} + +// writeJSON writes arbitrary data out as JSON +func (app *application) writeJSON( + w http.ResponseWriter, + status int, data interface{}, + headers ...http.Header, +) error { + out, err := json.MarshalIndent(data, "", "\t") + if err != nil { + return err + } + + if len(headers) > 0 { + for k, v := range headers[0] { + w.Header()[k] = v + } + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(status) + w.Write(out) + return nil +} + +func (app *application) badRequest(w http.ResponseWriter, r *http.Request, err error) error { + var payload JSONResponse + + payload.OK = false + payload.Message = err.Error() + + out, err := json.MarshalIndent(payload, "", "\t") + if err != nil { + return err + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusBadGateway) + w.Write(out) + return nil +} + +func (app *application) CreateDirIfNotExist(path string) error { + const mode = 0755 + if _, err := os.Stat(path); os.IsNotExist(err) { + err := os.Mkdir(path, mode) + if err != nil { + app.errorLog.Println(err) + return err + } + } + return nil +} diff --git a/cmd/micro/invoice/invoice-handlers.go b/cmd/micro/invoice/invoice-handlers.go new file mode 100644 index 0000000..0856435 --- /dev/null +++ b/cmd/micro/invoice/invoice-handlers.go @@ -0,0 +1,145 @@ +package main + +import ( + "fmt" + "net/http" + "time" + + "github.com/phpdave11/gofpdf" + "github.com/phpdave11/gofpdf/contrib/gofpdi" +) + +type Order struct { + ID int `json:"id"` + Quantity int `json:"quantity"` + Amount int `json:"amount"` + Product string `json:"product"` + FirstName string `json:"first_name"` + LastName string `json:"last_name"` + Email string `json:"email"` + CreatedAt time.Time `json:"-"` +} + +func (app *application) CreateAndSendInvoice(w http.ResponseWriter, r *http.Request) { + // receive json + var order Order + + err := app.readJSON(w, r, &order) + if err != nil { + app.badRequest(w, r, err) + return + } + + // generate a pdf invoice + + // create mail + + // send mail with attachment + + // send response + var resp JSONResponse + resp.OK = true + resp.Message = fmt.Sprintf("Invoice %d.pdf created and sent to %s", order.ID, order.Email) + app.writeJSON(w, http.StatusCreated, resp) +} + +func (app *application) createInvoicePDF(order Order) error { + pdf := gofpdf.New("P", "mm", "Letter", "") + pdf.SetMargins(10, 13, 10) + pdf.SetAutoPageBreak(true, 0) + + importer := gofpdi.NewImporter() + + t := importer.ImportPage(pdf, "./pdf-templates/invoice.pdf", q, "/MediaBox") + + pdf.AddPage() + importer.UseImportedTemplate(pdf, t, 0, 0, 215.9, 0) + + pdf.SetY(20) + + // write info + pdf.SetY(50) + pdf.SetX(10) + pdf.SetFont("Times", "", 11) + pdf.CellFormat( + 97, + 8, + fmt.Sprintf("Attention: %s %s", order.FirstName, order.LastName), + "", + 0, + "L", + false, + 0, + "", + ) + pdf.Ln(5) + pdf.CellFormat( + 97, + 8, + order.Email, + "", + 0, + "L", + false, + 0, + "", + ) + pdf.Ln(5) + pdf.CellFormat( + 97, + 8, + order.CreatedAt.Format("2005-01-02"), + "", + 0, + "L", + false, + 0, + "", + ) + + pdf.SetX(58) + pdf.SetY(93) + pdf.CellFormat( + 155, + 8, + order.Product, + "", + 0, + "L", + false, + 0, + "", + ) + pdf.SetX(166) + pdf.CellFormat( + 20, + 8, + fmt.Sprintf("%d", order.Quantity), + "", + 0, + "C", + false, + 0, + "", + ) + + pdf.SetX(185) + pdf.CellFormat( + 155, + 8, + fmt.Sprintf("€%.2f", float32(order.Amount/100.0)), + "", + 0, + "R", + false, + 0, + "", + ) + + invoicePath := fmt.Sprintf("./invoices/%d.pdf", order.ID) + err := pdf.OutputFileAndClose(invoicePath) + if err != nil { + return err + } + return nil +} diff --git a/cmd/micro/invoice/invoice-routes.go b/cmd/micro/invoice/invoice-routes.go new file mode 100644 index 0000000..594afbb --- /dev/null +++ b/cmd/micro/invoice/invoice-routes.go @@ -0,0 +1,23 @@ +package main + +import ( + "net/http" + + "github.com/go-chi/chi/v5" + "github.com/go-chi/cors" +) + +func (app *application) routes() http.Handler { + mux := chi.NewRouter() + + mux.Use(cors.Handler(cors.Options{ + AllowedOrigins: []string{"https://*", "http://*"}, + AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, + AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"}, + AllowCredentials: false, + MaxAge: 300, + })) + + mux.Post("/invoice/create-and-send", app.CreateAndSendInvoice) + return mux +} diff --git a/cmd/micro/invoice/invoice.go b/cmd/micro/invoice/invoice.go new file mode 100644 index 0000000..6be8b80 --- /dev/null +++ b/cmd/micro/invoice/invoice.go @@ -0,0 +1,79 @@ +package main + +import ( + "flag" + "fmt" + "log" + "net/http" + "os" + "time" +) + +const ( + version = "1.0.0" +) + +type config struct { + port int + smtp struct { + host string + port int + username string + password string + } + frontend string +} + +type application struct { + config config + infoLog *log.Logger + errorLog *log.Logger + version string +} + +func (app *application) serve() error { + srv := &http.Server{ + Addr: fmt.Sprintf(":%d", app.config.port), + Handler: app.routes(), + IdleTimeout: 30 * time.Second, + ReadTimeout: 10 * time.Second, + ReadHeaderTimeout: 5 * time.Second, + WriteTimeout: 5 * time.Second, + } + + app.infoLog.Printf( + "Starting invoice microservice on port %d", + app.config.port, + ) + return srv.ListenAndServe() +} + +func main() { + var cfg config + + flag.IntVar(&cfg.port, "port", 5000, "Server port to listen on") + flag.StringVar(&cfg.smtp.host, "smtphost", "0.0.0.0", "smtp host") + flag.IntVar(&cfg.smtp.port, "smtpport", 1025, "smtp host") + flag.StringVar(&cfg.smtp.username, "smtpuser", "user", "smtp user") + flag.StringVar(&cfg.smtp.password, "smtppwd", "password", "smtp password") + flag.StringVar(&cfg.frontend, "frontend", "http://localhost:4000", "frontend address") + + flag.Parse() + + infoLog := log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime) + errorLog := log.New(os.Stdout, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile) + + app := &application{ + version: version, + config: cfg, + infoLog: infoLog, + errorLog: errorLog, + } + + app.CreateDirIfNotExist("./invoices") + + err := app.serve() + if err != nil { + log.Fatal(err) + } +} diff --git a/go.mod b/go.mod index 85f88ff..1b90cdc 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/go-chi/cors v1.2.1 github.com/go-sql-driver/mysql v1.8.1 github.com/gorilla/websocket v1.5.3 + github.com/phpdave11/gofpdf v1.4.2 github.com/stripe/stripe-go/v79 v79.6.0 github.com/xhit/go-simple-mail/v2 v2.16.0 golang.org/x/crypto v0.26.0 @@ -18,6 +19,8 @@ require ( require ( filippo.io/edwards25519 v1.1.0 // indirect github.com/go-test/deep v1.1.1 // indirect + github.com/phpdave11/gofpdi v1.0.12 // indirect + github.com/pkg/errors v0.9.1 // indirect github.com/toorop/go-dkim v0.0.0-20201103131630-e1cd1a0a5208 // indirect golang.org/x/sys v0.23.0 // indirect ) diff --git a/go.sum b/go.sum index 596620a..20f6b41 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,7 @@ github.com/alexedwards/scs/mysqlstore v0.0.0-20240316134038-7e11d57e8885 h1:C7QA github.com/alexedwards/scs/mysqlstore v0.0.0-20240316134038-7e11d57e8885/go.mod h1:p8jK3D80sw1PFrCSdlcJF1O75bp55HqbgDyyCLM0FrE= github.com/alexedwards/scs/v2 v2.8.0 h1:h31yUYoycPuL0zt14c0gd+oqxfRwIj6SOjHdKRZxhEw= github.com/alexedwards/scs/v2 v2.8.0/go.mod h1:ToaROZxyKukJKT/xLcVQAChi5k6+Pn1Gvmdl7h3RRj8= +github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8= github.com/bwmarrin/go-alone v0.0.0-20190806015146-742bb55d1631 h1:Xb5rra6jJt5Z1JsZhIMby+IP5T8aU+Uc2RC9RzSxs9g= github.com/bwmarrin/go-alone v0.0.0-20190806015146-742bb55d1631/go.mod h1:P86Dksd9km5HGX5UMIocXvX87sEp2xUARle3by+9JZ4= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= @@ -19,9 +20,19 @@ github.com/go-test/deep v1.1.1 h1:0r/53hagsehfO4bzD2Pgr/+RgHqhmf+k1Bpse2cTu1U= github.com/go-test/deep v1.1.1/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE= github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes= +github.com/phpdave11/gofpdf v1.4.2 h1:KPKiIbfwbvC/wOncwhrpRdXVj2CZTCFlw4wnoyjtHfQ= +github.com/phpdave11/gofpdf v1.4.2/go.mod h1:zpO6xFn9yxo3YLyMvW8HcKWVdbNqgIfOOp2dXMnm1mY= +github.com/phpdave11/gofpdi v1.0.12 h1:RZb9NG62cw/RW0rHAduVRo+98R8o/G1krcg2ns7DakQ= +github.com/phpdave11/gofpdi v1.0.12/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfFZQK844Gfx8o5WFuvpxWRwnSoipWe/p622j1v06w= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stripe/stripe-go/v79 v79.6.0 h1:qSBV2f2rpLEEZTdTlVLzdmQJZNmfoo2E3hUEkFT8GBc= @@ -32,6 +43,7 @@ github.com/xhit/go-simple-mail/v2 v2.16.0 h1:ouGy/Ww4kuaqu2E2UrDw7SvLaziWTB60ICL github.com/xhit/go-simple-mail/v2 v2.16.0/go.mod h1:b7P5ygho6SYE+VIqpxA6QkYfv4teeyG4MKqB3utRu98= golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw= golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54= +golang.org/x/image v0.0.0-20190910094157-69e4b8554b2a/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/net v0.0.0-20210520170846-37e1c6afe023/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= @@ -40,6 +52,7 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM= golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= diff --git a/pdf-templates/invoice.pdf b/pdf-templates/invoice.pdf new file mode 100644 index 0000000..4431671 Binary files /dev/null and b/pdf-templates/invoice.pdf differ