iron out some bugs
This commit is contained in:
parent
5525e96125
commit
e7a522fba7
55
main.go
55
main.go
|
@ -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,36 +142,37 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
// return
|
// return
|
||||||
// }
|
// }
|
||||||
|
|
||||||
exists, err := exists(loc)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !exists {
|
|
||||||
http.NotFound(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var tmpl string
|
var tmpl string
|
||||||
|
|
||||||
userid, err := r.Cookie("user-id")
|
validpath := regexp.MustCompile(`^/(signup|login|hello)$`)
|
||||||
if errors.As(err, &http.ErrNoCookie) {
|
m := validpath.FindStringSubmatch(path)
|
||||||
tmpl = "templates/login.html"
|
|
||||||
|
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, _ := template.ParseFiles(tmpl)
|
||||||
t.Execute(w, config)
|
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in New Issue