From ae380d197604d3def4b07d0cb1de7a5891f7e7d2 Mon Sep 17 00:00:00 2001 From: efim Date: Sat, 28 Oct 2023 14:47:01 +0000 Subject: [PATCH] init: basic index and static routes --- main.go | 1 + routes/routes.go | 25 +++++++++++++++++++++---- routes/static/hello.txt | 1 + routes/templates/index.gohtml | 26 ++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 routes/static/hello.txt create mode 100644 routes/templates/index.gohtml diff --git a/main.go b/main.go index c20668a..d79483b 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ func main() { flag.Parse() fmt.Printf("Server will start on port %d", port) + routes.RegisterRoutes() log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil)) diff --git a/routes/routes.go b/routes/routes.go index 7da917b..41c00b5 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -1,15 +1,32 @@ package routes import ( - "fmt" + "embed" + "html/template" + "log" "net/http" ) +//go:embed templates +var templateFs embed.FS + +//go:embed static +var staticFilesFs embed.FS + func RegisterRoutes() { + + // main page template http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - someString := "hello" - someNum := 123 - fmt.Fprintf(w, "A response, with values %s and %d", someString, someNum) + var templFile = "templates/index.gohtml" + tmpl := template.Must(template.ParseFS(templateFs, templFile)) + err := tmpl.Execute(w, 15) + if err != nil { + log.Printf("my error in executing template, huh\n %s", err) + } }) + // static resources route + http.Handle("/static/", + http.FileServer(http.FS(staticFilesFs))) + } diff --git a/routes/static/hello.txt b/routes/static/hello.txt new file mode 100644 index 0000000..483e498 --- /dev/null +++ b/routes/static/hello.txt @@ -0,0 +1 @@ +this is some static file, it's ok diff --git a/routes/templates/index.gohtml b/routes/templates/index.gohtml new file mode 100644 index 0000000..9e5fe84 --- /dev/null +++ b/routes/templates/index.gohtml @@ -0,0 +1,26 @@ + + + + + + Untitled + + + + + + + + + + +

Hello

+

This is index

+ +