Init commit: success to render tx

This commit is contained in:
Wancat
2022-10-18 02:41:47 +08:00
commit 5153e807ed
6 changed files with 87 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
.env

14
index.html Normal file
View File

@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<title>Ledger Quick Note</title>
</head>
<body>
<h1>Ledger Quick Note</h1>
<form action="/action" method="POST">
<label>Action: <input name="action" type="text"></label>
<label>Amount: <input name="amount" type="number"></label>
<input type="submit">
</form>
</body>
</html>

56
main.go Normal file
View File

@@ -0,0 +1,56 @@
package main
import (
"fmt"
"io"
"log"
"net/http"
"net/url"
"text/template"
"time"
)
var tpl *template.Template
type TxData struct {
Date string
Amount string
}
func init() {
var err error
tpl, err = template.ParseGlob("templates/*.txt")
if err != nil {
panic(err)
}
log.Println(tpl.DefinedTemplates())
}
func main() {
fmt.Println("Hi")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "index.html")
})
http.HandleFunc("/action", func(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
panic(err)
}
fmt.Println(r.Form)
if err := renderTx(w, r.Form); err != nil {
panic(err)
}
})
log.Fatal(http.ListenAndServe(":8000", nil))
}
func renderTx(w io.Writer, params url.Values) (err error) {
name := params.Get("action")
data := TxData{
Date: time.Now().Format("2006/01/02"),
Amount: params.Get("amount"),
}
return tpl.ExecuteTemplate(w, name, data)
}

4
templates/cash.txt Normal file
View File

@@ -0,0 +1,4 @@
{{ .Date }} * cash
cash ${{ .Amount }}
assets:cash

4
templates/withdraw.txt Normal file
View File

@@ -0,0 +1,4 @@
{{ .Date }} * withdraw
assets:cash ${{ .Amount }}
assets:checking

8
test.txt Normal file
View File

@@ -0,0 +1,8 @@
2022/10/18
cash $1000
assets:cash
2022/10/18
cash $1000
assets:cash