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

+ +