Files
proxy-lite/web.go

53 lines
1.2 KiB
Go

package main
import (
"html/template"
"log"
"net/http"
)
func handleMain(db *Database, pc *chan Noodle) func(w http.ResponseWriter, req *http.Request) {
tmpl, err := template.ParseFS(content, "templates/*.html")
if err != nil {
log.Fatalf("Error parsing templates: %v", err)
}
return func(w http.ResponseWriter, req *http.Request) {
data := db.GetAll()
err := tmpl.ExecuteTemplate(w, "index.html", data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
func handleDelete(db *Database, pc *chan Noodle) func(w http.ResponseWriter, req *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
q := req.URL.Query()
vals, ok := q["id"]
if ok {
id := vals[0]
noodle := db.Get(id)
noodle.IsUp = false
*pc <- noodle
log.Printf("Deleting noodle=%v", noodle)
//log.Printf("Deleting noodle=%s", id)
db.Delete(noodle.Id)
}
http.Redirect(w, req, "/", 307)
}
}
func handleAdd(db *Database, pc *chan Noodle) func(w http.ResponseWriter, req *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
http.Error(w, "", http.StatusMethodNotAllowed)
return
}
http.Redirect(w, req, "/", 307)
}
}