mirror of
https://github.com/MedzikUser/go-pingbot
synced 2024-08-14 23:57:07 +00:00
deleted(api): now is vercel serverless
This commit is contained in:
parent
6eea751bdf
commit
f09209d1bf
6 changed files with 0 additions and 133 deletions
|
@ -2,5 +2,3 @@
|
|||
MONGODB_URI=mongodb+srv://...
|
||||
MONGODB_DB=...
|
||||
MONGODB_COLLECTION=...
|
||||
|
||||
PASSWORD=PrOtEdTeD_pAsSwOrD
|
||||
|
|
|
@ -11,6 +11,4 @@ var (
|
|||
Mongo_URI = os.Getenv("MONGODB_URI")
|
||||
Mongo_DB = os.Getenv("MONGODB_DB")
|
||||
Mongo_Collection = os.Getenv("MONGODB_COLLECTION")
|
||||
|
||||
Password = os.Getenv("PASSWORD")
|
||||
)
|
||||
|
|
|
@ -55,7 +55,6 @@ func init() {
|
|||
}
|
||||
|
||||
func DownloadFile(filepath string, url string) error {
|
||||
// Get the data
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -8,8 +8,6 @@ func ApplyRoutes(r *gin.Engine) {
|
|||
api := r.Group("/api")
|
||||
{
|
||||
api.GET("/url", GetAll)
|
||||
api.POST("/url", Insert)
|
||||
api.DELETE("/url/:url", Delete)
|
||||
|
||||
api.GET("/status", Status)
|
||||
}
|
||||
|
|
|
@ -1,64 +0,0 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gitlab.com/gaming0skar123/go/pingbot/config"
|
||||
"gitlab.com/gaming0skar123/go/pingbot/database/mongo"
|
||||
)
|
||||
|
||||
func Delete(c *gin.Context) {
|
||||
const BEARER_SCHEMA = "Password"
|
||||
authHeader := c.GetHeader("Authorization")
|
||||
passwordString := authHeader[len(BEARER_SCHEMA)+1:]
|
||||
|
||||
if passwordString != config.Password {
|
||||
c.JSON(http.StatusUnauthorized, json{
|
||||
"success": false,
|
||||
"message": "Unauth!",
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
url := c.Param("url")
|
||||
|
||||
d, err := base64.StdEncoding.DecodeString(url)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, json{
|
||||
"success": false,
|
||||
"message": "Error Parsing Base64!",
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
url = string(d)
|
||||
|
||||
r, err := mongo.Delete(url)
|
||||
|
||||
if r.DeletedCount <= 0 {
|
||||
c.JSON(http.StatusNotFound, json{
|
||||
"success": false,
|
||||
"message": "Not Found!",
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, json{
|
||||
"success": false,
|
||||
"message": "Error Deleting from Database!",
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, json{
|
||||
"success": true,
|
||||
"message": "Deleted!",
|
||||
})
|
||||
}
|
|
@ -1,62 +0,0 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gitlab.com/gaming0skar123/go/pingbot/database/mongo"
|
||||
)
|
||||
|
||||
func Insert(c *gin.Context) {
|
||||
var post mongo.URL
|
||||
err := c.BindJSON(&post)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, json{
|
||||
"success": false,
|
||||
"message": "Error Binding JSON!",
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if len(post.URL) < 1 {
|
||||
c.JSON(http.StatusBadRequest, json{
|
||||
"success": false,
|
||||
"message": "URL was not Provided!",
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
_, err = http.Get(post.URL)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, json{
|
||||
"success": false,
|
||||
"message": "Error Pinging URL!",
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if post.Cluster == 0 {
|
||||
post.Cluster = 1
|
||||
}
|
||||
|
||||
_, err = mongo.Insert(&mongo.URL{
|
||||
URL: post.URL,
|
||||
Cluster: post.Cluster,
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, json{
|
||||
"success": false,
|
||||
"message": "Error Inserting to Database!",
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, json{
|
||||
"success": true,
|
||||
"url": post.URL,
|
||||
})
|
||||
}
|
Loading…
Reference in a new issue