From 8709901ef9f687abd2a9871411cb54a8bb6546b0 Mon Sep 17 00:00:00 2001 From: Nickiel12 Date: Fri, 12 Jan 2024 16:34:26 -0800 Subject: [PATCH] basic example with two types of templates --- db.go | 1 - flake.nix | 3 --- web/{index.templ => hello.templ} | 0 web/{index_templ.go => hello_templ.go} | 2 +- web/router.go | 19 +++++++++++++++++++ web/templates/index.html | 12 ++++++++++++ 6 files changed, 32 insertions(+), 5 deletions(-) rename web/{index.templ => hello.templ} (100%) rename web/{index_templ.go => hello_templ.go} (94%) create mode 100644 web/templates/index.html diff --git a/db.go b/db.go index 86eddd5..881786b 100644 --- a/db.go +++ b/db.go @@ -34,7 +34,6 @@ func db_get_transactions(transactions *[]Transaction, r *GetTransactionPaginatio func db_new_transaction(transaction Transaction) error { db, err := sqlx.Connect(DB_TYPE, DB_CONNECTION_STRING) if err != nil { - log.Info() log.Fatal(). Err(err). Msg("Fatal error in db_get_transactions\nCannot connect to server") diff --git a/flake.nix b/flake.nix index 9b96797..33f21d6 100644 --- a/flake.nix +++ b/flake.nix @@ -73,9 +73,6 @@ pkgs = nixpkgsFor.${system}; in { default = pkgs.mkShell { - shellHook = '' - alias gotest='templ generate;go run . -d' - ''; buildInputs = with pkgs; [ air # live go app reloading go diff --git a/web/index.templ b/web/hello.templ similarity index 100% rename from web/index.templ rename to web/hello.templ diff --git a/web/index_templ.go b/web/hello_templ.go similarity index 94% rename from web/index_templ.go rename to web/hello_templ.go index d3f21fc..b232b72 100644 --- a/web/index_templ.go +++ b/web/hello_templ.go @@ -30,7 +30,7 @@ func hello(name string) templ.Component { var templ_7745c5c3_Var2 string templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(name) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/index.templ`, Line: 3, Col: 21} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/hello.templ`, Line: 3, Col: 21} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2)) if templ_7745c5c3_Err != nil { diff --git a/web/router.go b/web/router.go index 8e4eea7..1688919 100644 --- a/web/router.go +++ b/web/router.go @@ -2,6 +2,7 @@ package web import ( "net/http" + "html/template" "context" @@ -10,6 +11,12 @@ import ( "github.com/rs/zerolog/log" ) +const TemplateDir = "./web/templates/" + +type IndexData struct { + Title string +} + func SetLogLevel(level zerolog.Level) { zerolog.SetGlobalLevel(level) } @@ -17,10 +24,22 @@ func SetLogLevel(level zerolog.Level) { func WebRouter() http.Handler { r := chi.NewRouter() r.Get("/", getIndex) + r.Get("/hello", getHello) return r } func getIndex(w http.ResponseWriter, req *http.Request) { + index, err := template.ParseFiles(TemplateDir + "index.html") + if err != nil { + log.Fatal(). + Err(err). + Msg("Fatal error reading index template") + } + + err = index.Execute(w, IndexData{Title: "thetitle"}) +} + +func getHello(w http.ResponseWriter, req *http.Request) { log.Debug().Msg("Got index") component := hello("Nick") component.Render(context.Background(), w) diff --git a/web/templates/index.html b/web/templates/index.html new file mode 100644 index 0000000..3998d1d --- /dev/null +++ b/web/templates/index.html @@ -0,0 +1,12 @@ + + + + + + {{.Title}} + + +

Hello World!

+ Hello you! + +