Skip to content

Commit

Permalink
build: use go1.22 http.ServeMux
Browse files Browse the repository at this point in the history
  • Loading branch information
howeyc committed Feb 8, 2024
1 parent 76c9358 commit 564d3a6
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 58 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/howeyc/ledger

go 1.21
go 1.22

require (
github.com/alfredxing/calc v0.0.0-20180827002445-77daf576f976
Expand All @@ -10,7 +10,6 @@ require (
github.com/ivanpirog/coloredcobra v1.0.1
github.com/jbrukh/bayesian v0.0.0-20200318221351-d726b684ca4a
github.com/joyt/godate v0.0.0-20150226210126-7151572574a7
github.com/julienschmidt/httprouter v1.3.0
github.com/juztin/numeronym v0.0.0-20160223091026-859fcc2918e2
github.com/lucasb-eyer/go-colorful v1.2.0
github.com/patrickmn/go-cache v2.1.0+incompatible
Expand Down
13 changes: 0 additions & 13 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
github.com/alfredxing/calc v0.0.0-20180827002445-77daf576f976 h1:+jyVKPjl5Y39thM0ZlVrRqKjSO/Upr5tP9ZQGELv8gw=
github.com/alfredxing/calc v0.0.0-20180827002445-77daf576f976/go.mod h1:/HQknSiD7YKT15DoHXuiXezQfNPBUm8PeqFaTxeA3HU=
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sxfOI=
github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/hako/durafmt v0.0.0-20210608085754-5c1018a4e16b h1:wDUNC2eKiL35DbLvsDhiblTUXHxcOPwQSCzi7xpQUN4=
github.com/hako/durafmt v0.0.0-20210608085754-5c1018a4e16b/go.mod h1:VzxiSdG6j1pi7rwGm/xYI5RbtpBgM8sARDXlvEvxlu0=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
Expand All @@ -22,32 +18,25 @@ github.com/jbrukh/bayesian v0.0.0-20200318221351-d726b684ca4a h1:gbdjhSslIoRRiSS
github.com/jbrukh/bayesian v0.0.0-20200318221351-d726b684ca4a/go.mod h1:SELxwZQq/mPnfPCR2mchLmT4TQaPJvYtLcCtDWSM7vM=
github.com/joyt/godate v0.0.0-20150226210126-7151572574a7 h1:2wH5antjhmU3EuWyidm0lJ4B9hGMpl5lNRo+M9uGJ5A=
github.com/joyt/godate v0.0.0-20150226210126-7151572574a7/go.mod h1:R+UgFL3iylLhx9N4w35zZ2HdhDlgorRDx4SxbchWuN0=
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/juztin/numeronym v0.0.0-20160223091026-859fcc2918e2 h1:jrs0oyU9XY7MlTHbNxecqFgY+fgEENZdP4Z8FZln/pw=
github.com/juztin/numeronym v0.0.0-20160223091026-859fcc2918e2/go.mod h1:uVDl4OnjvPk07IzoXF/dFM7nBYqAKdJsz4e9xjjWo7Q=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhECwM=
github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
Expand All @@ -60,8 +49,6 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467 h1:CBpWXWQpIRjzmkkA+M7q9Fqnwd2mZr3AFqexg8YTfoM=
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek=
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
Expand Down
11 changes: 5 additions & 6 deletions ledger/cmd/internal/httpcompress/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"

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

// CompressResponseWriter is a Struct for manipulating io writer
Expand All @@ -25,26 +24,26 @@ func (res CompressResponseWriter) Write(b []byte) (int, error) {
}

// 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) {
func Middleware(fn http.HandlerFunc, force bool) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
if !strings.Contains(req.Header.Get("Accept-Encoding"), "br") {
if !strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") && !force {
fn(res, req, pm)
fn(res, req)
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)
fn(cw, req)
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)
fn(cw, req)
}
}
27 changes: 13 additions & 14 deletions ledger/cmd/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/howeyc/ledger/ledger/cmd/internal/httpcompress"

"github.com/howeyc/ledger"
"github.com/julienschmidt/httprouter"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -52,32 +51,32 @@ var webCmd = &cobra.Command{
log.Fatalln(err)
}

m := httprouter.New()
m := http.NewServeMux()

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

if !webReadOnly {
m.GET("/addtrans", httpcompress.Middleware(addTransactionHandler, false))
m.GET("/addtrans/:accountName", httpcompress.Middleware(addQuickTransactionHandler, false))
m.POST("/addtrans", httpcompress.Middleware(addTransactionPostHandler, false))
m.HandleFunc("GET /addtrans", httpcompress.Middleware(addTransactionHandler, false))
m.HandleFunc("GET /addtrans/{accountName}", httpcompress.Middleware(addQuickTransactionHandler, false))
m.HandleFunc("POST /addtrans", httpcompress.Middleware(addTransactionPostHandler, false))
}

m.GET("/ledger", httpcompress.Middleware(ledgerHandler, false))
m.GET("/accounts", httpcompress.Middleware(accountsHandler, 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("/favicon.ico", func(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
m.HandleFunc("GET /ledger", httpcompress.Middleware(ledgerHandler, false))
m.HandleFunc("GET /accounts", httpcompress.Middleware(accountsHandler, false))
m.HandleFunc("GET /portfolio/{portfolioName}", httpcompress.Middleware(portfolioHandler, false))
m.HandleFunc("GET /account/{accountName}", httpcompress.Middleware(accountHandler, false))
m.HandleFunc("GET /report/{reportName}", httpcompress.Middleware(reportHandler, false))
m.HandleFunc("GET /favicon.ico", func(w http.ResponseWriter, req *http.Request) {
req.URL.Path = "/static/favicon.ico"
fileServer.ServeHTTP(w, req)
})
m.GET("/", httpcompress.Middleware(quickviewHandler, false))
m.HandleFunc("/", httpcompress.Middleware(quickviewHandler, false))

log.Println("Listening on port", serverPort)
var listenAddress string
Expand Down
17 changes: 8 additions & 9 deletions ledger/cmd/webHandlerAccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import (
"time"

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

func quickviewHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
func quickviewHandler(w http.ResponseWriter, r *http.Request) {
if len(quickviewConfigData.Accounts) < 1 {
http.Redirect(w, r, "/accounts", http.StatusFound)
return
Expand Down Expand Up @@ -52,7 +51,7 @@ func quickviewHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Param
}
}

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

Expand Down Expand Up @@ -99,8 +98,8 @@ func addTransactionPostHandler(w http.ResponseWriter, r *http.Request, _ httprou
fmt.Fprintf(w, "Transaction added!")
}

func addQuickTransactionHandler(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
accountName := params.ByName("accountName")
func addQuickTransactionHandler(w http.ResponseWriter, r *http.Request) {
accountName := r.PathValue("accountName")

t, err := loadTemplates("templates/template.addtransaction.html")
if err != nil {
Expand Down Expand Up @@ -158,7 +157,7 @@ func addQuickTransactionHandler(w http.ResponseWriter, r *http.Request, params h
}
}

func addTransactionHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
func addTransactionHandler(w http.ResponseWriter, r *http.Request) {
t, err := loadTemplates("templates/template.addtransaction.html")
if err != nil {
http.Error(w, err.Error(), 500)
Expand All @@ -183,7 +182,7 @@ func addTransactionHandler(w http.ResponseWriter, r *http.Request, _ httprouter.
}
}

func accountsHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
func accountsHandler(w http.ResponseWriter, r *http.Request) {
t, err := loadTemplates("templates/template.accounts.html")
if err != nil {
http.Error(w, err.Error(), 500)
Expand All @@ -209,8 +208,8 @@ func accountsHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params
}
}

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

t, err := loadTemplates("templates/template.account.html")
if err != nil {
Expand Down
4 changes: 1 addition & 3 deletions ledger/cmd/webHandlerLedger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package cmd

import (
"net/http"

"github.com/julienschmidt/httprouter"
)

func ledgerHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
func ledgerHandler(w http.ResponseWriter, r *http.Request) {
t, err := loadTemplates("templates/template.ledger.html")
if err != nil {
http.Error(w, err.Error(), 500)
Expand Down
15 changes: 7 additions & 8 deletions ledger/cmd/webHandlerPortfolio.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"net/http"
"slices"
"strings"
"cmp"

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

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

var portfolio portfolioStruct
for _, port := range portfolioConfigData.Portfolios {
Expand Down Expand Up @@ -150,11 +150,10 @@ func portfolioHandler(w http.ResponseWriter, r *http.Request, params httprouter.
}

slices.SortFunc(pData.Stocks, func(a, b stockInfo) int {
if diff := strings.Compare(a.Section, b.Section); diff == 0 {
return strings.Compare(a.Ticker, b.Ticker)
} else {
return diff
}
return cmp.Or(
strings.Compare(a.Section, b.Section),
strings.Compare(a.Ticker, b.Ticker),
)
})

err = t.Execute(w, pData)
Expand Down
5 changes: 2 additions & 3 deletions ledger/cmd/webHandlerReport.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/howeyc/ledger"
"github.com/howeyc/ledger/decimal"
"github.com/howeyc/ledger/ledger/cmd/internal/pdr"
"github.com/julienschmidt/httprouter"
colorful "github.com/lucasb-eyer/go-colorful"
)

Expand Down Expand Up @@ -146,8 +145,8 @@ func mergeAccounts(input *ledger.Transaction) {
})
}

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

trans, terr := getTransactions()
if terr != nil {
Expand Down

0 comments on commit 564d3a6

Please sign in to comment.