2021-07-11 21:19:37 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"gitlab.com/gaming0skar123/go/pingbot/database"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Insert(c *gin.Context) {
|
2021-07-19 14:24:33 +00:00
|
|
|
var post database.URL
|
2021-07-14 10:00:17 +00:00
|
|
|
err := c.BindJSON(&post)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
|
|
"success": false,
|
|
|
|
"message": "Error Binding JSON!",
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2021-07-11 21:19:37 +00:00
|
|
|
|
|
|
|
if len(post.URL) < 1 {
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
|
|
"success": false,
|
|
|
|
"message": "URL was not Provided!",
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-14 10:00:17 +00:00
|
|
|
_, err = http.Get(post.URL)
|
2021-07-11 21:19:37 +00:00
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
|
|
"success": false,
|
|
|
|
"message": "Error Pinging URL!",
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = database.Insert(&database.URL{
|
|
|
|
URL: post.URL,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
|
|
"success": false,
|
|
|
|
"message": "Error Inserting to Database!",
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": true,
|
|
|
|
"url": post.URL,
|
|
|
|
})
|
|
|
|
}
|