rtr.go 661 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package srv
  2. import (
  3. "net/http"
  4. )
  5. var rtr *http.ServeMux
  6. /*
  7. Our Special HandleFunc
  8. */
  9. func Route(pattern string, handler func(req *Req, res Res)) {
  10. realHandler := func(w http.ResponseWriter, r *http.Request) {
  11. var req Req
  12. var res Res
  13. req.Construct(r)
  14. res.Construct(w, r)
  15. // run middleware on each request
  16. for _, fun := range middleware {
  17. cont := fun(&req, res)
  18. if !cont {
  19. return
  20. }
  21. }
  22. handler(&req, res)
  23. }
  24. rtr.HandleFunc(pattern, realHandler)
  25. }
  26. func InitRouter() {
  27. rtr = http.NewServeMux()
  28. // Public Files
  29. fs := http.FileServer(http.Dir("app/pub"))
  30. rtr.Handle("GET /app/pub/", http.StripPrefix("/app/pub/", fs))
  31. }