diff --git a/main.go b/main.go index e9df583..d2d952d 100644 --- a/main.go +++ b/main.go @@ -132,7 +132,7 @@ func exists(path string) (bool, error) { func handler(w http.ResponseWriter, r *http.Request) { path := r.URL.Path - loc := fmt.Sprintf("templates%s", path) + loc := fmt.Sprintf("templates%s.html", path) // var re = regexp.MustCompile(`\.(svg|jpg|jpeg|png|webp|ico|css|js)$`) @@ -142,36 +142,37 @@ func handler(w http.ResponseWriter, r *http.Request) { // return // } - exists, err := exists(loc) - if err != nil { - log.Fatal(err) - } - - if !exists { - http.NotFound(w, r) - return - } - var tmpl string - userid, err := r.Cookie("user-id") - if errors.As(err, &http.ErrNoCookie) { - tmpl = "templates/login.html" + validpath := regexp.MustCompile(`^/(signup|login|hello)$`) + m := validpath.FindStringSubmatch(path) + + if m == nil { + userid, err := r.Cookie("user-id") + if errors.As(err, &http.ErrNoCookie) { + http.Redirect(w, r, "/hello", http.StatusFound) + } + fmt.Print(userid) + } + + switch path { + case "/": + exists, err := exists(loc) + if err != nil { + log.Fatal(err) + } + + if !exists { + http.NotFound(w, r) + return + } + tmpl = "templates/home.html" + t, _ := template.ParseFiles(tmpl) + t.Execute(w, config) + default: + tmpl = fmt.Sprintf("templates%s.html", path) t, _ := template.ParseFiles(tmpl) t.Execute(w, config) - } else { - fmt.Println(userid) - - switch path { - case "/": - tmpl = "templates/home.html" - t, _ := template.ParseFiles(tmpl) - t.Execute(w, config) - default: - tmpl = fmt.Sprintf("templates%s", path) - t, _ := template.ParseFiles(tmpl) - t.Execute(w, config) - } } } diff --git a/templates/login.html b/templates/hello.html similarity index 100% rename from templates/login.html rename to templates/hello.html