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