commit 5153e807ed042457e4221a43fd375ed67f81bff0 Author: Wancat Date: Tue Oct 18 02:41:47 2022 +0800 Init commit: success to render tx diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c49bd7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env diff --git a/index.html b/index.html new file mode 100644 index 0000000..0da3610 --- /dev/null +++ b/index.html @@ -0,0 +1,14 @@ + + + + Ledger Quick Note + + +

Ledger Quick Note

+
+ + + +
+ + diff --git a/main.go b/main.go new file mode 100644 index 0000000..b4f0693 --- /dev/null +++ b/main.go @@ -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) +} diff --git a/templates/cash.txt b/templates/cash.txt new file mode 100644 index 0000000..fb234cb --- /dev/null +++ b/templates/cash.txt @@ -0,0 +1,4 @@ +{{ .Date }} * cash + cash ${{ .Amount }} + assets:cash + diff --git a/templates/withdraw.txt b/templates/withdraw.txt new file mode 100644 index 0000000..77cd60f --- /dev/null +++ b/templates/withdraw.txt @@ -0,0 +1,4 @@ +{{ .Date }} * withdraw + assets:cash ${{ .Amount }} + assets:checking + diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..c667989 --- /dev/null +++ b/test.txt @@ -0,0 +1,8 @@ +2022/10/18 + cash $1000 + assets:cash + +2022/10/18 + cash $1000 + assets:cash +