|
| 1 | +/*! |
| 2 | + * utils-api-framework |
| 3 | + * Copyright(c) 2019 Roshan Gade |
| 4 | + * MIT Licensed |
| 5 | + */ |
| 6 | +package rest |
| 7 | + |
| 8 | +import ( |
| 9 | + "./utils" |
| 10 | + "fmt" |
| 11 | + "net/http" |
| 12 | + "regexp" |
| 13 | +) |
| 14 | + |
| 15 | +type API struct { |
| 16 | + routes []route |
| 17 | + interceptors []interceptor |
| 18 | + errors []errorHandler |
| 19 | +} |
| 20 | + |
| 21 | +type route struct { |
| 22 | + method string |
| 23 | + pattern string |
| 24 | + regex *regexp.Regexp |
| 25 | + params []string |
| 26 | + handle func(ctx *Context) |
| 27 | +} |
| 28 | + |
| 29 | +type interceptor struct { |
| 30 | + handle func(ctx *Context) |
| 31 | +} |
| 32 | + |
| 33 | +//TODO: check errors in language specifications |
| 34 | +type errorHandler struct { |
| 35 | + code string |
| 36 | + handle func(ctx *Context) |
| 37 | +} |
| 38 | + |
| 39 | +func (api *API) handler(method string, pattern string, handle func(ctx *Context)) { |
| 40 | + regex, params, err := utils.Compile(pattern) |
| 41 | + if err != nil { |
| 42 | + fmt.Println("Error in pattern", err) |
| 43 | + panic(1) |
| 44 | + } |
| 45 | + api.routes = append(api.routes, route{ |
| 46 | + method: method, |
| 47 | + pattern: pattern, |
| 48 | + regex: regex, |
| 49 | + params: params, |
| 50 | + handle: handle, |
| 51 | + }) |
| 52 | +} |
| 53 | + |
| 54 | +func (api API) ServeHTTP(res http.ResponseWriter, req *http.Request) { |
| 55 | + //fmt.Println("-------------------------------------") |
| 56 | + //fmt.Println("URI: ", req.RequestURI) |
| 57 | + //fmt.Println("Method: ", req.Method) |
| 58 | + //fmt.Println("Form: ", req.Form) |
| 59 | + //fmt.Println("RawPath: ", req.URL.RawPath) |
| 60 | + //fmt.Println("Header: ", req.Header) |
| 61 | + //fmt.Println("Path: ", req.URL.Path) |
| 62 | + //fmt.Println("RawQuery: ", req.URL.RawQuery) |
| 63 | + //fmt.Println("Opaque: ", req.URL.Opaque) |
| 64 | + //fmt.Println("-------------------------------------") |
| 65 | + urlPath := []byte(req.URL.Path) |
| 66 | + |
| 67 | + ctx := Context{ |
| 68 | + req: req, |
| 69 | + res: res, |
| 70 | + } |
| 71 | + |
| 72 | + ctx.init() |
| 73 | + |
| 74 | + for _, route := range api.interceptors { |
| 75 | + if ctx.err != nil { |
| 76 | + break |
| 77 | + } |
| 78 | + |
| 79 | + if ctx.end == true { |
| 80 | + break |
| 81 | + } |
| 82 | + |
| 83 | + route.handle(&ctx) |
| 84 | + } |
| 85 | + |
| 86 | + for _, route := range api.routes { |
| 87 | + if ctx.err != nil { |
| 88 | + break |
| 89 | + } |
| 90 | + |
| 91 | + if ctx.end == true { |
| 92 | + break |
| 93 | + } |
| 94 | + |
| 95 | + if route.method == req.Method && route.regex.Match(urlPath) { |
| 96 | + ctx.Params = utils.Exec(route.regex, route.params, urlPath) |
| 97 | + route.handle(&ctx) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + //TODO: NOT FOUND, INTERNAL SERVER ERROR, ETC |
| 102 | + |
| 103 | + for _, route := range api.errors { |
| 104 | + if ctx.end == true { |
| 105 | + break |
| 106 | + } |
| 107 | + |
| 108 | + if ctx.err != nil && route.code == ctx.err.Error() { |
| 109 | + route.handle(&ctx) |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +func (api *API) Use(handle func(ctx *Context)) { |
| 115 | + api.interceptors = append(api.interceptors, interceptor{handle: handle}) |
| 116 | +} |
| 117 | + |
| 118 | +func (api *API) GET(pattern string, handle func(ctx *Context)) { |
| 119 | + api.handler("GET", pattern, handle) |
| 120 | +} |
| 121 | + |
| 122 | +//TODO: POST, PUT, DELETE, OPTIONS, HEAD, PATCH |
| 123 | + |
| 124 | +func (api *API) Error(code string, handle func(ctx *Context)) { |
| 125 | + api.errors = append(api.errors, errorHandler{ |
| 126 | + code: code, |
| 127 | + handle: handle, |
| 128 | + }) |
| 129 | +} |
0 commit comments