Compare commits

...

4 Commits

Author SHA1 Message Date
Wancat
526a1c4faa Migrate to layout 2022-10-20 11:20:24 +08:00
Wancat
ab43f732fe Migrate to GIN 2022-10-20 11:07:35 +08:00
Wancat
c23c781c63 Finish auth & pass test 2022-10-20 09:53:40 +08:00
Wancat
b7bad122e6 Add test 2022-10-20 09:33:05 +08:00
11 changed files with 372 additions and 99 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
.env .env
test.txt test.txt
ledger-quicknote ledger-quicknote
*.txt

93
auth/auth.go Normal file
View File

@@ -0,0 +1,93 @@
package auth
import (
"bufio"
"errors"
"fmt"
"os"
"strings"
"golang.org/x/crypto/bcrypt"
)
type AuthStore interface {
Register(user, pass string) error
Authenticate(user, pass string) error
Remove(user string) error
}
type Htpasswd struct {
accounts map[string]string
filePath string
}
func NewHtpasswd(path string) (AuthStore, error) {
s := Htpasswd{
filePath: path,
}
err := s.read()
return s, err
}
func (s *Htpasswd) read() (err error) {
file, err := os.Open(s.filePath)
if err != nil {
return err
}
defer file.Close()
fileScanner := bufio.NewScanner(file)
fileScanner.Split(bufio.ScanLines)
s.accounts = make(map[string]string)
for fileScanner.Scan() {
arr := strings.SplitN(fileScanner.Text(), ":", 2)
if len(arr) < 2 {
return fmt.Errorf("invalid data %s", arr)
}
s.accounts[arr[0]] = arr[1]
}
return nil
}
func (s *Htpasswd) write() (err error) {
file, err := os.OpenFile(s.filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return fmt.Errorf("failed to open htpasswd file: %w", err)
}
defer file.Close()
for u, p := range s.accounts {
_, err = fmt.Fprintf(file, "%s:%s\n", u, p)
if err != nil {
return err
}
}
return nil
}
func (s Htpasswd) Register(user, pass string) (err error) {
s.accounts[user], err = hash(pass)
if err != nil {
return
}
return s.write()
}
func (s Htpasswd) Authenticate(user, pass string) (err error) {
hashed, ok := s.accounts[user]
if !ok {
return errors.New("user not found")
}
return bcrypt.CompareHashAndPassword([]byte(hashed), []byte(pass))
}
func (s Htpasswd) Remove(user string) (err error) {
delete(s.accounts, user)
return s.write()
}
func hash(pass string) (string, error) {
output, err := bcrypt.GenerateFromPassword([]byte(pass), bcrypt.DefaultCost)
return string(output), err
}

66
auth/auth_test.go Normal file
View File

@@ -0,0 +1,66 @@
package auth
import (
"fmt"
"io/ioutil"
"strings"
"testing"
)
type User struct {
user string
pass string
hashed string
}
func TestHtpasswdSuccess(t *testing.T) {
path := "/tmp/.htpasswd"
user1 := User{
user: "user",
pass: "password",
hashed: "$2a$14$SQSscaF4fVO3e5dp2/.VPuVQDPKqxSagLQnN6OncTRtoQw0ie9ByK",
}
err := ioutil.WriteFile(path,
[]byte(fmt.Sprintf("%s:%s", user1.user, user1.hashed)), 0640)
if err != nil {
t.Fatal(err)
}
store, err := NewHtpasswd(path)
if err != nil {
t.Fatal(err)
}
err = store.Authenticate(user1.user, user1.pass)
if err != nil {
t.Error(err)
}
user2 := User{
user: "foo",
pass: "bar",
}
err = store.Register(user2.user, user2.pass)
if err != nil {
t.Error(err)
}
data, err := ioutil.ReadFile(path)
if err != nil {
t.Error(err)
}
for _, u := range []User{user1, user2} {
if !strings.Contains(string(data), u.user) {
t.Errorf("%s not found in htpasswd file: %s", u.user, string(data))
}
}
err = store.Remove(user1.user)
if err != nil {
t.Error(err)
}
data, err = ioutil.ReadFile(path)
if err != nil {
t.Error(err)
}
if strings.Contains(string(data), user1.user) {
t.Errorf("%s is found in htpasswd file but should be removed: %s", user1.user, string(data))
}
}

23
go.mod
View File

@@ -1,3 +1,26 @@
module github.com/lancatlin/ledger-quicknote module github.com/lancatlin/ledger-quicknote
go 1.19 go 1.19
require (
github.com/gin-contrib/multitemplate v0.0.0-20220829131020-8c2a8441bc2b // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.8.1 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.10.0 // indirect
github.com/goccy/go-json v0.9.7 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
golang.org/x/crypto v0.1.0 // indirect
golang.org/x/net v0.1.0 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/text v0.4.0 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

84
go.sum Normal file
View File

@@ -0,0 +1,84 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gin-contrib/multitemplate v0.0.0-20220829131020-8c2a8441bc2b h1:WMas4AhGwZLRBAYGikjEzx02gk5SbAn3xbbxhnGrgao=
github.com/gin-contrib/multitemplate v0.0.0-20220829131020-8c2a8441bc2b/go.mod h1:XLLtIXoP9+9zGcEDc7gAGV3AksGPO+vzv4kXHMJSdU0=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.8.1 h1:4+fr/el88TOO3ewCmQr8cx/CtZ/umlIRIs5M4NTNjf8=
github.com/gin-gonic/gin v1.8.1/go.mod h1:ji8BvRH1azfM+SYow9zQ6SZMvR8qOMZHmsCuWR9tTTk=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.0 h1:u50s323jtVGugKlcYeyzC0etD1HifMjqmJqb8WugfUU=
github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs=
github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/jYrnRPArHwAcmLoJZxyho=
github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
github.com/go-playground/validator/v10 v10.10.0 h1:I7mrTYv78z8k8VXa/qJlOlEXn/nBh+BF8dHX5nt/dr0=
github.com/go-playground/validator/v10 v10.10.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos=
github.com/goccy/go-json v0.9.7 h1:IcB+Aqpx/iMHu5Yooh7jEzJk1JZ7Pjtmys2ukPr7EeM=
github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
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/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pelletier/go-toml/v2 v2.0.1 h1:8e3L2cCQzLFi2CR4g7vGFuFxX7Jl1kKX8gW+iV0GUKU=
github.com/pelletier/go-toml/v2 v2.0.1/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU=
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.1.0 h1:hZ/3BUoy5aId7sCpA/Tc5lt8DkFgdVS2onTpJsZ/fl0=
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

77
main.go
View File

@@ -6,13 +6,13 @@ import (
"fmt" "fmt"
"io" "io"
"log" "log"
"net/http"
"net/url"
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
"text/template" "text/template"
"time" "time"
"github.com/gin-gonic/gin"
) )
var ledgerTpl *template.Template var ledgerTpl *template.Template
@@ -24,15 +24,15 @@ var WORKING_DIR string
var HOST string var HOST string
type TxData struct { type TxData struct {
Name string Action string `form:"action" binding:"required"`
Name string `form:"name"`
Date string Date string
Amount string Amount string `form:"amount" binding:"required"`
Account string Account string `form:"account"`
} }
func init() { func init() {
ledgerTpl = template.Must(template.ParseGlob("tx/*")) ledgerTpl = template.Must(template.ParseGlob("tx/*"))
htmlTpl = template.Must(template.ParseGlob("templates/*.html"))
flag.StringVar(&LEDGER_FILE, "f", "example.txt", "ledger journal file to write") flag.StringVar(&LEDGER_FILE, "f", "example.txt", "ledger journal file to write")
flag.StringVar(&LEDGER_INIT, "i", "", "ledger initiation file") flag.StringVar(&LEDGER_INIT, "i", "", "ledger initiation file")
flag.StringVar(&WORKING_DIR, "w", "", "ledger working directory") flag.StringVar(&WORKING_DIR, "w", "", "ledger working directory")
@@ -41,8 +41,10 @@ func init() {
} }
func main() { func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { r := gin.Default()
htmlTpl.ExecuteTemplate(w, "index.html", struct { r.HTMLRender = loadTemplates("templates")
r.GET("/", func(c *gin.Context) {
c.HTML(200, "index.html", struct {
Templates []*template.Template Templates []*template.Template
Scripts map[string][]string Scripts map[string][]string
}{ }{
@@ -51,66 +53,51 @@ func main() {
}) })
}) })
http.HandleFunc("/new", func(w http.ResponseWriter, r *http.Request) { r.POST("/new", func(c *gin.Context) {
if err := r.ParseForm(); err != nil { var data TxData
http.Error(w, err.Error(), 400) if err := c.ShouldBind(&data); err != nil {
c.AbortWithError(400, err)
return return
} }
tx, err := newTx(r.Form) tx, err := newTx(data)
if err != nil { if err != nil {
http.Error(w, err.Error(), 400) c.AbortWithError(400, err)
log.Println(err, r.Form) log.Println(err, c.Request.Form)
return return
} }
if err := htmlTpl.ExecuteTemplate(w, "new.html", struct { c.HTML(200, "new.html", struct {
Tx string Tx string
}{tx}); err != nil { }{tx})
http.Error(w, err.Error(), 500)
}
}) })
http.HandleFunc("/submit", func(w http.ResponseWriter, r *http.Request) { r.POST("/submit", func(c *gin.Context) {
if err := r.ParseForm(); err != nil { tx := c.PostForm("tx")
http.Error(w, err.Error(), 400)
return
}
tx := r.FormValue("tx")
if err := appendToFile(tx); err != nil { if err := appendToFile(tx); err != nil {
http.Error(w, err.Error(), 500) c.AbortWithError(500, err)
return return
} }
if err := htmlTpl.ExecuteTemplate(w, "success.html", struct { c.HTML(200, "success.html", struct {
Tx string Tx string
}{tx}); err != nil { }{tx})
http.Error(w, err.Error(), 500)
}
}) })
http.HandleFunc("/exec", func(w http.ResponseWriter, r *http.Request) { r.GET("/exec", func(c *gin.Context) {
name := r.FormValue("name") name, _ := c.GetQuery("name")
if err := executeScript(w, name); err != nil { if err := executeScript(c.Writer, name); err != nil {
http.Error(w, err.Error(), 500) c.AbortWithError(500, err)
log.Println(err) log.Println(err)
return return
} }
}) })
log.Printf("Listen on %s", HOST) log.Fatal(r.Run(HOST))
log.Fatal(http.ListenAndServe(HOST, nil))
} }
func newTx(params url.Values) (result string, err error) { func newTx(data TxData) (result string, err error) {
action := params.Get("action") data.Date = time.Now().Format("2006/01/02")
data := TxData{
Date: time.Now().Format("2006/01/02"),
Amount: params.Get("amount"),
Account: params.Get("account"),
Name: params.Get("name"),
}
var buf bytes.Buffer var buf bytes.Buffer
err = ledgerTpl.ExecuteTemplate(&buf, action, data) err = ledgerTpl.ExecuteTemplate(&buf, data.Action, data)
return buf.String(), nil return buf.String(), nil
} }

27
template.go Normal file
View File

@@ -0,0 +1,27 @@
package main
import (
"path"
"path/filepath"
"github.com/gin-contrib/multitemplate"
)
func loadTemplates(templatesDir string) multitemplate.Renderer {
r := multitemplate.NewRenderer()
layouts, err := filepath.Glob(path.Join(templatesDir, "layouts", "*.html"))
if err != nil {
panic(err)
}
includes, err := filepath.Glob(path.Join(templatesDir, "*.html"))
if err != nil {
panic(err)
}
for _, include := range includes {
r.AddFromFiles(filepath.Base(include), append(layouts, include)...)
}
return r
}

View File

@@ -1,27 +1,20 @@
<!DOCTYPE html> {{ define "main" }}
<html> <h1>Ledger Quick Note</h1>
<head> <form action="/new" method="POST">
<title>Ledger Quick Note</title> <label>Action:
<meta name="viewport" content="width=device-width, initial-scale=1.0"> {{ range .Templates }}
</head> <input type="radio" name="action" value="{{ .Name }}">{{ .Name }}</option>
<body> {{ end }}
<h1>Ledger Quick Note</h1> </label><br>
<form action="/new" method="POST"> <label>Amount: <input name="amount" type="number"></label><br>
<label>Action: <label>Account: <input name="account" type="text"></label></br>
{{ range .Templates }} <label>Tx Name: <input name="name" type="text"></label></br>
<input type="radio" name="action" value="{{ .Name }}">{{ .Name }}</option> <input type="submit">
{{ end }} </form>
</label><br> <h2>Scripts</h2>
<label>Amount: <input name="amount" type="number"></label><br> <ul>
<label>Account: <input name="account" type="text"></label></br> {{ range $k, $v := .Scripts }}
<label>Tx Name: <input name="name" type="text"></label></br> <li><a href="/exec?name={{ $k }}">{{ $k }}</a></li>
<input type="submit"> {{ end }}
</form> </ul>
<h2>Scripts</h2> {{ end }}
<ul>
{{ range $k, $v := .Scripts }}
<li><a href="/exec?name={{ $k }}">{{ $k }}</a></li>
{{ end }}
</ul>
</body>
</html>

View File

@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>{{ block "title" . }}Ledger Quick Note{{ end }}</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
{{ block "main" . }}
{{ end }}
</body>
</html>

View File

@@ -1,14 +1,8 @@
<!DOCTYPE html> {{ define "title" }}Confirm{{ end }}
<html> {{ define "main" }}
<head> <h1>Confirm new Tx</h1>
<title>Ledger Quick Note</title> <form action="/submit" method="POST">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <textarea name="tx" rows="15" cols="40">{{ .Tx }}</textarea><br>
</head> <input type="submit">
<body> </form>
<h1>Confirm new Tx</h1> {{ end }}
<form action="/submit" method="POST">
<textarea name="tx" rows="15" cols="40">{{ .Tx }}</textarea><br>
<input type="submit">
</form>
</body>
</html>

View File

@@ -1,13 +1,6 @@
<!DOCTYPE html> {{ define "title" }}Success{{ end }}
<html> {{ define "main" }}
<head> <p><strong>Success</strong></p>
<title>Ledger Quick Note</title> <pre><code>{{ .Tx }}</code></pre>
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <p><a href="/">Back to home</a></p>
</head> {{ end }}
<body>
<h1>Ledger Quick Note</h1>
<p><strong>Success</strong></p>
<pre><code>{{ .Tx }}</code></pre>
<p><a href="/">Back to home</a></p>
</body>
</html>