|
| 1 | +/*! |
| 2 | + * rest-api-framework |
| 3 | + * Copyright(c) 2019 Roshan Gade |
| 4 | + * MIT Licensed |
| 5 | + */ |
| 6 | +package rest |
| 7 | + |
| 8 | +import ( |
| 9 | + "errors" |
| 10 | + "fmt" |
| 11 | + "github.com/go-rs/rest-api-framework/utils" |
| 12 | + "net/http" |
| 13 | + "regexp" |
| 14 | +) |
| 15 | + |
| 16 | +type Handler func(ctx *Context) |
| 17 | + |
| 18 | +/** |
| 19 | + * API - Application |
| 20 | + */ |
| 21 | +type API struct { |
| 22 | + prefix string |
| 23 | + routes []route |
| 24 | + interceptors []interceptor |
| 25 | + exceptions []exception |
| 26 | + unhandled Handler |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Route |
| 31 | + */ |
| 32 | +type route struct { |
| 33 | + method string |
| 34 | + pattern string |
| 35 | + regex *regexp.Regexp |
| 36 | + params []string |
| 37 | + handle Handler |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Request interceptor |
| 42 | + */ |
| 43 | +type interceptor struct { |
| 44 | + handle Handler |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Exception Route |
| 49 | + */ |
| 50 | +type exception struct { |
| 51 | + message string |
| 52 | + handle Handler |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Common Route |
| 57 | + */ |
| 58 | +func (api *API) Route(method string, pattern string, handle Handler) { |
| 59 | + regex, params, err := utils.Compile(pattern) |
| 60 | + if err != nil { |
| 61 | + fmt.Println("Error in pattern", err) |
| 62 | + panic(1) |
| 63 | + } |
| 64 | + api.routes = append(api.routes, route{ |
| 65 | + method: method, |
| 66 | + pattern: pattern, |
| 67 | + regex: regex, |
| 68 | + params: params, |
| 69 | + handle: handle, |
| 70 | + }) |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Required handle for http module |
| 75 | + */ |
| 76 | +func (api API) ServeHTTP(res http.ResponseWriter, req *http.Request) { |
| 77 | + |
| 78 | + urlPath := []byte(req.URL.Path) |
| 79 | + |
| 80 | + ctx := Context{ |
| 81 | + Request: req, |
| 82 | + Response: res, |
| 83 | + } |
| 84 | + |
| 85 | + // STEP 1: initialize context |
| 86 | + ctx.init() |
| 87 | + defer ctx.destroy() |
| 88 | + |
| 89 | + // STEP 2: execute all interceptors |
| 90 | + for _, task := range api.interceptors { |
| 91 | + if ctx.end || ctx.err != nil { |
| 92 | + break |
| 93 | + } |
| 94 | + |
| 95 | + task.handle(&ctx) |
| 96 | + } |
| 97 | + |
| 98 | + // STEP 3: check routes |
| 99 | + for _, route := range api.routes { |
| 100 | + if ctx.end || ctx.err != nil { |
| 101 | + break |
| 102 | + } |
| 103 | + |
| 104 | + if (route.method == "" || route.method == req.Method) && route.regex.Match(urlPath) { |
| 105 | + ctx.found = route.method != "" //? |
| 106 | + ctx.Params = utils.Exec(route.regex, route.params, urlPath) |
| 107 | + route.handle(&ctx) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // STEP 4: check handled exceptions |
| 112 | + for _, exp := range api.exceptions { |
| 113 | + if ctx.end || ctx.err == nil { |
| 114 | + break |
| 115 | + } |
| 116 | + |
| 117 | + if exp.message == ctx.err.Error() { |
| 118 | + exp.handle(&ctx) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + // STEP 5: unhandled exceptions |
| 123 | + if !ctx.end { |
| 124 | + if ctx.err == nil && !ctx.found { |
| 125 | + ctx.err = errors.New("URL_NOT_FOUND") |
| 126 | + } |
| 127 | + |
| 128 | + if api.unhandled != nil { |
| 129 | + api.unhandled(&ctx) |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + // STEP 6: system handle |
| 134 | + if !ctx.end { |
| 135 | + ctx.unhandledException() |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +func (api *API) Use(handle Handler) { |
| 140 | + task := interceptor{ |
| 141 | + handle: handle, |
| 142 | + } |
| 143 | + api.interceptors = append(api.interceptors, task) |
| 144 | +} |
| 145 | + |
| 146 | +func (api *API) All(pattern string, handle Handler) { |
| 147 | + api.Route("", pattern, handle) |
| 148 | +} |
| 149 | + |
| 150 | +func (api *API) Get(pattern string, handle Handler) { |
| 151 | + api.Route("GET", pattern, handle) |
| 152 | +} |
| 153 | + |
| 154 | +func (api *API) Post(pattern string, handle Handler) { |
| 155 | + api.Route("POST", pattern, handle) |
| 156 | +} |
| 157 | + |
| 158 | +func (api *API) Put(pattern string, handle Handler) { |
| 159 | + api.Route("PUT", pattern, handle) |
| 160 | +} |
| 161 | + |
| 162 | +func (api *API) Delete(pattern string, handle Handler) { |
| 163 | + api.Route("DELETE", pattern, handle) |
| 164 | +} |
| 165 | + |
| 166 | +func (api *API) Options(pattern string, handle Handler) { |
| 167 | + api.Route("OPTIONS", pattern, handle) |
| 168 | +} |
| 169 | + |
| 170 | +func (api *API) Head(pattern string, handle Handler) { |
| 171 | + api.Route("HEAD", pattern, handle) |
| 172 | +} |
| 173 | + |
| 174 | +func (api *API) Patch(pattern string, handle Handler) { |
| 175 | + api.Route("PATCH", pattern, handle) |
| 176 | +} |
| 177 | + |
| 178 | +func (api *API) Exception(err string, handle Handler) { |
| 179 | + exp := exception{ |
| 180 | + message: err, |
| 181 | + handle: handle, |
| 182 | + } |
| 183 | + api.exceptions = append(api.exceptions, exp) |
| 184 | +} |
| 185 | + |
| 186 | +func (api *API) UnhandledException(handle Handler) { |
| 187 | + api.unhandled = handle |
| 188 | +} |
0 commit comments