-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
09cee66
commit d582370
Showing
3 changed files
with
24 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
# httprespond | ||
Go webhook that throws http errors to test http exception handling against | ||
Go server that returns http errors to test http exception handling against. | ||
|
||
Request /400 for a 400 response, /300 for a 300 response etc. |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,32 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/nathanhettige/httprespond/internal/responses" | ||
) | ||
|
||
func main() { | ||
router := gin.Default() | ||
router.Any("/:input", httpResponse) | ||
router.Run(":3000") | ||
} | ||
|
||
router.Any("/200", responses.TwoHundred) | ||
router.Any("/300", responses.ThreeHundred) | ||
router.Any("/400", responses.FourHundred) | ||
func httpResponse(c *gin.Context) { | ||
input := c.Param("input") | ||
|
||
router.Run(":3000") | ||
status, err := strconv.Atoi(input) | ||
if err != nil { | ||
c.String(http.StatusNotAcceptable, fmt.Sprintf("Invalid http response status code - %s.", input)) | ||
return | ||
} | ||
|
||
if http.StatusText(status) == "" { | ||
c.String(http.StatusNotAcceptable, fmt.Sprintf("HTTP Status code %d doesn't exist.", status)) | ||
return | ||
} | ||
|
||
c.String(status, fmt.Sprintf("%d %s", status, http.StatusText(status))) | ||
} |