Skip to content

Commit

Permalink
added pages
Browse files Browse the repository at this point in the history
  • Loading branch information
PeanutBrrutter committed Jun 2, 2024
1 parent 6892e77 commit 78241d6
Show file tree
Hide file tree
Showing 16 changed files with 633 additions and 29 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.4 // indirect
github.com/joho/godotenv v1.5.1 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ github.com/googleapis/enterprise-certificate-proxy v0.3.2 h1:Vie5ybvEvT75RniqhfF
github.com/googleapis/enterprise-certificate-proxy v0.3.2/go.mod h1:VLSiSSBs/ksPL8kq3OBOQ6WRI2QnaFynd1DCjZ62+V0=
github.com/googleapis/gax-go/v2 v2.12.4 h1:9gWcmF85Wvq4ryPFvGFaOgPIs1AQX0d0bcbGw4Z96qg=
github.com/googleapis/gax-go/v2 v2.12.4/go.mod h1:KYEYLorsnIGDi/rPC8b5TdlB9kbKoFubselGIoBMCwI=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
107 changes: 79 additions & 28 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,96 @@ import (
"fmt"
"html/template"
"net/http"
"os"
"path/filepath"
)

var templates = template.Must(template.ParseFiles("hello.html"))
var homePages = map[string]string{
"": "index.html",
"about": "about.html",
"contact": "contact.html",
"login": "login.html",
}

var adminPages = map[string]string{
"": "index.html",
"about": "about.html",
}

var tmpl *template.Template
var adminTmpl *template.Template
var templateDir = "templates"

func init() {
fmt.Println("Hello init!")
var err error
tmpl, err = template.ParseGlob(filepath.Join(templateDir, "*.html"))
if err != nil {
fmt.Println("Error parsing home templates: ", err)
os.Exit(1)
}

adminTmpl, err = template.ParseGlob(filepath.Join(templateDir, "admin", "*.html"))
if err != nil {
fmt.Println("Error parsing admin templates: ", err)
os.Exit(1)
}
}

func handler() http.Handler {
r := http.NewServeMux()
r.HandleFunc("/1", serverhome)
r.HandleFunc("/2", setCookieHandler)
return r
mux := http.NewServeMux()
mux.HandleFunc("/admin/", adminHandler)
mux.HandleFunc("/", mainHandler)
fmt.Println("Server started")
http.ListenAndServe(":8080", mux)
// r.HandleFunc("/1", serverhome)
// r.HandleFunc("/2", setCookieHandler)
return mux
}

func serverhome(w http.ResponseWriter, r *http.Request) {
// w.Write([]byte("Hello World!"))
fmt.Println(r)
templates.ExecuteTemplate(w, "hello.html", nil)
func adminHandler(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path[len("/admin/"):]
templateFile, ok := adminPages[path]
if !ok {
http.NotFound(w, r)
return
}
adminTmpl.ExecuteTemplate(w, templateFile, nil)
}

func setCookieHandler(w http.ResponseWriter, r *http.Request) {
// Initialize a new cookie containing the string "Hello world!" and some
// non-default attributes.
cookie := http.Cookie{
Name: "exampleCookie",
Value: "Hello world!",
// Path: "/",
// MaxAge: 3600,
// HttpOnly: true,
// Secure: true,
// SameSite: http.SameSiteLaxMode,
func mainHandler(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path[1:]
templateFile, ok := homePages[path]
if !ok {
http.NotFound(w, r)
return
}
tmpl.ExecuteTemplate(w, templateFile, nil)
}

// Use the http.SetCookie() function to send the cookie to the client.
// Behind the scenes this adds a `Set-Cookie` header to the response
// containing the necessary cookie data.
http.SetCookie(w, &cookie)
// func serverhome(w http.ResponseWriter, r *http.Request) {
// // w.Write([]byte("Hello World!"))
// fmt.Println(r)
// templates.ExecuteTemplate(w, "hello.html", nil)
// }

// Write a HTTP response as normal.
w.Write([]byte("cookie set!"))
}
// func setCookieHandler(w http.ResponseWriter, r *http.Request) {
// // Initialize a new cookie containing the string "Hello world!" and some
// // non-default attributes.
// cookie := http.Cookie{
// Name: "exampleCookie",
// Value: "Hello world!",
// // Path: "/",
// // MaxAge: 3600,
// // HttpOnly: true,
// // Secure: true,
// // SameSite: http.SameSiteLaxMode,
// }

// // Use the http.SetCookie() function to send the cookie to the client.
// // Behind the scenes this adds a `Set-Cookie` header to the response
// // containing the necessary cookie data.
// http.SetCookie(w, &cookie)

// // Write a HTTP response as normal.
// w.Write([]byte("cookie set!"))
// }
2 changes: 1 addition & 1 deletion server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestRouter(t *testing.T) {
}

rr := httptest.NewRecorder()
handler := http.HandlerFunc(serverhome)
handler := http.HandlerFunc(mainHandler)

handler.ServeHTTP(rr, req)
// Check the status code is what we expect.
Expand Down
30 changes: 30 additions & 0 deletions templates/about.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>About</title>
<style>
/* Body styles */
body {
background-color: #dad8c9; /* Light blue background */
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}

/* Header styles */
h1 {
color: #6a6767; /* Yellow for header */
font-size: 2em;
text-align: center;
margin: 20px 0; /* Add some top and bottom margin */
}
</style>
</head>
<body>
{{template "header.html" .}}
<p></p>
<h1>About</h1>
<div>This is an about page.</div>
</body>
</html>
68 changes: 68 additions & 0 deletions templates/admin/header.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<html>
<head>
<title>My Website</title>
<style>
body {
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
}

header {
background-color: #f0f0f0;
padding: 20px;
display: flex; /* Make header elements flexible */
justify-content: space-between; /* Space buttons evenly */
}

h1 {
margin: 0;
font-size: 2em;
}

nav {
display: flex; /* Make buttons behave as flex items */
}

nav a {
margin: 0 10px; /* Adjust spacing between buttons */
text-decoration: none;
color: #333;
padding: 10px 20px; /* Add padding for buttons */
border: 1px solid #ddd; /* Add simple border */
border-radius: 5px; /* Add rounded corners */
}

nav a:hover {
background-color: #eee;
color: #000;
}

/* Responsive Styles (for smaller screens) */
@media (max-width: 768px) {
header {
flex-direction: column; /* Stack elements vertically */
}

nav {
flex-direction: column; /* Make buttons stack vertically */
}

nav a {
margin-bottom: 10px; /* Adjust spacing for stacked buttons */
}
}
</style>
</head>
<body>
<header>
<h1>NK Robotics - Admin</h1>
<nav>
<a href="http://localhost:8080/admin/">Home</a>
<a href="http://localhost:8080/admin/about">About</a>
<a href="contact">Contact</a>
<a href="login">Login</a>
</nav>
</header>
</body>
</html>
30 changes: 30 additions & 0 deletions templates/admin/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Management</title>
<style>
/* Body styles */
body {
background-color: #dad8c9; /* Light blue background */
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}

/* Header styles */
h1 {
color: #6a6767; /* Yellow for header */
font-size: 2em;
text-align: center;
margin: 20px 0; /* Add some top and bottom margin */
}
</style>
</head>
<body>
{{template "header.html" .}}
<p></p>
<h1>Admin Management Panel</h1>
<div>Admin Management here.</div>
</body>
</html>
30 changes: 30 additions & 0 deletions templates/contact.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Contact Us</title>
<style>
/* Body styles */
body {
background-color: #dad8c9; /* Light blue background */
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}

/* Header styles */
h1 {
color: #6a6767; /* Yellow for header */
font-size: 2em;
text-align: center;
margin: 20px 0; /* Add some top and bottom margin */
}
</style>
</head>
<body>
{{template "header.html" .}}
<p></p>
<h1>Contact Us</h1>
<div>This is a contact page.</div>
</body>
</html>
68 changes: 68 additions & 0 deletions templates/header.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<html>
<head>
<title>My Website</title>
<style>
body {
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
}

header {
background-color: #f0f0f0;
padding: 20px;
display: flex; /* Make header elements flexible */
justify-content: space-between; /* Space buttons evenly */
}

h1 {
margin: 0;
font-size: 2em;
}

nav {
display: flex; /* Make buttons behave as flex items */
}

nav a {
margin: 0 10px; /* Adjust spacing between buttons */
text-decoration: none;
color: #333;
padding: 10px 20px; /* Add padding for buttons */
border: 1px solid #ddd; /* Add simple border */
border-radius: 5px; /* Add rounded corners */
}

nav a:hover {
background-color: #eee;
color: #000;
}

/* Responsive Styles (for smaller screens) */
@media (max-width: 768px) {
header {
flex-direction: column; /* Stack elements vertically */
}

nav {
flex-direction: column; /* Make buttons stack vertically */
}

nav a {
margin-bottom: 10px; /* Adjust spacing for stacked buttons */
}
}
</style>
</head>
<body>
<header>
<h1>NK Robotics</h1>
<nav>
<a href="http://localhost:8080/">Home</a>
<a href="about">About</a>
<a href="contact">Contact</a>
<a href="login">Login</a>
</nav>
</header>
</body>
</html>
Loading

0 comments on commit 78241d6

Please sign in to comment.