init: basic index and static routes
This commit is contained in:
parent
c7571d2e81
commit
ae380d1976
1
main.go
1
main.go
|
@ -14,6 +14,7 @@ func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
fmt.Printf("Server will start on port %d", port)
|
fmt.Printf("Server will start on port %d", port)
|
||||||
|
|
||||||
routes.RegisterRoutes()
|
routes.RegisterRoutes()
|
||||||
|
|
||||||
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
|
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
|
||||||
|
|
|
@ -1,15 +1,32 @@
|
||||||
package routes
|
package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"embed"
|
||||||
|
"html/template"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//go:embed templates
|
||||||
|
var templateFs embed.FS
|
||||||
|
|
||||||
|
//go:embed static
|
||||||
|
var staticFilesFs embed.FS
|
||||||
|
|
||||||
func RegisterRoutes() {
|
func RegisterRoutes() {
|
||||||
|
|
||||||
|
// main page template
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
someString := "hello"
|
var templFile = "templates/index.gohtml"
|
||||||
someNum := 123
|
tmpl := template.Must(template.ParseFS(templateFs, templFile))
|
||||||
fmt.Fprintf(w, "A response, with values %s and %d", someString, someNum)
|
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)))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
this is some static file, it's ok
|
|
@ -0,0 +1,26 @@
|
||||||
|
<!doctype html>
|
||||||
|
<html class="no-js" lang="">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta http-equiv="x-ua-compatible" content="ie=edge">
|
||||||
|
<title>Untitled</title>
|
||||||
|
<meta name="description" content="">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
|
||||||
|
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
|
||||||
|
<!-- Place favicon.ico in the root directory -->
|
||||||
|
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<!--[if lt IE 8]>
|
||||||
|
<p class="browserupgrade">
|
||||||
|
You are using an <strong>outdated</strong> browser. Please
|
||||||
|
<a href="http://browsehappy.com/">upgrade your browser</a> to improve
|
||||||
|
your experience.
|
||||||
|
</p>
|
||||||
|
<![endif]-->
|
||||||
|
|
||||||
|
<h1>Hello</h1>
|
||||||
|
<p>This is index</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in New Issue