init: basic index and static routes
This commit is contained in:
@@ -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)))
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user