Skip to content

Commit

Permalink
restructured
Browse files Browse the repository at this point in the history
  • Loading branch information
jagottsicher committed Mar 3, 2023
1 parent 4bf8c58 commit e8c805e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
6 changes: 4 additions & 2 deletions main.go → cmd/web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package main
import (
"fmt"
"net/http"

"github.com/jagottsicher/myGoWebApplication/pkg/handlers"
)

const portNumber = ":8080"

// main is the main function
func main() {
http.HandleFunc("/", Home)
http.HandleFunc("/about", About)
http.HandleFunc("/", handlers.Home)
http.HandleFunc("/about", handlers.About)

fmt.Println(fmt.Sprintf("Starting application on port %s", portNumber))
_ = http.ListenAndServe(portNumber, nil)
Expand Down
12 changes: 8 additions & 4 deletions handlers.go → pkg/handlers/handlers.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package main
package handlers

import "net/http"
import (
"net/http"

"github.com/jagottsicher/myGoWebApplication/pkg/render"
)

// Home is the handler for the home page
func Home(w http.ResponseWriter, r *http.Request) {
rendernTemplate(w, "home-page.tpml")
render.RenderTemplate(w, "home-page.tpml")
}

// About is the handler for the about page
func About(w http.ResponseWriter, r *http.Request) {
rendernTemplate(w, "about-page.tpml")
render.RenderTemplate(w, "about-page.tpml")
}
4 changes: 2 additions & 2 deletions render.go → pkg/render/render.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package render

import (
"fmt"
Expand All @@ -8,7 +8,7 @@ import (

// rendernTemplate serves as a wrapper and renders
// a template from folder /templates to a desired writer
func rendernTemplate(w http.ResponseWriter, tpml string) {
func RenderTemplate(w http.ResponseWriter, tpml string) {
parsedTemplate, _ := template.ParseFiles("./templates/" + tpml)
err := parsedTemplate.Execute(w, nil)
if err != nil {
Expand Down

0 comments on commit e8c805e

Please sign in to comment.