Skip to content

Commit

Permalink
adding the base application before structuring lessons
Browse files Browse the repository at this point in the history
  • Loading branch information
zachsdevpit committed Apr 12, 2024
1 parent 0795699 commit 209b3f4
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/zachsdevpit/GoWebDevBasics

go 1.22.1
113 changes: 113 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package main

import (
"bytes"
"html/template"
"log"
"net/http"
)

const homeTemplate = `<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<link rel="stylesheet" href="/static/styles.css">
</head>
<body>
<h1>Pick A Motorcycle</h1>
<button class="Suzuki"><a href="/suzuki">Suzuki</a></button>
<button class="Ducati"><a href="/ducati">Ducati</a></button>
<button class="Kawasaki"><a href="/kawasaki">Kawasaki</a></button>
</body>
</html>`

const motoTemplate = `<!DOCTYPE html>
<html>
<head>
<title>{{.Brand}}</title>
<link rel="stylesheet" href="/static/styles.css">
</head>
<body>
<h1 class="{{.Brand}}">{{.Brand}}</h1>
<p>{{.Model}}</p>
<button><a href="/"> <- Go Back</a></button>
</body>
</html>`

type Motorcycle struct {
Brand string
Model string
}

const (
homePath = "/"
motorcyclePath = "/{motorcycle}"
)

func main() {
fs := http.FileServer(http.Dir("./static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))

http.HandleFunc(homePath, serveHome)
http.HandleFunc(motorcyclePath, serveMotorcycle)

log.Println("Listening on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}

func renderTemplate(w http.ResponseWriter, templateName string, data interface{}) {
t, err := template.New(templateName).Parse(templateName)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

var buf bytes.Buffer
err = t.Execute(&buf, data)

if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "text/html; charset=UTF-8")
buf.WriteTo(w)
}

func serveHome(w http.ResponseWriter, r *http.Request) {
data := ""
renderTemplate(w, homeTemplate, data)
}

func serveMotorcycle(w http.ResponseWriter, r *http.Request) {
whichMotorcycle := r.PathValue("motorcycle")
data := findMotorcycle(whichMotorcycle)
renderTemplate(w, motoTemplate, data)
}

func findMotorcycle(brand string) *Motorcycle {
var m Motorcycle
switch brand {
case "ducati":
m = Motorcycle{
Brand: "Ducati",
Model: "The Red One",
}
case "suzuki":
m = Motorcycle{
Brand: "Suzuki",
Model: "The Blue One",
}
case "kawasaki":
m = Motorcycle{
Brand: "Kawasaki",
Model: "The Green One",
}
default:
m = Motorcycle{
Brand: "404",
Model: "We don't have that one",
}
}
return &m
}
16 changes: 16 additions & 0 deletions static/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* styles.css */
body {
background-color: #f0f0f0;
}

.Ducati {
color: #CB1517;
}

.Suzuki {
color: #0053B9;
}

.Kawasaki {
color: #6BBF23;
}

0 comments on commit 209b3f4

Please sign in to comment.