-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ales Verbic <[email protected]>
- Loading branch information
Showing
6 changed files
with
347 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"time" | ||
|
||
v1 "github.com/blinklabs-io/snek/api/v1" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func ConfigureRouter(debug bool) *gin.Engine { | ||
if !debug { | ||
gin.SetMode(gin.ReleaseMode) | ||
} | ||
gin.DisableConsoleColor() | ||
g := gin.New() | ||
g.Use(gin.Recovery()) | ||
// Custom access logging | ||
g.Use(gin.LoggerWithFormatter(accessLogger)) | ||
// Healthcheck endpoint | ||
g.GET("/healthcheck", handleHealthcheck) | ||
// No-op API endpoint for testing | ||
g.GET("/ping", func(c *gin.Context) { | ||
c.String(200, "pong") | ||
}) | ||
v1.ConfigureRoutes(g) | ||
return g | ||
} | ||
|
||
func accessLogger(param gin.LogFormatterParams) string { | ||
logEntry := gin.H{ | ||
"type": "access", | ||
"client_ip": param.ClientIP, | ||
"timestamp": param.TimeStamp.Format(time.RFC1123), | ||
"method": param.Method, | ||
"path": param.Path, | ||
"proto": param.Request.Proto, | ||
"status_code": param.StatusCode, | ||
"latency": param.Latency, | ||
"user_agent": param.Request.UserAgent(), | ||
"error_message": param.ErrorMessage, | ||
} | ||
ret, _ := json.Marshal(logEntry) | ||
return string(ret) + "\n" | ||
} | ||
|
||
func handleHealthcheck(c *gin.Context) { | ||
// TODO: add some actual health checking here | ||
c.JSON(200, gin.H{"failed": false}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package v1 | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// TODO implement FCM storage | ||
var fcmTokens = make(map[string]string) | ||
|
||
var tokenRequest struct { | ||
FCMToken string `json:"fcmToken" binding:"required"` | ||
} | ||
|
||
// TODO: update this with actual storage and implementation | ||
func storeFCMToken(c *gin.Context) { | ||
// Handle the creation of an FCM token | ||
if err := c.ShouldBindJSON(&tokenRequest); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
// Use the FCM token as the map key | ||
fcmTokens[tokenRequest.FCMToken] = tokenRequest.FCMToken | ||
c.Status(http.StatusCreated) | ||
} | ||
|
||
// TODO: update this with actual storage and implementation | ||
func readFCMToken(c *gin.Context) { | ||
token := c.Param("token") | ||
storedToken, exists := fcmTokens[token] | ||
if !exists { | ||
c.Status(http.StatusNotFound) | ||
return | ||
} | ||
c.JSON(http.StatusOK, gin.H{"fcmToken": storedToken}) | ||
} | ||
|
||
// TODO: update this with actual storage and implementation | ||
func deleteFCMToken(c *gin.Context) { | ||
token := c.Param("token") | ||
// Check if the token exists | ||
_, exists := fcmTokens[token] | ||
if !exists { | ||
c.Status(http.StatusNotFound) | ||
return | ||
} | ||
// Delete the FCM token | ||
delete(fcmTokens, token) | ||
c.Status(http.StatusNoContent) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package v1 | ||
|
||
import "github.com/gin-gonic/gin" | ||
|
||
func ConfigureRoutes(g *gin.Engine) { | ||
apiGroup := g.Group("/v1") | ||
|
||
apiGroup.POST("/fcm", storeFCMToken) | ||
apiGroup.POST("/fcm/", storeFCMToken) | ||
|
||
apiGroup.GET("/fcm/:token", readFCMToken) | ||
apiGroup.GET("/fcm/:token/", readFCMToken) | ||
|
||
apiGroup.DELETE("/fcm/:token", deleteFCMToken) | ||
apiGroup.DELETE("/fcm/:token/", deleteFCMToken) | ||
|
||
// Swagger UI | ||
// TODO: add swagger UI | ||
// g.Static("/v1/swagger-ui", "swagger-ui") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.