package srv

import (
	"net/http"
)

var router *http.ServeMux

/*
Our Special HandleFunc
*/
func Route(pattern string, handler func(req *Req, res Res)) {

	realHandler := func(w http.ResponseWriter, r *http.Request) {

		var req Req
		var res Res

		req.Construct(r)
		res.Construct(w, r)

		// run middleware on each request
		for _, fun := range middleware {
			cont := fun(&req, res)
			if !cont {
				return
			}
		}

		handler(&req, res)

	}

	router.HandleFunc(pattern, realHandler)
}

func InitRouter() {
	router = http.NewServeMux()

	// Public Files
	fs := http.FileServer(http.Dir("app/pub"))
	router.Handle("GET /app/pub/", http.StripPrefix("/app/pub/", fs))
}
