This commit is contained in:
Jan Barfuss 2024-03-19 23:56:55 +01:00
parent 2eaeec2a26
commit 3c6ab561fd
2 changed files with 58 additions and 0 deletions

16
api/fetch.go Normal file
View File

@ -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)
}

42
api/server.go Normal file
View File

@ -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",
})
}