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

33
main.go
View File

@ -132,7 +132,7 @@ func exists(path string) (bool, error) {
func handler(w http.ResponseWriter, r *http.Request) { func handler(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path 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)$`) // var re = regexp.MustCompile(`\.(svg|jpg|jpeg|png|webp|ico|css|js)$`)
@ -142,6 +142,21 @@ func handler(w http.ResponseWriter, r *http.Request) {
// return // return
// } // }
var tmpl string
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) exists, err := exists(loc)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -151,29 +166,15 @@ func handler(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r) http.NotFound(w, r)
return return
} }
var tmpl string
userid, err := r.Cookie("user-id")
if errors.As(err, &http.ErrNoCookie) {
tmpl = "templates/login.html"
t, _ := template.ParseFiles(tmpl)
t.Execute(w, config)
} else {
fmt.Println(userid)
switch path {
case "/":
tmpl = "templates/home.html" tmpl = "templates/home.html"
t, _ := template.ParseFiles(tmpl) t, _ := template.ParseFiles(tmpl)
t.Execute(w, config) t.Execute(w, config)
default: default:
tmpl = fmt.Sprintf("templates%s", path) tmpl = fmt.Sprintf("templates%s.html", path)
t, _ := template.ParseFiles(tmpl) t, _ := template.ParseFiles(tmpl)
t.Execute(w, config) t.Execute(w, config)
} }
} }
}
func loginHandler(w http.ResponseWriter, r *http.Request) { func loginHandler(w http.ResponseWriter, r *http.Request) {
// userid, err := r.Cookie("user-id") // userid, err := r.Cookie("user-id")