iron out some bugs

This commit is contained in:
leah 2022-01-29 15:14:09 +00:00
parent 5525e96125
commit e7a522fba7
2 changed files with 28 additions and 27 deletions

55
main.go
View File

@ -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)
}
}
}