Skip to content

Commit

Permalink
feat: refactor to use echo logger (#10)
Browse files Browse the repository at this point in the history
* feat: refactor to use echo logger

* fix: lint & fmt

* fix: missing lint
  • Loading branch information
wolmi authored Oct 5, 2023
1 parent 6fdc5f0 commit 538d8d1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
11 changes: 6 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ and manages to publish them in a GCP PubSub topic.`,

proxy.Init(ctx)



},
}

Expand All @@ -50,14 +48,17 @@ func init() {

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.livekit-webhook-proxy.yaml)")

rootCmd.PersistentFlags().Bool("debug", false, "Debug mode")
viper.BindPFlag("debug", rootCmd.PersistentFlags().Lookup("debug")) // nolint

rootCmd.Flags().IntP("port", "p", 8080, "Port to start the proxy")
viper.BindPFlag("port", rootCmd.Flags().Lookup("port"))
viper.BindPFlag("port", rootCmd.Flags().Lookup("port")) // nolint

rootCmd.Flags().StringP("topic", "t", "", "Topic in the GCP PubSub to publish events")
viper.BindPFlag("topic", rootCmd.Flags().Lookup("topic"))
viper.BindPFlag("topic", rootCmd.Flags().Lookup("topic")) // nolint

rootCmd.Flags().StringP("project-id", "P", "", "GCP ProjectId")
viper.BindPFlag("project-id", rootCmd.Flags().Lookup("project-id"))
viper.BindPFlag("project-id", rootCmd.Flags().Lookup("project-id")) // nolint
}

// initConfig reads in config file and ENV variables if set.
Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module livekit-webhook-proxy

go 1.20

require cloud.google.com/go/compute/metadata v0.2.3
require (
cloud.google.com/go/compute/metadata v0.2.3
github.com/labstack/gommon v0.4.0
)

require (
cloud.google.com/go v0.110.2 // indirect
Expand All @@ -13,7 +16,6 @@ require (
github.com/google/s2a-go v0.1.4 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
github.com/googleapis/gax-go/v2 v2.11.0 // indirect
github.com/labstack/gommon v0.4.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
Expand Down
1 change: 0 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/*
Copyright © 2023 Miquel Ramon Ortega Tido
*/
package main

Expand Down
20 changes: 15 additions & 5 deletions types/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"encoding/json"
"fmt"
"livekit-webhook-proxy/utils"
"log"
"net/http"

"github.com/labstack/gommon/log"

"cloud.google.com/go/pubsub"
"github.com/labstack/echo/v4"
"github.com/spf13/viper"
Expand All @@ -22,6 +23,14 @@ func (p *Proxy) Init(ctx context.Context) {
p.echo = echo.New()
p.pubsub.Init(ctx)

p.echo.HideBanner = true

// get log level from flag
logLevel := viper.GetBool("debug")
if logLevel {
p.echo.Logger.SetLevel(log.DEBUG)
}

p.echo.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "ok")
})
Expand All @@ -33,20 +42,21 @@ func (p *Proxy) Init(ctx context.Context) {
func (p *Proxy) publish(c echo.Context) error {
var payload map[string]interface{}
if err := json.NewDecoder(c.Request().Body).Decode(&payload); err != nil {
p.echo.Logger.Errorf("could not decode body: %v", err)
return echo.NewHTTPError(http.StatusBadRequest, "could not bind body").SetInternal(err)
}
log.Printf("[INFO] livekit event %s in room %s", payload["event"], payload["room"].(map[string]interface{})["name"])
p.echo.Logger.Infof("livekit event %s in room %s", payload["event"], payload["room"].(map[string]interface{})["name"])
jsonPayload, _ := json.Marshal(payload)
log.Printf("[DEBUG] event payload data: %s", jsonPayload)
p.echo.Logger.Debugf("event payload data: %s", jsonPayload)

topic := p.pubsub.Client.Topic(viper.GetString("topic"))
res := topic.Publish(c.Request().Context(), &pubsub.Message{
Data: jsonPayload,
})
msgID, err := res.Get(c.Request().Context())
if err != nil {
log.Fatal(err)
p.echo.Logger.Fatal(err)
}
log.Printf("[DEBUG] event published with msgID %v", msgID)
p.echo.Logger.Debugf("event published with msgID %v", msgID)
return c.JSON(http.StatusOK, payload)
}

0 comments on commit 538d8d1

Please sign in to comment.