type Context struct{ //origin objects Writer http.ResponseWriter Req *http.Request //request info Path string Method string //response info StatusCode int }
// HandlerFunc defines the request handler used by Gogee type HandlerFunc func(*Context)
// Engine implement the interface of ServeHTTP type Engine struct { router *router }
// New is the constructor of Gogee.Engine funcNew() *Engine { return &Engine{router: newRouter()} }
// addRoute is the way to add something to route func(engine *Engine) addRoute(method string, pattern string, handler HandlerFunc) { log.Printf("Route %4s - %s", method, pattern) engine.router.addRoute(method, pattern, handler) }
// GET defines the method to add GET request func(engine *Engine) GET(pattern string, handler HandlerFunc) { engine.addRoute("GET", pattern, handler) }
//POST defines the method to POST request func(engine *Engine) POST(pattern string, handler HandlerFunc) { engine.addRoute("POST", pattern, handler) }
//Run definesd the method to start a http server func(engine *Engine) Run(addr string) (err error) { return http.ListenAndServe(addr, engine) }
// To use the ListenAndServe, we need to set a ServeHTTP struct func(engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) { c := newContext(w, req) engine.router.handle(c) }