Enabling Go Modules and refactoring our code to use packages
This commit is contained in:
19
cmd/web/main.go
Normal file
19
cmd/web/main.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"gitea.sitodosi.com/betology/curso/pkg/handlers"
|
||||||
|
)
|
||||||
|
|
||||||
|
var portNumber = ":8080"
|
||||||
|
|
||||||
|
// main is the main application function
|
||||||
|
func main() {
|
||||||
|
http.HandleFunc("/", handlers.Home)
|
||||||
|
http.HandleFunc("/about", handlers.About)
|
||||||
|
|
||||||
|
fmt.Println(fmt.Sprintf("Starting application on port %s", portNumber))
|
||||||
|
_ = http.ListenAndServe(portNumber, nil)
|
||||||
|
}
|
||||||
17
pkg/handlers/handlers.go
Normal file
17
pkg/handlers/handlers.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"gitea.sitodosi.com/betology/curso/pkg/render"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Home is the home page handler
|
||||||
|
func Home(w http.ResponseWriter, r *http.Request) {
|
||||||
|
render.RenderTemplate(w, "home.page.tmpl")
|
||||||
|
}
|
||||||
|
|
||||||
|
// About is the about page handler
|
||||||
|
func About(w http.ResponseWriter, r *http.Request) {
|
||||||
|
render.RenderTemplate(w, "about.page.tmpl")
|
||||||
|
}
|
||||||
17
pkg/render/render.go
Normal file
17
pkg/render/render.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package render
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"text/template"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RenderTemplate renders templates using html/template
|
||||||
|
func RenderTemplate(w http.ResponseWriter, tmpl string) {
|
||||||
|
parsedTemplate, _ := template.ParseFiles("./templates/" + tmpl)
|
||||||
|
err := parsedTemplate.Execute(w, nil)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("error parsing template:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user