Init
This commit is contained in:
42
api/authed.go
Normal file
42
api/authed.go
Normal file
@ -0,0 +1,42 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/clerkinc/clerk-sdk-go/clerk"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func Authed(c echo.Context) error {
|
||||
apiKey := os.Getenv("CLERK_SECRET_KEY")
|
||||
|
||||
client, err := clerk.NewClient(apiKey)
|
||||
if err != nil {
|
||||
// handle error
|
||||
println(err.Error())
|
||||
}
|
||||
|
||||
// get session token from Authorization header
|
||||
sessionToken := c.Request().Header.Get("Authorization")
|
||||
sessionToken = strings.TrimPrefix(sessionToken, "Bearer ")
|
||||
|
||||
println(sessionToken)
|
||||
|
||||
// verify the session
|
||||
sessClaims, err := client.VerifyToken(sessionToken)
|
||||
if err != nil {
|
||||
println(err.Error())
|
||||
return c.String(http.StatusUnauthorized, "Unauthorized!")
|
||||
}
|
||||
|
||||
// get the user, and say welcome!
|
||||
user, err := client.Users().Read(sessClaims.Claims.Subject)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return c.String(http.StatusOK, "Welcome "+*user.FirstName)
|
||||
|
||||
}
|
27
api/nowplaying.go
Normal file
27
api/nowplaying.go
Normal file
@ -0,0 +1,27 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"atri.dad/lib"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func NowPlayingHandler(c echo.Context) error {
|
||||
clientID := os.Getenv("SPOTIFY_CLIENT_ID")
|
||||
clientSecret := os.Getenv("SPOTIFY_CLIENT_SECRET")
|
||||
refreshToken := os.Getenv("SPOTIFY_REFRESH_TOKEN")
|
||||
|
||||
playing, err := lib.GetCurrentlyPlayingTrack(clientID, clientSecret, refreshToken)
|
||||
if err != nil {
|
||||
http.Error(c.Response().Writer, err.Error(), http.StatusInternalServerError)
|
||||
return err
|
||||
}
|
||||
|
||||
if playing.Item != nil && playing.Playing {
|
||||
return c.String(http.StatusOK, `<div class="indicator-item badge badge-success"><a _="on mouseover put '🎧 Listening to `+playing.Item.Name+" by "+playing.Item.Artists[0].Name+` 🎧' into my.textContent on mouseout put '🎧' into my.textContent" href="`+playing.Item.ExternalURLs["spotify"]+`" rel="noreferrer" target="_blank">🎧</a></div>`)
|
||||
} else {
|
||||
return c.String(http.StatusOK, "")
|
||||
}
|
||||
}
|
26
api/pay.go
Normal file
26
api/pay.go
Normal file
@ -0,0 +1,26 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"atri.dad/lib"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
type PayRequest struct {
|
||||
SuccessUrl string `json:"successUrl"`
|
||||
CancelUrl string `json:"cancelUrl"`
|
||||
PriceId string `json:"priceId"`
|
||||
}
|
||||
|
||||
func Pay(c echo.Context) error {
|
||||
payReq := new(PayRequest)
|
||||
|
||||
if err := c.Bind(payReq); err != nil {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid request payload"})
|
||||
}
|
||||
|
||||
lib.CreateCheckoutSession(c.Response().Writer, c.Request(), payReq.SuccessUrl, payReq.CancelUrl, payReq.PriceId)
|
||||
|
||||
return c.String(http.StatusOK, "Checkout session created")
|
||||
}
|
11
api/ping.go
Normal file
11
api/ping.go
Normal file
@ -0,0 +1,11 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func Ping(c echo.Context) error {
|
||||
return c.String(http.StatusOK, "Pong!")
|
||||
}
|
12
api/post.copy.go
Normal file
12
api/post.copy.go
Normal file
@ -0,0 +1,12 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func PostCopy(c echo.Context) error {
|
||||
return c.String(http.StatusOK, `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-copy"><rect width="14" height="14" x="8" y="8" rx="2" ry="2"/><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/></svg>`)
|
||||
|
||||
}
|
51
api/sse.go
Normal file
51
api/sse.go
Normal file
@ -0,0 +1,51 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"atri.dad/lib"
|
||||
"atri.dad/lib/pubsub"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func SSE(c echo.Context, pubSub pubsub.PubSub) error {
|
||||
if pubSub == nil {
|
||||
return errors.New("pubSub is nil")
|
||||
}
|
||||
|
||||
channel := c.QueryParam("channel")
|
||||
if channel == "" {
|
||||
channel = "default"
|
||||
}
|
||||
|
||||
// Use the request context, which is cancelled when the client disconnects
|
||||
ctx := c.Request().Context()
|
||||
|
||||
pubsub, err := pubSub.SubscribeToChannel(channel)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to subscribe to channel: %w", err)
|
||||
}
|
||||
|
||||
lib.SetSSEHeaders(c)
|
||||
|
||||
// Create a ticker that fires every 15 seconds
|
||||
ticker := lib.CreateTickerAndKeepAlive(c, 30*time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
// Create a client channel and add it to the SSE server
|
||||
client := make(chan string)
|
||||
lib.SSEServer.AddClient(channel, client)
|
||||
defer lib.SSEServer.RemoveClient(channel, client)
|
||||
|
||||
go lib.HandleIncomingMessages(c, pubsub, client)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
// If the client has disconnected, stop the loop
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
37
api/ssedemosend.go
Normal file
37
api/ssedemosend.go
Normal file
@ -0,0 +1,37 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"atri.dad/lib"
|
||||
"atri.dad/lib/pubsub"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func SSEDemoSend(c echo.Context, pubSub pubsub.PubSub) error {
|
||||
channel := c.QueryParam("channel")
|
||||
if channel == "" {
|
||||
channel = "default"
|
||||
}
|
||||
|
||||
// Get message from query parameters, form value, or request body
|
||||
message := c.QueryParam("message")
|
||||
if message == "" {
|
||||
message = c.FormValue("message")
|
||||
if message == "" {
|
||||
var body map[string]string
|
||||
if err := c.Bind(&body); err != nil {
|
||||
return err
|
||||
}
|
||||
message = body["message"]
|
||||
}
|
||||
}
|
||||
|
||||
if message == "" {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": "message parameter is required"})
|
||||
}
|
||||
|
||||
lib.SendSSE(c.Request().Context(), pubSub, "default", message)
|
||||
|
||||
return c.JSON(http.StatusOK, map[string]string{"status": "message sent"})
|
||||
}
|
Reference in New Issue
Block a user