| package main |
| |
| import ( |
| "context" |
| "fmt" |
| "net/http" |
| |
| "code.hackerspace.pl/hscloud/go/mirko" |
| "code.hackerspace.pl/hscloud/go/statusz" |
| "github.com/golang/glog" |
| ) |
| |
| const invoicesFragment = ` |
| <style type="text/css"> |
| .table td,th { |
| background-color: #eee; |
| padding: 0.2em 0.4em 0.2em 0.4em; |
| } |
| .table th { |
| background-color: #c0c0c0; |
| } |
| .table { |
| background-color: #fff; |
| border-spacing: 0.2em; |
| margin-left: auto; |
| margin-right: auto; |
| } |
| </style> |
| <div> |
| {{ .Msg }} |
| <table class="table"> |
| <tr> |
| <th>Internal ID</th> |
| <th>Number</th> |
| <th>Customer</th> |
| <th>Amount (net)</th> |
| <th>Actions</th> |
| </tr> |
| {{ range .Invoices }} |
| {{ if .Sealed }} |
| <tr> |
| {{ else }} |
| <tr style="opacity: 0.5"> |
| {{ end }} |
| <td>{{ .ID }}</td> |
| <td>{{ .Number }}</td> |
| <td>{{ index .Proto.CustomerBilling 0 }}</td> |
| <td>{{ .TotalNetPretty }}</td> |
| <td> |
| <a href="/debug/view?id={{ .ID }}">View</a> |
| </td> |
| </tr> |
| {{ end }} |
| </table> |
| </div> |
| ` |
| |
| type templateInvoice struct { |
| invoice |
| TotalNetPretty string |
| } |
| |
| func (s *service) setupStatusz(m *mirko.Mirko) { |
| statusz.AddStatusPart("Invoices", invoicesFragment, func(ctx context.Context) interface{} { |
| var res struct { |
| Invoices []templateInvoice |
| Msg string |
| } |
| invoices, err := s.m.getInvoices(ctx) |
| res.Invoices = make([]templateInvoice, len(invoices)) |
| for i, inv := range invoices { |
| res.Invoices[i] = templateInvoice{ |
| invoice: inv, |
| TotalNetPretty: fmt.Sprintf("%.2f %s", float64(inv.TotalNet)/100, inv.Proto.Unit), |
| } |
| } |
| |
| if err != nil { |
| glog.Errorf("Could not get invoices for statusz: %v", err) |
| res.Msg = fmt.Sprintf("Could not get invoices: %v", err) |
| } |
| return res |
| }) |
| |
| m.HTTPMux().HandleFunc("/debug/view", func(w http.ResponseWriter, r *http.Request) { |
| rendered, err := s.invoicePDF(r.Context(), r.URL.Query().Get("id")) |
| if err != nil { |
| fmt.Fprintf(w, "error: %v", err) |
| } |
| w.Header().Set("Content-type", "application/pdf") |
| w.Write(rendered) |
| }) |
| } |