From 78241d639cfd6f243a0e946f50017e61568ee102 Mon Sep 17 00:00:00 2001 From: PeanutBrrutter Date: Sun, 2 Jun 2024 16:38:57 +0800 Subject: [PATCH] added pages --- go.mod | 1 + go.sum | 2 + server.go | 107 +++++++++++++++++++++++-------- server_test.go | 2 +- templates/about.html | 30 +++++++++ templates/admin/header.html | 68 ++++++++++++++++++++ templates/admin/index.html | 30 +++++++++ templates/contact.html | 30 +++++++++ templates/header.html | 68 ++++++++++++++++++++ templates/index.html | 30 +++++++++ templates/instructor/header.html | 68 ++++++++++++++++++++ templates/instructor/index.html | 30 +++++++++ templates/parent/header.html | 68 ++++++++++++++++++++ templates/parent/index.html | 30 +++++++++ templates/student/header.html | 68 ++++++++++++++++++++ templates/student/index.html | 30 +++++++++ 16 files changed, 633 insertions(+), 29 deletions(-) create mode 100644 templates/about.html create mode 100644 templates/admin/header.html create mode 100644 templates/admin/index.html create mode 100644 templates/contact.html create mode 100644 templates/header.html create mode 100644 templates/index.html create mode 100644 templates/instructor/header.html create mode 100644 templates/instructor/index.html create mode 100644 templates/parent/header.html create mode 100644 templates/parent/index.html create mode 100644 templates/student/header.html create mode 100644 templates/student/index.html diff --git a/go.mod b/go.mod index 44fcf72..768b17c 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 363ccbf..b76f01c 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/server.go b/server.go index 6a383ea..706a320 100644 --- a/server.go +++ b/server.go @@ -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!")) +// } diff --git a/server_test.go b/server_test.go index 96e7ee2..6acc6b0 100644 --- a/server_test.go +++ b/server_test.go @@ -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. diff --git a/templates/about.html b/templates/about.html new file mode 100644 index 0000000..fbe852c --- /dev/null +++ b/templates/about.html @@ -0,0 +1,30 @@ + + + + + About + + + + {{template "header.html" .}} +

+

About

+
This is an about page.
+ + \ No newline at end of file diff --git a/templates/admin/header.html b/templates/admin/header.html new file mode 100644 index 0000000..99dd339 --- /dev/null +++ b/templates/admin/header.html @@ -0,0 +1,68 @@ + + + My Website + + + +
+

NK Robotics - Admin

+ +
+ + \ No newline at end of file diff --git a/templates/admin/index.html b/templates/admin/index.html new file mode 100644 index 0000000..e678e0d --- /dev/null +++ b/templates/admin/index.html @@ -0,0 +1,30 @@ + + + + + Management + + + + {{template "header.html" .}} +

+

Admin Management Panel

+
Admin Management here.
+ + \ No newline at end of file diff --git a/templates/contact.html b/templates/contact.html new file mode 100644 index 0000000..3e9acda --- /dev/null +++ b/templates/contact.html @@ -0,0 +1,30 @@ + + + + + Contact Us + + + + {{template "header.html" .}} +

+

Contact Us

+
This is a contact page.
+ + \ No newline at end of file diff --git a/templates/header.html b/templates/header.html new file mode 100644 index 0000000..04c0029 --- /dev/null +++ b/templates/header.html @@ -0,0 +1,68 @@ + + + My Website + + + +
+

NK Robotics

+ +
+ + \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..94febe1 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,30 @@ + + + + + Home + + + + {{template "header.html" .}} +

+

Home Page

+
This is a home page.
+ + \ No newline at end of file diff --git a/templates/instructor/header.html b/templates/instructor/header.html new file mode 100644 index 0000000..497e67a --- /dev/null +++ b/templates/instructor/header.html @@ -0,0 +1,68 @@ + + + My Website + + + +
+

NK Robotics - Instructor/h1> + +

+ + \ No newline at end of file diff --git a/templates/instructor/index.html b/templates/instructor/index.html new file mode 100644 index 0000000..d58e82d --- /dev/null +++ b/templates/instructor/index.html @@ -0,0 +1,30 @@ + + + + + Management + + + + {{template "header.html" .}} +

+

Instructor Management Panel

+
Instructor Management here.
+ + \ No newline at end of file diff --git a/templates/parent/header.html b/templates/parent/header.html new file mode 100644 index 0000000..f3bc787 --- /dev/null +++ b/templates/parent/header.html @@ -0,0 +1,68 @@ + + + My Website + + + +
+

NK Robotics - Parent

+ +
+ + \ No newline at end of file diff --git a/templates/parent/index.html b/templates/parent/index.html new file mode 100644 index 0000000..7f85da7 --- /dev/null +++ b/templates/parent/index.html @@ -0,0 +1,30 @@ + + + + + Management + + + + {{template "header.html" .}} +

+

Parent Management Panel

+
Parent Management here.
+ + \ No newline at end of file diff --git a/templates/student/header.html b/templates/student/header.html new file mode 100644 index 0000000..faea685 --- /dev/null +++ b/templates/student/header.html @@ -0,0 +1,68 @@ + + + My Website + + + +
+

NK Robotics - Student

+ +
+ + \ No newline at end of file diff --git a/templates/student/index.html b/templates/student/index.html new file mode 100644 index 0000000..06e2a1d --- /dev/null +++ b/templates/student/index.html @@ -0,0 +1,30 @@ + + + + + Management + + + + {{template "header.html" .}} +

+

Student Management Panel

+
Student Management here.
+ + \ No newline at end of file