Skip to content

Commit

Permalink
Update web to use embed
Browse files Browse the repository at this point in the history
switch to httprouter
add gzip/brotli compression if client allows
  • Loading branch information
howeyc committed Apr 24, 2021
1 parent 1eab359 commit 55a1183
Show file tree
Hide file tree
Showing 65 changed files with 158 additions and 1,877 deletions.
35 changes: 3 additions & 32 deletions cmd/lweb/assets.go
Original file line number Diff line number Diff line change
@@ -1,52 +1,23 @@
package main

//go:generate go-bindata -o bindata.go public/... templates/...

import (
"fmt"
"html/template"
"path"
)

func parseAssetsWithFunc(funcMap template.FuncMap, filenames ...string) (*template.Template, error) {
if len(filenames) == 0 {
// Not really a problem, but be consistent.
return nil, fmt.Errorf("html/template: no files named in call to ParseFiles")
}
tresult := template.New("result").Funcs(funcMap)

for _, filename := range filenames {
tdata, aerr := Asset(filename)
if aerr != nil {
return nil, aerr
}

_, err := tresult.Parse(string(tdata))
if err != nil {
return nil, err
}
}

return tresult, nil
return template.New(path.Base(filenames[0])).Funcs(funcMap).ParseFS(contentTemplates, filenames...)
}

func parseAssets(filenames ...string) (*template.Template, error) {
if len(filenames) == 0 {
// Not really a problem, but be consistent.
return nil, fmt.Errorf("html/template: no files named in call to ParseFiles")
}
tresult := template.New("result")

for _, filename := range filenames {
tdata, aerr := Asset(filename)
if aerr != nil {
return nil, aerr
}

_, err := tresult.Parse(string(tdata))
if err != nil {
return nil, err
}
}

return tresult, nil
return template.New(path.Base(filenames[0])).ParseFS(contentTemplates, filenames...)
}
1,500 changes: 0 additions & 1,500 deletions cmd/lweb/bindata.go

This file was deleted.

21 changes: 12 additions & 9 deletions cmd/lweb/handler_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"time"

"github.com/howeyc/ledger"
"github.com/julienschmidt/httprouter"

"github.com/go-martini/martini"
"github.com/pelletier/go-toml"
)

Expand All @@ -26,19 +26,22 @@ type quickviewConfigStruct struct {
Accounts []quickviewAccountConfig `toml:"account"`
}

func quickviewHandler(w http.ResponseWriter, r *http.Request) {
func quickviewHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
if len(quickviewConfigFileName) == 0 {
http.Redirect(w, r, "/accounts", http.StatusFound)
return
}
ifile, ierr := os.Open(quickviewConfigFileName)
if ierr != nil {
http.Redirect(w, r, "/accounts", http.StatusFound)
return
}
defer ifile.Close()
tdec := toml.NewDecoder(ifile)
var quickviewConfigData quickviewConfigStruct
if lerr := tdec.Decode(&quickviewConfigData); lerr != nil || len(quickviewConfigData.Accounts) < 1 {
http.Redirect(w, r, "/accounts", http.StatusFound)
return
}

shorten := func(accname string) string {
Expand Down Expand Up @@ -89,7 +92,7 @@ func quickviewHandler(w http.ResponseWriter, r *http.Request) {
}
}

func addTransactionPostHandler(w http.ResponseWriter, r *http.Request) {
func addTransactionPostHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
strDate := r.FormValue("transactionDate")
strPayee := r.FormValue("transactionPayee")

Expand Down Expand Up @@ -140,8 +143,8 @@ func addTransactionPostHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Transaction added!")
}

func addQuickTransactionHandler(w http.ResponseWriter, r *http.Request, params martini.Params) {
accountName := params["accountName"]
func addQuickTransactionHandler(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
accountName := params.ByName("accountName")
funcMap := template.FuncMap{
"abbrev": abbrev,
}
Expand Down Expand Up @@ -202,7 +205,7 @@ func addQuickTransactionHandler(w http.ResponseWriter, r *http.Request, params m
}
}

func addTransactionHandler(w http.ResponseWriter, r *http.Request) {
func addTransactionHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
funcMap := template.FuncMap{
"abbrev": abbrev,
}
Expand Down Expand Up @@ -232,7 +235,7 @@ func addTransactionHandler(w http.ResponseWriter, r *http.Request) {
}
}

func accountsHandler(w http.ResponseWriter, r *http.Request) {
func accountsHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
funcMap := template.FuncMap{
"abbrev": abbrev,
}
Expand Down Expand Up @@ -263,8 +266,8 @@ func accountsHandler(w http.ResponseWriter, r *http.Request) {
}
}

func accountHandler(w http.ResponseWriter, r *http.Request, params martini.Params) {
accountName := params["accountName"]
func accountHandler(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
accountName := params.ByName("accountName")

t, err := parseAssets("templates/template.account.html", "templates/template.nav.html")
if err != nil {
Expand Down
11 changes: 8 additions & 3 deletions cmd/lweb/handler_ledger.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package main

import "net/http"
import (
"html/template"
"net/http"

func ledgerHandler(w http.ResponseWriter, r *http.Request) {
t, err := parseAssets("templates/template.ledger.html", "templates/template.nav.html")
"github.com/julienschmidt/httprouter"
)

func ledgerHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
t, err := template.ParseFS(contentTemplates, "templates/template.ledger.html", "templates/template.nav.html")
if err != nil {
http.Error(w, err.Error(), 500)
return
Expand Down
6 changes: 3 additions & 3 deletions cmd/lweb/handler_portfolio.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"net/http"
"sort"

"github.com/go-martini/martini"
"github.com/howeyc/ledger"
"github.com/julienschmidt/httprouter"
)

type iexQuote struct {
Expand Down Expand Up @@ -60,8 +60,8 @@ func cryptoQuote(symbol string) (quote gdaxQuote, err error) {
return quote, nil
}

func portfolioHandler(w http.ResponseWriter, r *http.Request, params martini.Params) {
portfolioName := params["portfolioName"]
func portfolioHandler(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
portfolioName := params.ByName("portfolioName")

var portfolio portfolioStruct
for _, port := range portfolioConfigData.Portfolios {
Expand Down
10 changes: 4 additions & 6 deletions cmd/lweb/handler_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"strings"
"time"

"github.com/go-martini/martini"
"github.com/howeyc/ledger"
"github.com/julienschmidt/httprouter"
colorful "github.com/lucasb-eyer/go-colorful"
)

Expand Down Expand Up @@ -109,9 +109,7 @@ func getAccounts(accountNeedle string, accountsHaystack []*ledger.Account) (resu
// Remove any parents
for k := range foundAccountNames {
kpre := k[:strings.LastIndex(k, ":")]
if _, found := foundAccountNames[kpre]; found {
delete(foundAccountNames, kpre)
}
delete(foundAccountNames, kpre)
}
// Remaining are the results
for _, hay := range foundAccountNames {
Expand Down Expand Up @@ -214,8 +212,8 @@ func mergeAccounts(input *ledger.Transaction) {
})
}

func reportHandler(w http.ResponseWriter, r *http.Request, params martini.Params) {
reportName := params["reportName"]
func reportHandler(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
reportName := params.ByName("reportName")

trans, terr := getTransactions()
if terr != nil {
Expand Down
50 changes: 50 additions & 0 deletions cmd/lweb/internal/httpcompress/compress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package httpcompress

import (
"compress/gzip"
"io"
"net/http"
"strings"

"github.com/andybalholm/brotli"
"github.com/julienschmidt/httprouter"
)

// CompressResponseWriter is a Struct for manipulating io writer
type CompressResponseWriter struct {
io.Writer
http.ResponseWriter
}

func (res CompressResponseWriter) Write(b []byte) (int, error) {
if "" == res.Header().Get("Content-Type") {
// If no content type, apply sniffing algorithm to un-gzipped body.
res.Header().Set("Content-Type", http.DetectContentType(b))
}
return res.Writer.Write(b)
}

// Middleware force - bool, whether or not to force Compression regardless of the sent headers.
func Middleware(fn httprouter.Handle, force bool) httprouter.Handle {
return func(res http.ResponseWriter, req *http.Request, pm httprouter.Params) {
if !strings.Contains(req.Header.Get("Accept-Encoding"), "br") {
if !strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") && !force {
fn(res, req, pm)
return
}
res.Header().Set("Vary", "Accept-Encoding")
res.Header().Set("Content-Encoding", "gzip")
gz := gzip.NewWriter(res)
defer gz.Close()
cw := CompressResponseWriter{Writer: gz, ResponseWriter: res}
fn(cw, req, pm)
return
}
res.Header().Set("Vary", "Accept-Encoding")
res.Header().Set("Content-Encoding", "br")
br := brotli.NewWriter(res)
defer br.Close()
cw := CompressResponseWriter{Writer: br, ResponseWriter: res}
fn(cw, req, pm)
}
}
44 changes: 28 additions & 16 deletions cmd/lweb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bytes"
"crypto/sha256"
"embed"
"flag"
"fmt"
"io"
Expand All @@ -13,10 +14,9 @@ import (
"time"

"github.com/howeyc/ledger"
"github.com/howeyc/ledger/cmd/lweb/internal/httpcompress"

"github.com/go-martini/martini"
"github.com/martini-contrib/gzip"
"github.com/martini-contrib/staticbin"
"github.com/julienschmidt/httprouter"
"github.com/pelletier/go-toml"
)

Expand All @@ -28,6 +28,12 @@ var ledgerLock sync.Mutex
var currentSum []byte
var currentTrans []*ledger.Transaction

//go:embed static/*
var contentStatic embed.FS

//go:embed templates/*
var contentTemplates embed.FS

func getTransactions() ([]*ledger.Transaction, error) {
ledgerLock.Lock()
defer ledgerLock.Unlock()
Expand Down Expand Up @@ -198,19 +204,25 @@ func main() {
log.Fatalln(err)
}

m := martini.Classic()
m.Use(gzip.All())
m.Use(staticbin.Static("public", Asset))

m.Get("/ledger", ledgerHandler)
m.Get("/accounts", accountsHandler)
m.Get("/addtrans", addTransactionHandler)
m.Get("/addtrans/:accountName", addQuickTransactionHandler)
m.Post("/addtrans", addTransactionPostHandler)
m.Get("/portfolio/:portfolioName", portfolioHandler)
m.Get("/account/:accountName", accountHandler)
m.Get("/report/:reportName", reportHandler)
m.Get("/", quickviewHandler)
m := httprouter.New()

fileServer := http.FileServer(http.FS(contentStatic))
m.GET("/static/*filepath", func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
w.Header().Set("Vary", "Accept-Encoding")
w.Header().Set("Cache-Control", "public, max-age=7776000")
req.URL.Path = "/static/" + ps.ByName("filepath")
fileServer.ServeHTTP(w, req)
})

m.GET("/ledger", httpcompress.Middleware(ledgerHandler, false))
m.GET("/accounts", httpcompress.Middleware(accountsHandler, false))
m.GET("/addtrans", httpcompress.Middleware(addTransactionHandler, false))
m.GET("/addtrans/:accountName", httpcompress.Middleware(addQuickTransactionHandler, false))
m.POST("/addtrans", httpcompress.Middleware(addTransactionPostHandler, false))
m.GET("/portfolio/:portfolioName", httpcompress.Middleware(portfolioHandler, false))
m.GET("/account/:accountName", httpcompress.Middleware(accountHandler, false))
m.GET("/report/:reportName", httpcompress.Middleware(reportHandler, false))
m.GET("/", httpcompress.Middleware(quickviewHandler, false))

log.Println("Listening on port", serverPort)
var listenAddress string
Expand Down
2 changes: 0 additions & 2 deletions cmd/lweb/public/browserconfig.xml

This file was deleted.

48 changes: 0 additions & 48 deletions cmd/lweb/public/manifest.json

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
Loading

0 comments on commit 55a1183

Please sign in to comment.