Started Go; Added Home, About

This commit is contained in:
Patrick Hatsune 2024-07-06 02:04:33 -07:00
commit 9f595558b6
Signed by: keyemail
GPG key ID: 6FD1A0FDB0D914C2
12 changed files with 184 additions and 0 deletions

0
.gitignore vendored Normal file
View file

3
README.md Normal file
View file

@ -0,0 +1,3 @@
# MBot Website
The website that provides an explanation to MBot! There will be more future updates for this website but for now it basically just shows off the cool things it can do.

3
go.mod Normal file
View file

@ -0,0 +1,3 @@
module mbot
go 1.22.3

63
main.go Normal file
View file

@ -0,0 +1,63 @@
package main
import (
"net/http"
"os"
"log"
"html/template"
)
type Index struct {
Title string
}
var data = &Index {
Title: "Website",
}
func aboutHandler(w http.ResponseWriter, req *http.Request) {
templates := template.Must(template.ParseFiles(
"src/index.tmpl",
"src/pages/about.tmpl",
"src/components/navbar.tmpl",
"src/components/footer.tmpl",
))
err := templates.ExecuteTemplate(w, "base", data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func indexHandler(w http.ResponseWriter, req *http.Request) {
templates := template.Must(template.ParseFiles(
"src/index.tmpl",
"src/pages/home.tmpl",
"src/components/navbar.tmpl",
"src/components/footer.tmpl",
))
err := templates.ExecuteTemplate(w, "base", data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func main() {
if _, err := os.Stat("src/index.tmpl"); os.IsNotExist(err) {
log.Fatal("Template file does not exist:\n", err)
}
fs := http.FileServer(http.Dir("./src/"))
http.Handle("/src/", http.StripPrefix("/src/", fs))
port := ":3939"
http.HandleFunc("/", indexHandler)
http.HandleFunc("/about", aboutHandler)
log.Println("Server running at http://localhost" + port)
http.ListenAndServe(port, nil)
}

View file

@ -0,0 +1,5 @@
{{ define "footer" }}
<footer>
<h2>My footer!</h2>
</footer>
{{ end }}

View file

@ -0,0 +1,16 @@
{{ define "navbar" }}
<nav>
<h1>navbar</h1>
<ul>
<a href="/">
<li>Home</li>
</a>
<a href="/about">
<li>About</li>
</a>
<a href="https://git.keyemail.dev/MBot/mbot">
<li>GitHub</li>
</a>
</ul>
</nav>
{{ end }}

17
src/index.tmpl Normal file
View file

@ -0,0 +1,17 @@
{{ define "base" }}
<!DOCTYPE html>
<html lang="en">
<head>
{{ template "head" . }}
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>{{ .Title }}</title>
<link rel="stylesheet" href="/src/styles/main.css">
</head>
<body>
{{ template "navbar" . }}
{{ template "body" . }}
{{ template "footer" . }}
</body>
</html>
{{ end }}

12
src/pages/about.tmpl Normal file
View file

@ -0,0 +1,12 @@
<head>
{{ define "head" }}
<link rel="stylesheet" href="/src/styles/about.css">
{{ end }}
</head>
<body>
{{ define "body" }}
<header>
<h1>About page!</h1>
</header>
{{ end }}
</body>

12
src/pages/home.tmpl Normal file
View file

@ -0,0 +1,12 @@
<head>
{{ define "head" }}
<link rel="stylesheet" href="/src/styles/home.css">
{{ end }}
</head>
<body>
{{ define "body" }}
<header>
<h1>Home page!</h1>
</header>
{{ end }}
</body>

5
src/styles/about.css Normal file
View file

@ -0,0 +1,5 @@
header {
h1 {
margin-top: 30px;
}
}

5
src/styles/home.css Normal file
View file

@ -0,0 +1,5 @@
header {
h1 {
margin-top: 30px;
}
}

43
src/styles/main.css Normal file
View file

@ -0,0 +1,43 @@
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
body {
display: flex;
flex-direction: column;
justify-content: space-between;
max-width: 1920px;
margin: auto;
padding: 20px;
min-height: 100vh;
}
a {
color: inherit;
text-decoration: none;
}
nav {
display: flex;
align-items: center;
justify-content: space-between;
ul {
list-style: none;
float: right;
li {
display: inline;
cursor: pointer;
margin-left: 10px;
}
}
}
footer {
margin-top: auto;
width: 100%;
text-align: center;
}