diff --git a/api/fetch.go b/api/fetch.go new file mode 100644 index 0000000..0e6f6c0 --- /dev/null +++ b/api/fetch.go @@ -0,0 +1,16 @@ +package fetch + +import ( + "net/http" + "net/url" +) + +func Fetch(url string) (*http.Response, error) { + u, err := url.ParseRequestURI(url) + if err != nil { + return nil, err + } + + return http.Get(url) +} + diff --git a/api/server.go b/api/server.go new file mode 100644 index 0000000..a4a3d73 --- /dev/null +++ b/api/server.go @@ -0,0 +1,42 @@ +package main + +import ( + "net/http" + + "fetch" + "github.com/gin-gonic/gin" +) + +func main() { + r := gin.Default() + r.GET("/ping", ping) + r.GET("/fetch", fetchRoute) + r.Run("localhost:8080") +} + +func fetchRoute(c *gin.Context) { + url := c.Query("url") + if url == "" { + c.JSON(http.StatusBadRequest, gin.H{ + "error": "url is required", + }) + return + } + resp, err := fetch.Fetch(url) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "error": err.Error(), + }) + return + } + defer resp.Body.Close() + c.JSON(http.StatusOK, gin.H{ + "status": resp.Status, + }) +} + +func ping(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "pong", + }) +}