Mensa-API/api/server.go

43 lines
706 B
Go
Raw Normal View History

2024-03-19 22:56:55 +00:00
package main
import (
"net/http"
2024-03-19 23:04:26 +00:00
"github.com/LeRoid-hub/Mensa-API/blob/go/api/fetch.go"
2024-03-19 22:56:55 +00:00
"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",
})
}