diff --git a/README.md b/README.md index ff63cad..9e3d9b6 100644 --- a/README.md +++ b/README.md @@ -112,18 +112,18 @@ Basically the same environment variables for the database, mqqt and timezone nee More detailed documentation of every endpoint will come.. -- GET `/` -- GET `/v1` -- GET `/v1/cars` -- GET `/v1/cars/:CarID` -- GET `/v1/cars/:CarID/charges` -- GET `/v1/cars/:CarID/charges/:ChargeID` -- GET `/v1/cars/:CarID/drives` -- GET `/v1/cars/:CarID/drives/:DriveID` -- GET `/v1/cars/:CarID/status` -- GET `/v1/cars/:CarID/updates` -- GET `/v1/globalsettings` -- GET `/ping` +- GET `/api` +- GET `/api/v1` +- GET `/api/v1/cars` +- GET `/api/v1/cars/:CarID` +- GET `/api/v1/cars/:CarID/charges` +- GET `/api/v1/cars/:CarID/charges/:ChargeID` +- GET `/api/v1/cars/:CarID/drives` +- GET `/api/v1/cars/:CarID/drives/:DriveID` +- GET `/api/v1/cars/:CarID/status` +- GET `/api/v1/cars/:CarID/updates` +- GET `/api/v1/globalsettings` +- GET `/api/ping` ## Security information diff --git a/src/V1_TeslaMateAPICarsChargesDetails.go b/src/V1_TeslaMateAPICarsChargesDetails.go index c7cdbf2..6f2030c 100644 --- a/src/V1_TeslaMateAPICarsChargesDetails.go +++ b/src/V1_TeslaMateAPICarsChargesDetails.go @@ -318,17 +318,17 @@ func TeslaMateAPICarsChargesDetailsV1(c *gin.Context) { // print to log about request if gin.IsDebugging() { - log.Printf("[debug] TeslaMateAPICarsChargesDetailsV1 returned /cars/%d/charges/%d data:", CarID, ChargeID) + log.Printf("[debug] TeslaMateAPICarsChargesDetailsV1 returned /api/v1/cars/%d/charges/%d data:", CarID, ChargeID) js, _ := json.Marshal(jsonData) log.Printf("[debug] %s\n", js) } // return jsonData if ValidResponse { - log.Printf("[info] TeslaMateAPICarsChargesDetailsV1 executed /cars/%d/charges/%d successful.", CarID, ChargeID) + log.Printf("[info] TeslaMateAPICarsChargesDetailsV1 executed /api/v1/cars/%d/charges/%d successful.", CarID, ChargeID) c.JSON(http.StatusOK, jsonData) } else { - log.Printf("[error] TeslaMateAPICarsChargesDetailsV1 error in /cars/%d/charges/%d execution!", CarID, ChargeID) + log.Printf("[error] TeslaMateAPICarsChargesDetailsV1 error in /api/v1/cars/%d/charges/%d execution!", CarID, ChargeID) c.JSON(http.StatusNotFound, gin.H{"error": "something went wrong in TeslaMateAPICarsChargesDetailsV1.."}) } } diff --git a/src/v1_TeslaMateAPICars.go b/src/v1_TeslaMateAPICars.go index 26e360e..c915efa 100644 --- a/src/v1_TeslaMateAPICars.go +++ b/src/v1_TeslaMateAPICars.go @@ -179,9 +179,9 @@ func TeslaMateAPICarsV1(c *gin.Context) { // print to log about request if gin.IsDebugging() && ValidResponse { if CarID > 0 { - log.Printf("[debug] TeslaMateAPICarsV1 returned /cars/%d data:", CarID) + log.Printf("[debug] TeslaMateAPICarsV1 returned /api/v1/cars/%d data:", CarID) } else { - log.Println("[debug] TeslaMateAPICarsV1 returned /cars data:") + log.Println("[debug] TeslaMateAPICarsV1 returned /api/v1/cars data:") } js, _ := json.Marshal(jsonData) log.Printf("[debug] %s\n", js) @@ -190,18 +190,18 @@ func TeslaMateAPICarsV1(c *gin.Context) { // return jsonData if ValidResponse { if CarID > 0 { - log.Printf("[info] TeslaMateAPICarsV1 executed /cars/%d successful.", CarID) + log.Printf("[info] TeslaMateAPICarsV1 executed /api/v1/cars/%d successful.", CarID) } else { - log.Println("[info] TeslaMateAPICarsV1 executed /cars successful.") + log.Println("[info] TeslaMateAPICarsV1 executed /api/v1/cars successful.") } c.JSON(http.StatusOK, jsonData) } else { if CarID > 0 { - log.Printf("[error] TeslaMateAPICarsV1 error in /cars/%d execution!", CarID) + log.Printf("[error] TeslaMateAPICarsV1 error in /api/v1/cars/%d execution!", CarID) } else if len(ParamCarID) > 0 { - log.Printf("[error] TeslaMateAPICarsV1 error in /cars/%s execution!", ParamCarID) + log.Printf("[error] TeslaMateAPICarsV1 error in /api/v1/cars/%s execution!", ParamCarID) } else { - log.Println("[error] TeslaMateAPICarsV1 error in /cars execution!") + log.Println("[error] TeslaMateAPICarsV1 error in /api/v1/cars execution!") } c.JSON(http.StatusNotFound, gin.H{"error": "something went wrong in TeslaMateAPICarsV1.."}) } diff --git a/src/v1_TeslaMateAPICarsCharges.go b/src/v1_TeslaMateAPICarsCharges.go index 9878818..aac48a4 100644 --- a/src/v1_TeslaMateAPICarsCharges.go +++ b/src/v1_TeslaMateAPICarsCharges.go @@ -198,17 +198,17 @@ func TeslaMateAPICarsChargesV1(c *gin.Context) { // print to log about request if gin.IsDebugging() { - log.Printf("[debug] TeslaMateAPICarsChargesV1 returned /cars/%d/charges data:", CarID) + log.Printf("[debug] TeslaMateAPICarsChargesV1 returned /api/v1/cars/%d/charges data:", CarID) js, _ := json.Marshal(jsonData) log.Printf("[debug] %s\n", js) } // return jsonData if ValidResponse { - log.Printf("[info] TeslaMateAPICarsChargesV1 executed /cars/%d/charges successful.", CarID) + log.Printf("[info] TeslaMateAPICarsChargesV1 executed /api/v1/cars/%d/charges successful.", CarID) c.JSON(http.StatusOK, jsonData) } else { - log.Printf("[error] TeslaMateAPICarsChargesV1 error in /cars/%d/charges execution!", CarID) + log.Printf("[error] TeslaMateAPICarsChargesV1 error in /api/v1/cars/%d/charges execution!", CarID) c.JSON(http.StatusNotFound, gin.H{"error": "something went wrong in TeslaMateAPICarsChargesV1.."}) } } diff --git a/src/v1_TeslaMateAPICarsDrives.go b/src/v1_TeslaMateAPICarsDrives.go index b9def50..0c58ea9 100644 --- a/src/v1_TeslaMateAPICarsDrives.go +++ b/src/v1_TeslaMateAPICarsDrives.go @@ -246,17 +246,17 @@ func TeslaMateAPICarsDrivesV1(c *gin.Context) { // print to log about request if gin.IsDebugging() { - log.Printf("[debug] TeslaMateAPICarsDrivesV1 returned /cars/%d/drives data:", CarID) + log.Printf("[debug] TeslaMateAPICarsDrivesV1 returned /api/v1/cars/%d/drives data:", CarID) js, _ := json.Marshal(jsonData) log.Printf("[debug] %s\n", js) } // return jsonData if ValidResponse { - log.Printf("[info] TeslaMateAPICarsDrivesV1 executed /cars/%d/drives successful.", CarID) + log.Printf("[info] TeslaMateAPICarsDrivesV1 executed /api/v1/cars/%d/drives successful.", CarID) c.JSON(http.StatusOK, jsonData) } else { - log.Printf("[error] TeslaMateAPICarsDrivesV1 error in /cars/%d/drives execution!", CarID) + log.Printf("[error] TeslaMateAPICarsDrivesV1 error in /api/v1/cars/%d/drives execution!", CarID) c.JSON(http.StatusNotFound, gin.H{"error": "something went wrong in TeslaMateAPICarsDrivesV1.."}) } } diff --git a/src/v1_TeslaMateAPICarsDrivesDetails.go b/src/v1_TeslaMateAPICarsDrivesDetails.go index 785e198..d69b89c 100644 --- a/src/v1_TeslaMateAPICarsDrivesDetails.go +++ b/src/v1_TeslaMateAPICarsDrivesDetails.go @@ -381,17 +381,17 @@ func TeslaMateAPICarsDrivesDetailsV1(c *gin.Context) { // print to log about request if gin.IsDebugging() { - log.Printf("[debug] TeslaMateAPICarsDrivesDetailsV1 returned /cars/%d/drives/%d data:", CarID, DriveID) + log.Printf("[debug] TeslaMateAPICarsDrivesDetailsV1 returned /api/v1/cars/%d/drives/%d data:", CarID, DriveID) js, _ := json.Marshal(jsonData) log.Printf("[debug] %s\n", js) } // return jsonData if ValidResponse { - log.Printf("[info] TeslaMateAPICarsDrivesDetailsV1 executed /cars/%d/drives/%d successful.", CarID, DriveID) + log.Printf("[info] TeslaMateAPICarsDrivesDetailsV1 executed /api/v1/cars/%d/drives/%d successful.", CarID, DriveID) c.JSON(http.StatusOK, jsonData) } else { - log.Printf("[error] TeslaMateAPICarsDrivesDetailsV1 error in /cars/%d/drives/%d execution!", CarID, DriveID) + log.Printf("[error] TeslaMateAPICarsDrivesDetailsV1 error in /api/v1/cars/%d/drives/%d execution!", CarID, DriveID) c.JSON(http.StatusNotFound, gin.H{"error": "something went wrong in TeslaMateAPICarsDrivesDetailsV1.."}) } } diff --git a/src/v1_TeslaMateAPICarsStatus.go b/src/v1_TeslaMateAPICarsStatus.go index e9c3313..dadc3d2 100644 --- a/src/v1_TeslaMateAPICarsStatus.go +++ b/src/v1_TeslaMateAPICarsStatus.go @@ -548,17 +548,17 @@ func TeslaMateAPICarsStatusV1(c *gin.Context) { // print to log about request if gin.IsDebugging() { - log.Printf("[debug] TeslaMateAPICarsStatusV1 returned /cars/%d/status data:", CarID) + log.Printf("[debug] TeslaMateAPICarsStatusV1 returned /api/v1/cars/%d/status data:", CarID) js, _ := json.Marshal(jsonData) log.Printf("[debug] %s\n", js) } // return jsonData if ValidResponse { - log.Printf("[info] TeslaMateAPICarsStatusV1 executed /cars/%d/status successful.", CarID) + log.Printf("[info] TeslaMateAPICarsStatusV1 executed /api/v1/cars/%d/status successful.", CarID) c.JSON(http.StatusOK, jsonData) } else { - log.Printf("[error] TeslaMateAPICarsStatusV1 error in /cars/%d/status execution!", CarID) + log.Printf("[error] TeslaMateAPICarsStatusV1 error in /api/v1/cars/%d/status execution!", CarID) c.JSON(http.StatusNotFound, gin.H{"error": "something went wrong in TeslaMateAPICarsStatusV1.."}) } } diff --git a/src/v1_TeslaMateAPICarsUpdates.go b/src/v1_TeslaMateAPICarsUpdates.go index 7e6da9e..11858bf 100644 --- a/src/v1_TeslaMateAPICarsUpdates.go +++ b/src/v1_TeslaMateAPICarsUpdates.go @@ -124,17 +124,17 @@ func TeslaMateAPICarsUpdatesV1(c *gin.Context) { // print to log about request if gin.IsDebugging() { - log.Printf("[debug] TeslaMateAPICarsUpdatesV1 returned /cars/%d/updates data:", CarID) + log.Printf("[debug] TeslaMateAPICarsUpdatesV1 returned /api/v1/cars/%d/updates data:", CarID) js, _ := json.Marshal(jsonData) log.Printf("[debug] %s\n", js) } // return jsonData if ValidResponse { - log.Printf("[info] TeslaMateAPICarsUpdatesV1 executed /cars/%d/updates successful.", CarID) + log.Printf("[info] TeslaMateAPICarsUpdatesV1 executed /api/v1/cars/%d/updates successful.", CarID) c.JSON(http.StatusOK, jsonData) } else { - log.Printf("[error] TeslaMateAPICarsUpdatesV1 error in /cars/%d/updates execution!", CarID) + log.Printf("[error] TeslaMateAPICarsUpdatesV1 error in /api/v1/cars/%d/updates execution!", CarID) c.JSON(http.StatusNotFound, gin.H{"error": "something went wrong in TeslaMateAPICarsUpdatesV1.."}) } } diff --git a/src/v1_TeslaMateAPIGlobalsettings.go b/src/v1_TeslaMateAPIGlobalsettings.go index 305f9b7..c6ea907 100644 --- a/src/v1_TeslaMateAPIGlobalsettings.go +++ b/src/v1_TeslaMateAPIGlobalsettings.go @@ -127,17 +127,17 @@ func TeslaMateAPIGlobalsettingsV1(c *gin.Context) { // print to log about request if gin.IsDebugging() { - log.Println("[debug] TeslaMateAPIGlobalsettingsV1 returned /cars data:") + log.Println("[debug] TeslaMateAPIGlobalsettingsV1 returned /api/v1/globalsettings data:") js, _ := json.Marshal(jsonData) log.Printf("[debug] %s\n", js) } // return jsonData if ValidResponse { - log.Println("[info] TeslaMateAPIGlobalsettingsV1 executed /cars successful.") + log.Println("[info] TeslaMateAPIGlobalsettingsV1 executed /api/v1/globalsettings successful.") c.JSON(http.StatusOK, jsonData) } else { - log.Println("[error] TeslaMateAPIGlobalsettingsV1 error in /cars execution!") + log.Println("[error] TeslaMateAPIGlobalsettingsV1 error in /api/v1/globalsettings execution!") c.JSON(http.StatusNotFound, gin.H{"error": "something went wrong in TeslaMateAPIGlobalsettingsV1.."}) } } diff --git a/src/webserver.go b/src/webserver.go index bb513ed..6a8a604 100644 --- a/src/webserver.go +++ b/src/webserver.go @@ -42,36 +42,50 @@ func main() { r := gin.Default() // root endpoint telling API is running - r.GET("/", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"message": "TeslaMateApi container runnnig.."}) }) + r.GET("/", func(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{"message": "TeslaMateApi container runnnig..", "path": "/"}) + }) - // TeslaMateApi v1 endpoints - v1 := r.Group("/v1") + // TeslaMateApi /api endpoints + api := r.Group("/api") { - v1.GET("/", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"message": "TeslaMateApi v1 runnnig.."}) }) - v1.GET("/cars", TeslaMateAPICarsV1) - v1.GET("/cars/:CarID", TeslaMateAPICarsV1) - v1.GET("/cars/:CarID/charges", TeslaMateAPICarsChargesV1) - v1.GET("/cars/:CarID/charges/:ChargeID", TeslaMateAPICarsChargesDetailsV1) - v1.GET("/cars/:CarID/drives", TeslaMateAPICarsDrivesV1) - v1.GET("/cars/:CarID/drives/:DriveID", TeslaMateAPICarsDrivesDetailsV1) - v1.GET("/cars/:CarID/status", TeslaMateAPICarsStatusV1) - v1.GET("/cars/:CarID/updates", TeslaMateAPICarsUpdatesV1) - v1.GET("/globalsettings", TeslaMateAPIGlobalsettingsV1) + // TeslaMateApi /api root + api.GET("/", func(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{"message": "TeslaMateApi container runnnig..", "path": "/api"}) + }) + + // TeslaMateApi /api/v1 endpoints + v1 := api.Group("/v1") + { + // TeslaMateApi /api/v1 root + v1.GET("/", func(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{"message": "TeslaMateApi v1 runnnig..", "path": "/api/v1"}) + }) + v1.GET("/cars", TeslaMateAPICarsV1) + v1.GET("/cars/:CarID", TeslaMateAPICarsV1) + v1.GET("/cars/:CarID/charges", TeslaMateAPICarsChargesV1) + v1.GET("/cars/:CarID/charges/:ChargeID", TeslaMateAPICarsChargesDetailsV1) + v1.GET("/cars/:CarID/drives", TeslaMateAPICarsDrivesV1) + v1.GET("/cars/:CarID/drives/:DriveID", TeslaMateAPICarsDrivesDetailsV1) + v1.GET("/cars/:CarID/status", TeslaMateAPICarsStatusV1) + v1.GET("/cars/:CarID/updates", TeslaMateAPICarsUpdatesV1) + v1.GET("/globalsettings", TeslaMateAPIGlobalsettingsV1) + } + + // /api/ping endpoint + api.GET("/ping", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"message": "pong"}) }) } // TeslaMateApi endpoints (bofore versioning) - r.GET("/cars", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/v1"+c.Request.RequestURI) }) - r.GET("/cars/:CarID", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/v1"+c.Request.RequestURI) }) - r.GET("/cars/:CarID/charges", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/v1"+c.Request.RequestURI) }) - r.GET("/cars/:CarID/charges/:ChargeID", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/v1"+c.Request.RequestURI) }) - r.GET("/cars/:CarID/drives", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/v1"+c.Request.RequestURI) }) - r.GET("/cars/:CarID/drives/:DriveID", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/v1"+c.Request.RequestURI) }) - r.GET("/cars/:CarID/status", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/v1"+c.Request.RequestURI) }) - r.GET("/cars/:CarID/updates", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/v1"+c.Request.RequestURI) }) - r.GET("/globalsettings", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/v1"+c.Request.RequestURI) }) - - // /ping endpoint - r.GET("/ping", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"message": "pong"}) }) + r.GET("/cars", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1"+c.Request.RequestURI) }) + r.GET("/cars/:CarID", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1"+c.Request.RequestURI) }) + r.GET("/cars/:CarID/charges", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1"+c.Request.RequestURI) }) + r.GET("/cars/:CarID/charges/:ChargeID", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1"+c.Request.RequestURI) }) + r.GET("/cars/:CarID/drives", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1"+c.Request.RequestURI) }) + r.GET("/cars/:CarID/drives/:DriveID", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1"+c.Request.RequestURI) }) + r.GET("/cars/:CarID/status", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1"+c.Request.RequestURI) }) + r.GET("/cars/:CarID/updates", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1"+c.Request.RequestURI) }) + r.GET("/globalsettings", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/api/v1"+c.Request.RequestURI) }) // run this and serve on 0.0.0.0:8080 r.Run(":8080")