|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + "net/http" |
| 6 | + |
| 7 | + "github.com/gorilla/mux" |
| 8 | + "github.com/tonyvugithub/statusCodeApi/helpers" |
| 9 | +) |
| 10 | + |
| 11 | +//Request handler |
| 12 | +func getStatusCode(w http.ResponseWriter, r *http.Request) { |
| 13 | + w.Header().Set("Content-Type", "application/json") |
| 14 | + vars := mux.Vars(r) |
| 15 | + code := vars["code"] |
| 16 | + switch code { |
| 17 | + case "200": |
| 18 | + w.WriteHeader(http.StatusOK) |
| 19 | + w.Write([]byte(`{"status": "200 OK"}`)) |
| 20 | + case "201": |
| 21 | + w.WriteHeader(http.StatusCreated) |
| 22 | + w.Write([]byte(`{"status": "201 Created"}`)) |
| 23 | + case "202": |
| 24 | + w.WriteHeader(http.StatusAccepted) |
| 25 | + w.Write([]byte(`{"status": "202 Accepted"}`)) |
| 26 | + case "203": |
| 27 | + w.WriteHeader(http.StatusNonAuthoritativeInfo) |
| 28 | + w.Write([]byte(`{"status": "203 Non Authorative Info"}`)) |
| 29 | + case "206": |
| 30 | + w.WriteHeader(http.StatusPartialContent) |
| 31 | + w.Write([]byte(`{"status": "206 Partial Content"}`)) |
| 32 | + case "207": |
| 33 | + w.WriteHeader(http.StatusMultiStatus) |
| 34 | + w.Write([]byte(`{"status": "207 Multi Status"}`)) |
| 35 | + case "208": |
| 36 | + w.WriteHeader(http.StatusAlreadyReported) |
| 37 | + w.Write([]byte(`{"status": "208 Already Reported"}`)) |
| 38 | + case "226": |
| 39 | + w.WriteHeader(http.StatusIMUsed) |
| 40 | + w.Write([]byte(`{"status": "226 IM Used"}`)) |
| 41 | + case "300": |
| 42 | + w.WriteHeader(http.StatusMultipleChoices) |
| 43 | + w.Write([]byte(`{"status": "300 Multiple Choices"}`)) |
| 44 | + case "302": |
| 45 | + w.WriteHeader(http.StatusFound) |
| 46 | + w.Write([]byte(`{"status": "302 Found"}`)) |
| 47 | + case "303": |
| 48 | + w.WriteHeader(http.StatusSeeOther) |
| 49 | + w.Write([]byte(`{"status": "303 See Other"}`)) |
| 50 | + case "305": |
| 51 | + w.WriteHeader(http.StatusUseProxy) |
| 52 | + w.Write([]byte(`{"status": "305 Use Proxy"}`)) |
| 53 | + case "307": |
| 54 | + w.WriteHeader(http.StatusTemporaryRedirect) |
| 55 | + w.Write([]byte(`{"status": "307 Temporary Redirect"}`)) |
| 56 | + case "308": |
| 57 | + w.WriteHeader(http.StatusPermanentRedirect) |
| 58 | + w.Write([]byte(`{"status": "308 Permanent Redirect"}`)) |
| 59 | + case "400": |
| 60 | + w.WriteHeader(http.StatusBadRequest) |
| 61 | + w.Write([]byte(`{"status": "400 Bad Request"}`)) |
| 62 | + case "401": |
| 63 | + w.WriteHeader(http.StatusUnauthorized) |
| 64 | + w.Write([]byte(`{"status": "401 Unauthorized"}`)) |
| 65 | + case "402": |
| 66 | + w.WriteHeader(http.StatusPaymentRequired) |
| 67 | + w.Write([]byte(`{"status": "402 Payment Required"}`)) |
| 68 | + case "403": |
| 69 | + w.WriteHeader(http.StatusForbidden) |
| 70 | + w.Write([]byte(`{"status": "403 Forbidden"}`)) |
| 71 | + case "404": |
| 72 | + w.WriteHeader(http.StatusNotFound) |
| 73 | + w.Write([]byte(`{"status": "404 Not Found"}`)) |
| 74 | + case "405": |
| 75 | + w.WriteHeader(http.StatusMethodNotAllowed) |
| 76 | + w.Write([]byte(`{"status": "405 Method Not Allowed"}`)) |
| 77 | + case "406": |
| 78 | + w.WriteHeader(http.StatusNotAcceptable) |
| 79 | + w.Write([]byte(`{"status": "406 Not Acceptable"}`)) |
| 80 | + case "408": |
| 81 | + w.WriteHeader(http.StatusRequestTimeout) |
| 82 | + w.Write([]byte(`{"status": "408 Request Timeout"}`)) |
| 83 | + case "409": |
| 84 | + w.WriteHeader(http.StatusConflict) |
| 85 | + w.Write([]byte(`{"status": "409 Conflict"}`)) |
| 86 | + case "410": |
| 87 | + w.WriteHeader(http.StatusGone) |
| 88 | + w.Write([]byte(`{"status": "410 Gone"}`)) |
| 89 | + case "411": |
| 90 | + w.WriteHeader(http.StatusLengthRequired) |
| 91 | + w.Write([]byte(`{"status": "411 Length Required"}`)) |
| 92 | + case "412": |
| 93 | + w.WriteHeader(http.StatusPreconditionFailed) |
| 94 | + w.Write([]byte(`{"status": "412 Precondition Failed"}`)) |
| 95 | + case "413": |
| 96 | + w.WriteHeader(http.StatusRequestEntityTooLarge) |
| 97 | + w.Write([]byte(`{"status": "413 Request Entity Too Large"}`)) |
| 98 | + case "414": |
| 99 | + w.WriteHeader(http.StatusRequestURITooLong) |
| 100 | + w.Write([]byte(`{"status": "414 Request URI Too Long"}`)) |
| 101 | + case "415": |
| 102 | + w.WriteHeader(http.StatusUnsupportedMediaType) |
| 103 | + w.Write([]byte(`{"status": "415 Unsupported Media Type"}`)) |
| 104 | + case "416": |
| 105 | + w.WriteHeader(http.StatusRequestedRangeNotSatisfiable) |
| 106 | + w.Write([]byte(`{"status": "416 Requested Range Not Satisfiable"}`)) |
| 107 | + case "417": |
| 108 | + w.WriteHeader(http.StatusExpectationFailed) |
| 109 | + w.Write([]byte(`{"status": "417 Expectation Failed"}`)) |
| 110 | + case "418": |
| 111 | + w.WriteHeader(http.StatusTeapot) |
| 112 | + w.Write([]byte(`{"status": "418 Teapot"}`)) |
| 113 | + case "421": |
| 114 | + w.WriteHeader(http.StatusMisdirectedRequest) |
| 115 | + w.Write([]byte(`{"status": "421 Misdirected Request"}`)) |
| 116 | + case "422": |
| 117 | + w.WriteHeader(http.StatusUnprocessableEntity) |
| 118 | + w.Write([]byte(`{"status": "422 Unprocessable Entity"}`)) |
| 119 | + case "423": |
| 120 | + w.WriteHeader(http.StatusLocked) |
| 121 | + w.Write([]byte(`{"status": "423 Locked"}`)) |
| 122 | + case "424": |
| 123 | + w.WriteHeader(http.StatusFailedDependency) |
| 124 | + w.Write([]byte(`{"status": "424 Failed Dependency"}`)) |
| 125 | + case "426": |
| 126 | + w.WriteHeader(http.StatusUpgradeRequired) |
| 127 | + w.Write([]byte(`{"status": "426 Upgrade Required"}`)) |
| 128 | + case "428": |
| 129 | + w.WriteHeader(http.StatusPreconditionFailed) |
| 130 | + w.Write([]byte(`{"status": "428 Precondition Failed"}`)) |
| 131 | + case "429": |
| 132 | + w.WriteHeader(http.StatusTooManyRequests) |
| 133 | + w.Write([]byte(`{"status": "429 Too Many Requests"}`)) |
| 134 | + case "431": |
| 135 | + w.WriteHeader(http.StatusRequestHeaderFieldsTooLarge) |
| 136 | + w.Write([]byte(`{"status": "431 Request Header Fields Too Large"}`)) |
| 137 | + case "451": |
| 138 | + w.WriteHeader(http.StatusUnavailableForLegalReasons) |
| 139 | + w.Write([]byte(`{"status": "451 Unavailable For Legal Reasons"}`)) |
| 140 | + case "500": |
| 141 | + w.WriteHeader(http.StatusInternalServerError) |
| 142 | + w.Write([]byte(`{"status": "500 Internal Server Error"}`)) |
| 143 | + case "501": |
| 144 | + w.WriteHeader(http.StatusNotImplemented) |
| 145 | + w.Write([]byte(`{"status": "501 Not Implemented"}`)) |
| 146 | + case "502": |
| 147 | + w.WriteHeader(http.StatusBadGateway) |
| 148 | + w.Write([]byte(`{"status": "502 Bad Gateway"}`)) |
| 149 | + case "503": |
| 150 | + w.WriteHeader(http.StatusServiceUnavailable) |
| 151 | + w.Write([]byte(`{"status": "503 Service Unavailable"}`)) |
| 152 | + case "504": |
| 153 | + w.WriteHeader(http.StatusGatewayTimeout) |
| 154 | + w.Write([]byte(`{"status": "504 Gateway Timeout"}`)) |
| 155 | + case "505": |
| 156 | + w.WriteHeader(http.StatusHTTPVersionNotSupported) |
| 157 | + w.Write([]byte(`{"status": "505 HTTP Version Not Supported"}`)) |
| 158 | + case "506": |
| 159 | + w.WriteHeader(http.StatusVariantAlsoNegotiates) |
| 160 | + w.Write([]byte(`{"status": "506 Variant Also Negotiates"}`)) |
| 161 | + case "507": |
| 162 | + w.WriteHeader(http.StatusInsufficientStorage) |
| 163 | + w.Write([]byte(`{"status": "507 Insufficient Storage"}`)) |
| 164 | + case "508": |
| 165 | + w.WriteHeader(http.StatusLoopDetected) |
| 166 | + w.Write([]byte(`{"status": "508 Loop Detected"}`)) |
| 167 | + case "510": |
| 168 | + w.WriteHeader(http.StatusNotExtended) |
| 169 | + w.Write([]byte(`{"status": "510 Not Extended"}`)) |
| 170 | + case "511": |
| 171 | + w.WriteHeader(http.StatusNetworkAuthenticationRequired) |
| 172 | + w.Write([]byte(`{"status": "511 Network Authentication Required"}`)) |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +func HandleRequests() { |
| 177 | + //Create a mux router object to extract params |
| 178 | + router := mux.NewRouter().StrictSlash(true) |
| 179 | + |
| 180 | + //Set up request handler |
| 181 | + router.HandleFunc("/status/{code}", getStatusCode) |
| 182 | + |
| 183 | + //Listen for request and log error if any |
| 184 | + log.Fatal(http.ListenAndServe(helpers.Port(), router)) |
| 185 | +} |
0 commit comments