iron out some bugs
This commit is contained in:
parent
5525e96125
commit
e7a522fba7
33
main.go
33
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,6 +142,21 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
|||
// 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)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
@ -151,28 +166,14 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
|||
http.NotFound(w, r)
|
||||
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"
|
||||
t, _ := template.ParseFiles(tmpl)
|
||||
t.Execute(w, config)
|
||||
default:
|
||||
tmpl = fmt.Sprintf("templates%s", path)
|
||||
tmpl = fmt.Sprintf("templates%s.html", path)
|
||||
t, _ := template.ParseFiles(tmpl)
|
||||
t.Execute(w, config)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func loginHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
Reference in New Issue
Block a user