mirror of
https://github.com/IllusionMan1212/kotapi-go.git
synced 2026-10-24 10:12:29 +00:00
fix RNG and routes. and added a some logging when POSTing to the api
This commit is contained in:
+8
-2
@@ -7,6 +7,7 @@ import (
|
||||
"illusionman1212/kotapi-go/models"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -34,10 +35,12 @@ func RandomHandler(w http.ResponseWriter, req *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// ids start from 1
|
||||
min := 1
|
||||
// seed the default source using the current time in unix nanosecond format
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
// generate random number between 0 and `max`
|
||||
num := rand.Intn(int(max + 1))
|
||||
// generate random number between min and `max`
|
||||
num := rand.Intn(int(max)-min) + min
|
||||
|
||||
// find the random kot document and store it in the `kot` variable
|
||||
err = db.Kots.FindOne(db.Ctx, bson.M{"id": num}).Decode(&kot)
|
||||
@@ -110,6 +113,7 @@ func AddKotHandler(w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
file, handler, err := req.FormFile("image")
|
||||
if err != nil {
|
||||
log.Print("no image was uploaded")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte(`{ "error": "Invalid or incomplete request","status": 400, "failed": true }`))
|
||||
return
|
||||
@@ -117,11 +121,13 @@ func AddKotHandler(w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
password := req.Form.Get("password")
|
||||
if password == "" {
|
||||
log.Print("no password was sent")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte(`{ "error": "Invalid or incomplete request","status": 400, "failed": true }`))
|
||||
return
|
||||
}
|
||||
if password != os.Getenv("PASSWORD") {
|
||||
log.Print("password is incorrect")
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
w.Write([]byte(`{ "error": "Not authorized","status": 401, "failed": true }`))
|
||||
return
|
||||
|
||||
@@ -18,13 +18,13 @@ func main() {
|
||||
}
|
||||
db.InitializeDB()
|
||||
|
||||
router := mux.NewRouter()
|
||||
router.HandleFunc("/", routes.RandomHandler).Methods("GET")
|
||||
router.HandleFunc("/addkot", routes.AddKotHandler).Methods("POST")
|
||||
router.HandleFunc("/{id}", routes.IdHandler).Methods("GET")
|
||||
router := mux.NewRouter().StrictSlash(true)
|
||||
router.HandleFunc("/kotapi", routes.RandomHandler).Methods("GET")
|
||||
router.HandleFunc("/kotapi/addkot", routes.AddKotHandler).Methods("POST")
|
||||
router.HandleFunc("/kotapi/{id}", routes.IdHandler).Methods("GET")
|
||||
|
||||
router.HandleFunc("/kots/compressed/{filename}", routes.KotsCompressedHandler).Methods("GET")
|
||||
router.HandleFunc("/kots/{filename}", routes.KotsHandler).Methods("GET")
|
||||
router.HandleFunc("/kotapi/kots/compressed/{filename}", routes.KotsCompressedHandler).Methods("GET")
|
||||
router.HandleFunc("/kotapi/kots/{filename}", routes.KotsHandler).Methods("GET")
|
||||
|
||||
fmt.Println("Listening on 8080")
|
||||
http.ListenAndServe(":8080", router)
|
||||
|
||||
Reference in New Issue
Block a user