This commit is contained in:
Atridad Lahiji
2023-05-18 20:04:55 -06:00
committed by Atridad Lahiji
parent 84591f3a2d
commit 3d719132f1
96 changed files with 3422 additions and 4793 deletions

31
lib/email.go Normal file
View File

@ -0,0 +1,31 @@
package lib
import (
"fmt"
"os"
"github.com/resendlabs/resend-go"
)
var client *resend.Client
// init function
func init() {
client = resend.NewClient(os.Getenv("RESEND_API_KEY"))
}
func SendEmail(to_email string, from_email string, from_name string, html string, subject string) {
params := &resend.SendEmailRequest{
From: from_name + "<" + from_email + ">",
To: []string{to_email},
Html: html,
Subject: subject,
}
sent, err := client.Emails.Send(params)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(sent.Id)
}

20
lib/links.go Normal file
View File

@ -0,0 +1,20 @@
package lib
import (
"html/template"
)
type IconLink struct {
Name string
Href string
Icon template.HTML
}
type CardLink struct {
Name string
Href string
Description string
Date string
Tags []string
Internal bool
}

27
lib/logging.go Normal file
View File

@ -0,0 +1,27 @@
package lib
import "github.com/fatih/color"
// Error logging
var red = color.New(color.FgRed)
var LogError = red.Add(color.Bold)
// Info logging
var cyan = color.New(color.FgCyan)
var LogInfo = cyan.Add(color.Bold)
// Success logging
var green = color.New(color.FgGreen)
var LogSuccess = green.Add(color.Bold)
// Warning logging
var yellow = color.New(color.FgYellow)
var LogWarning = yellow.Add(color.Bold)
// Debug logging
var magenta = color.New(color.FgMagenta)
var LogDebug = magenta.Add(color.Bold)
// Custom logging
var white = color.New(color.FgWhite)
var LogCustom = white.Add(color.Bold)

65
lib/markdown.go Normal file
View File

@ -0,0 +1,65 @@
package lib
import (
"bufio"
"bytes"
"errors"
"fmt"
"io/fs"
"strings"
"github.com/yuin/goldmark"
"gopkg.in/yaml.v2"
)
type FrontMatter struct {
Name string
Date string
Tags []string
}
func ExtractFrontMatter(file fs.DirEntry, contentFS fs.FS) (CardLink, error) {
f, err := contentFS.Open(file.Name())
if err != nil {
return CardLink{}, fmt.Errorf("failed to open file: %w", err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
var lines []string
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
return CardLink{}, fmt.Errorf("failed to read file: %w", err)
}
content := strings.Join(lines, "\n")
splitContent := strings.SplitN(content, "---", 3)
if len(splitContent) < 3 {
return CardLink{}, fmt.Errorf("invalid file format: %s", file.Name())
}
frontMatter := CardLink{}
if err := yaml.Unmarshal([]byte(splitContent[1]), &frontMatter); err != nil {
return CardLink{}, fmt.Errorf("failed to unmarshal frontmatter: %w", err)
}
md := goldmark.New(goldmark.WithExtensions())
var buf bytes.Buffer
if err := md.Convert([]byte(splitContent[2]), &buf); err != nil {
return CardLink{}, fmt.Errorf("failed to convert markdown: %w", err)
}
return frontMatter, nil
}
func SplitFrontmatter(md []byte) (frontmatter []byte, content []byte, err error) {
parts := bytes.SplitN(md, []byte("---"), 3)
if len(parts) < 3 {
return nil, nil, errors.New("invalid or missing frontmatter")
}
return parts[1], parts[2], nil
}

View File

@ -0,0 +1,90 @@
package adapters
import (
"context"
"sync"
"time"
"atri.dad/lib"
"atri.dad/lib/pubsub"
)
type LocalPubSub struct {
subscribers map[string][]chan pubsub.Message
lock sync.RWMutex
}
type LocalPubSubMessage struct {
messages <-chan pubsub.Message
}
func (ps *LocalPubSub) SubscribeToChannel(channel string) (pubsub.PubSubMessage, error) {
ps.lock.Lock()
defer ps.lock.Unlock()
if ps.subscribers == nil {
ps.subscribers = make(map[string][]chan pubsub.Message)
}
ch := make(chan pubsub.Message, 100)
ps.subscribers[channel] = append(ps.subscribers[channel], ch)
lib.LogInfo.Printf("[PUBSUB/LOCAL] Subscribed to channel %s\n", channel)
return &LocalPubSubMessage{messages: ch}, nil
}
func (ps *LocalPubSub) PublishToChannel(channel string, message string) error {
subscribers, ok := ps.subscribers[channel]
if !ok {
lib.LogWarning.Printf("\n[PUBSUB/LOCAL] No subscribers for channel %s\n", channel)
return nil
}
ps.lock.Lock()
defer ps.lock.Unlock()
lib.LogInfo.Printf("\n[PUBSUB/LOCAL] Publishing message to channel %s: %s\n", channel, message)
for _, ch := range subscribers {
ch <- pubsub.Message{Payload: message}
}
return nil
}
func (ps *LocalPubSub) UnsubscribeFromChannel(channel string, ch <-chan pubsub.Message) {
ps.lock.Lock()
defer ps.lock.Unlock()
subscribers := ps.subscribers[channel]
for i, subscriber := range subscribers {
if subscriber == ch {
// Remove the subscriber from the slice
subscribers = append(subscribers[:i], subscribers[i+1:]...)
break
}
}
if len(subscribers) == 0 {
delete(ps.subscribers, channel)
} else {
ps.subscribers[channel] = subscribers
}
}
func (m *LocalPubSubMessage) ReceiveMessage(ctx context.Context) (*pubsub.Message, error) {
for {
select {
case <-ctx.Done():
// The client has disconnected. Stop trying to send messages.
return nil, ctx.Err()
case msg := <-m.messages:
// A message has been received. Send it to the client.
lib.LogInfo.Printf("\n[PUBSUB/LOCAL] Received message: %s\n", msg.Payload)
return &msg, nil
case <-time.After(30 * time.Second):
// No message has been received for 30 seconds. Send a keep-alive message.
return &pubsub.Message{Payload: "keep-alive"}, nil
}
}
}

View File

@ -0,0 +1,70 @@
package adapters
import (
"context"
"os"
"atri.dad/lib"
"atri.dad/lib/pubsub"
"github.com/joho/godotenv"
"github.com/redis/go-redis/v9"
)
var RedisClient *redis.Client
type RedisPubSubMessage struct {
pubsub *redis.PubSub
}
// RedisPubSub is a Redis implementation of the PubSub interface.
type RedisPubSub struct {
Client *redis.Client
}
func NewRedisClient() *redis.Client {
if RedisClient != nil {
return RedisClient
}
godotenv.Load(".env")
redis_host := os.Getenv("REDIS_HOST")
redis_password := os.Getenv("REDIS_PASSWORD")
lib.LogInfo.Printf("\n[PUBSUB/REDIS]Connecting to Redis at %s\n", redis_host)
RedisClient = redis.NewClient(&redis.Options{
Addr: redis_host,
Password: redis_password,
DB: 0,
})
return RedisClient
}
func (m *RedisPubSubMessage) ReceiveMessage(ctx context.Context) (*pubsub.Message, error) {
msg, err := m.pubsub.ReceiveMessage(ctx)
if err != nil {
return nil, err
}
lib.LogInfo.Printf("\n[PUBSUB/REDIS] Received message: %s\n", msg.Payload)
return &pubsub.Message{Payload: msg.Payload}, nil
}
func (ps *RedisPubSub) SubscribeToChannel(channel string) (pubsub.PubSubMessage, error) {
pubsub := ps.Client.Subscribe(context.Background(), channel)
_, err := pubsub.Receive(context.Background())
if err != nil {
return nil, err
}
lib.LogInfo.Printf("\n[PUBSUB/REDIS] Subscribed to channel %s\n", channel)
return &RedisPubSubMessage{pubsub: pubsub}, nil
}
func (r *RedisPubSub) PublishToChannel(channel string, message string) error {
err := r.Client.Publish(context.Background(), channel, message).Err()
if err != nil {
return err
}
lib.LogInfo.Printf("\n[PUBSUB/REDIS] Publishing message to channel %s: %s\n", channel, message)
return nil
}

16
lib/pubsub/interface.go Normal file
View File

@ -0,0 +1,16 @@
package pubsub
import "context"
type Message struct {
Payload string
}
type PubSubMessage interface {
ReceiveMessage(ctx context.Context) (*Message, error)
}
type PubSub interface {
SubscribeToChannel(channel string) (PubSubMessage, error)
PublishToChannel(channel string, message string) error
}

14
lib/s3.go Normal file
View File

@ -0,0 +1,14 @@
package lib
import (
"fmt"
"os"
)
func GeneratePublicURL(key string) string {
bucket := os.Getenv("BUCKET_NAME")
endpoint := os.Getenv("AWS_ENDPOINT_URL_S3")
url := fmt.Sprintf("%s/%s/%s", endpoint, bucket, key)
return url
}

64
lib/spotify.go Normal file
View File

@ -0,0 +1,64 @@
package lib
import (
"context"
"os"
"atri.dad/lib/pubsub"
"github.com/zmb3/spotify"
"golang.org/x/oauth2"
)
var spotifyOAuth2Endpoint = oauth2.Endpoint{
TokenURL: "https://accounts.spotify.com/api/token",
AuthURL: "https://accounts.spotify.com/authorize",
}
func GetCurrentlyPlayingTrack(clientID string, clientSecret string, refreshToken string) (*spotify.CurrentlyPlaying, error) {
// OAuth2 config
config := &oauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
Scopes: []string{spotify.ScopeUserReadCurrentlyPlaying},
Endpoint: spotifyOAuth2Endpoint,
}
// Token source
tokenSource := config.TokenSource(context.Background(), &oauth2.Token{RefreshToken: refreshToken})
// Get new token
newToken, err := tokenSource.Token()
if err != nil {
return nil, err
}
// Create new client
client := spotify.Authenticator{}.NewClient(newToken)
// Get currently playing track
playing, err := client.PlayerCurrentlyPlaying()
if err != nil {
return nil, err
}
return playing, nil
}
func CurrentlyPlayingTrackSSE(ctx context.Context, pubSub pubsub.PubSub) error {
clientID := os.Getenv("SPOTIFY_CLIENT_ID")
clientSecret := os.Getenv("SPOTIFY_CLIENT_SECRET")
refreshToken := os.Getenv("SPOTIFY_REFRESH_TOKEN")
playing, err := GetCurrentlyPlayingTrack(clientID, clientSecret, refreshToken)
if err != nil {
return err
}
if playing.Item != nil && playing.Playing {
SendSSE(ctx, pubSub, "spotify", `<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 {
SendSSE(ctx, pubSub, "spotify", "")
}
return nil
}

145
lib/sse.go Normal file
View File

@ -0,0 +1,145 @@
package lib
import (
"context"
"fmt"
"log"
"net/http"
"sync"
"time"
"atri.dad/lib/pubsub"
"github.com/labstack/echo/v4"
)
type SSEServerType struct {
clients map[string]map[chan string]bool
mu sync.Mutex
}
var SSEServer *SSEServerType
var mutex = &sync.Mutex{}
func init() {
SSEServer = &SSEServerType{
clients: make(map[string]map[chan string]bool),
}
}
func NewSSEServer() *SSEServerType {
return &SSEServerType{
clients: make(map[string]map[chan string]bool),
}
}
func (s *SSEServerType) AddClient(channel string, client chan string) {
s.mu.Lock()
defer s.mu.Unlock()
if _, ok := s.clients[channel]; !ok {
s.clients[channel] = make(map[chan string]bool)
}
s.clients[channel][client] = true
}
func (s *SSEServerType) RemoveClient(channel string, client chan string) {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.clients[channel], client)
if len(s.clients[channel]) == 0 {
delete(s.clients, channel)
}
}
func (s *SSEServerType) ClientCount(channel string) int {
s.mu.Lock()
defer s.mu.Unlock()
return len(s.clients[channel])
}
func SendSSE(ctx context.Context, messageBroker pubsub.PubSub, channel string, message string) error {
// Create a channel to receive an error from the goroutine
errCh := make(chan error, 1)
// Use a goroutine to send the message asynchronously
go func() {
select {
case <-ctx.Done():
// The client has disconnected, so return an error
errCh <- ctx.Err()
default:
err := messageBroker.PublishToChannel(channel, message)
errCh <- err // Send the error to the channel
}
}()
// Wait for the goroutine to finish and check for errors
err := <-errCh
if err != nil {
return err
}
return nil
}
func SetSSEHeaders(c echo.Context) {
c.Response().Header().Set(echo.HeaderContentType, "text/event-stream")
c.Response().Header().Set(echo.HeaderConnection, "keep-alive")
c.Response().Header().Set(echo.HeaderCacheControl, "no-cache")
}
func CreateTickerAndKeepAlive(c echo.Context, duration time.Duration) *time.Ticker {
ticker := time.NewTicker(duration)
go func() {
for range ticker.C {
if _, err := c.Response().Write([]byte(": keep-alive\n\n")); err != nil {
log.Printf("Failed to write keep-alive: %v", err)
}
c.Response().Flush()
}
}()
return ticker
}
func HandleIncomingMessages(c echo.Context, pubsub pubsub.PubSubMessage, client chan string) {
for {
select {
case <-c.Request().Context().Done():
// The client has disconnected. Stop trying to send messages.
return
default:
// The client is still connected. Continue processing messages.
msg, err := pubsub.ReceiveMessage(c.Request().Context())
if err != nil {
log.Printf("Failed to receive message: %v", err)
continue
}
data := fmt.Sprintf("data: %s\n\n", msg.Payload)
mutex.Lock()
_, err = c.Response().Write([]byte(data))
mutex.Unlock()
if err != nil {
log.Printf("Failed to write message: %v", err)
return // Stop processing if an error occurs
}
// Check if the ResponseWriter is nil before trying to flush it
if c.Response().Writer != nil {
// Check if the ResponseWriter implements http.Flusher before calling Flush
flusher, ok := c.Response().Writer.(http.Flusher)
if ok {
flusher.Flush()
} else {
log.Println("Failed to flush: ResponseWriter does not implement http.Flusher")
}
} else {
log.Println("Failed to flush: ResponseWriter is nil")
}
}
}
}

41
lib/stripe.go Normal file
View File

@ -0,0 +1,41 @@
package lib
import (
"log"
"net/http"
"os"
"github.com/joho/godotenv"
"github.com/stripe/stripe-go/v76"
"github.com/stripe/stripe-go/v76/checkout/session"
)
// init function
func init() {
godotenv.Load(".env")
stripe.Key = os.Getenv("STRIPE_SECRET_KEY")
}
func CreateCheckoutSession(w http.ResponseWriter, r *http.Request, successUrl string, cancelUrl string, priceId string) {
params := &stripe.CheckoutSessionParams{
LineItems: []*stripe.CheckoutSessionLineItemParams{
{
// Provide the exact Price ID (for example, pr_1234) of the product you want to sell
Price: stripe.String(priceId),
Quantity: stripe.Int64(1),
},
},
Mode: stripe.String(string(stripe.CheckoutSessionModePayment)),
SuccessURL: stripe.String(successUrl),
CancelURL: stripe.String(cancelUrl),
AutomaticTax: &stripe.CheckoutSessionAutomaticTaxParams{Enabled: stripe.Bool(true)},
}
s, err := session.New(params)
if err != nil {
log.Printf("session.New: %v", err)
}
http.Redirect(w, r, s.URL, http.StatusSeeOther)
}

43
lib/templates.go Normal file
View File

@ -0,0 +1,43 @@
package lib
import (
"html/template"
"log"
"net/http"
"path/filepath"
"runtime"
templatefs "atri.dad/pages/templates"
)
func RenderTemplate(w http.ResponseWriter, layout string, partials []string, props interface{}) error {
// Get the name of the current file
_, filename, _, _ := runtime.Caller(1)
page := filepath.Base(filename)
page = page[:len(page)-len(filepath.Ext(page))] // remove the file extension
// Build the list of templates
templates := []string{
"layouts/" + layout + ".html",
page + ".html",
}
for _, partial := range partials {
templates = append(templates, "partials/"+partial+".html")
}
// Parse the templates
ts, err := template.ParseFS(templatefs.FS, templates...)
if err != nil {
log.Print(err.Error())
return err
}
// Execute the layout template
err = ts.ExecuteTemplate(w, layout, props)
if err != nil {
log.Print(err.Error())
return err
}
return nil
}