Skip to content

Commit

Permalink
feat: add API endpoints
Browse files Browse the repository at this point in the history
Signed-off-by: Ales Verbic <[email protected]>
  • Loading branch information
verbotenj committed Oct 3, 2023
1 parent 6fa2b67 commit 1341c02
Show file tree
Hide file tree
Showing 6 changed files with 347 additions and 2 deletions.
50 changes: 50 additions & 0 deletions api/api.go
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})
}
52 changes: 52 additions & 0 deletions api/v1/fcm.go
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)
}
20 changes: 20 additions & 0 deletions api/v1/v1.go
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")
}
10 changes: 10 additions & 0 deletions cmd/snek/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"net/http"
"os"

"github.com/blinklabs-io/snek/api"
_ "github.com/blinklabs-io/snek/filter"
_ "github.com/blinklabs-io/snek/input"
"github.com/blinklabs-io/snek/internal/config"
Expand Down Expand Up @@ -126,6 +127,15 @@ func main() {
}
pipe.AddOutput(output)

// Configure router and start listening
// TODO: add debug config
debug := true
router := api.ConfigureRouter(debug)
// TODO: add endpoint server config
if err := router.Run(fmt.Sprintf("%s:%d", "127.0.0.1", 8090)); err != nil {
logger.Fatalf("failed to start server: %s\n", err)
}

// Start pipeline and wait for error
if err := pipe.Start(); err != nil {
logger.Fatalf("failed to start pipeline: %s\n", err)
Expand Down
47 changes: 45 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,57 @@ require (
// replace github.com/blinklabs-io/gouroboros v0.52.0 => ../gouroboros

require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/PuerkitoBio/purell v1.2.0 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/bytedance/sonic v1.10.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/fxamacker/cbor/v2 v2.4.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.9.1 // indirect
github.com/go-openapi/jsonpointer v0.20.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/spec v0.20.9 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.15.5 // indirect
github.com/go-toast/toast v0.0.0-20190211030409-01e6764cf0a4 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/jinzhu/copier v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/swaggo/files v1.0.1 // indirect
github.com/swaggo/gin-swagger v1.6.0 // indirect
github.com/swaggo/swag v1.16.2 // indirect
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/urfave/cli/v2 v2.25.7 // indirect
github.com/x448/float16 v0.8.4 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/arch v0.5.0 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/tools v0.13.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
Loading

0 comments on commit 1341c02

Please sign in to comment.