From 91d14e414fc9c451879e834d7866ad765084a836 Mon Sep 17 00:00:00 2001 From: René Neumann Date: Sat, 10 Feb 2024 23:24:37 +0100 Subject: Add templating --- main.go | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 92ae59e..b8d2436 100644 --- a/main.go +++ b/main.go @@ -4,12 +4,13 @@ import ( "context" "database/sql" "flag" - "fmt" - "gosten/model" "log" "net" "net/http" "strconv" + + "gosten/model" + "gosten/templ" ) // flags @@ -32,22 +33,11 @@ func main() { } queries := model.New(db) + _, err = queries.GetUsers(context.Background()) - helloHandler := func(w http.ResponseWriter, req *http.Request) { - fmt.Fprintln(w, "Hello, world!") - fmt.Fprintf(w, "Running on %s:%d\n", host, port) - - users, err := queries.GetUsers(context.Background()) - if err != nil { - log.Fatal(err) - } - - fmt.Fprintln(w, "\nUSERS") - for _, u := range users { - fmt.Fprintln(w, u.Name) - } - } - http.HandleFunc("/", helloHandler) + http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) { + templ.Lookup("index").Execute(w, nil) + }) address := net.JoinHostPort(host, strconv.FormatUint(port, 10)) log.Fatal(http.ListenAndServe(address, nil)) -- cgit v1.2.3-54-g00ecf