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 @@ + + +
+ +