Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. func main() {
  2. db = models.DB{}
  3. db.Open()
  4. r := mux.NewRouter()
  5. r.HandleFunc("/index", IndexHandler).Methods("GET")
  6. r.HandleFunc("/add", AddHandler).Methods("POST")
  7. r.HandleFunc("/knife/{id}", KnifeHandler).Methods("GET")
  8.  
  9. r.PathPrefix("/").Handler(http.FileServer(http.Dir("./")))
  10.  
  11. http.Handle("/", r)
  12. http.ListenAndServe(":1111", nil)
  13. db.Close()
  14. }
  15.  
  16. func KnifeHandler(w http.ResponseWriter, r *http.Request) {
  17. vars := mux.Vars(r)
  18. tmpl, _ := template.ParseFiles("eachKnife.html")
  19. tmpl.ExecuteTemplate(w, "knife", nil)
  20. ID := vars["id"]
  21. knife := db.GetKnife(ID)
  22. knifeJSON, _ := json.Marshal(knife)
  23. w.Write(knifeJSON)
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement