Skip to content

Commit

Permalink
new: Enable push notification
Browse files Browse the repository at this point in the history
  • Loading branch information
ybizeul committed Sep 3, 2023
1 parent 2247df7 commit 7d2e9e4
Show file tree
Hide file tree
Showing 14 changed files with 462 additions and 136 deletions.
24 changes: 7 additions & 17 deletions cmd/ybfeed/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package main

import (
"fmt"
"net/http"
"os"

"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -78,23 +76,15 @@ func run() {
makeDataDirectory(dataDir)

// Start HTTP Server
r := http.NewServeMux()

api := handlers.ApiHandler{
BasePath: dataDir,
Version: version,
MaxBodySize: maxBodySize * 1024 * 1024,
}

r.HandleFunc("/api/", api.ApiHandleFunc)
r.HandleFunc("/", handlers.RootHandlerFunc)

slog.Info("ybFeed starting", slog.String("version", version), slog.String("data_dir", dataDir), slog.Int("port", HTTP_PORT), slog.Int("max-upload-size", maxBodySize))
err := http.ListenAndServe(fmt.Sprintf(":%d", HTTP_PORT), r)
api, err := handlers.NewApiHandler(dataDir)
if err != nil {
slog.Error("Unable to start HTTP server", slog.String("error", err.Error()))
os.Exit(1)
return
}
api.Version = version
api.MaxBodySize = maxBodySize * 1024 * 1024
api.HttpPort = HTTP_PORT

api.StartServer()
}

func makeDataDirectory(dir string) {
Expand Down
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ module github.com/ybizeul/ybfeed
go 1.21

require (
github.com/Appboy/webpush-go v0.0.0-20221006204155-f206645c3cb7
github.com/davecgh/go-spew v1.1.1
github.com/go-chi/chi/v5 v5.0.10
github.com/google/uuid v1.3.0
github.com/sirupsen/logrus v1.9.3
github.com/urfave/cli/v2 v2.25.7
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/go-chi/chi v1.5.4 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/crypto v0.0.0-20190131182504-b8fe1690c613 // indirect
)
26 changes: 10 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
github.com/Appboy/webpush-go v0.0.0-20221006204155-f206645c3cb7 h1:llWykCnBcqW1sbTI11bXzbFOkd/U4/Og64h/ifcwjPU=
github.com/Appboy/webpush-go v0.0.0-20221006204155-f206645c3cb7/go.mod h1:3IpCGyYxgZWbmm8zBOfp4C01dGq0AhGPTz3TT0Vv3k0=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-chi/chi v1.5.4 h1:QHdzF2szwjqVV4wmByUnTcsbIg7UGaQ0tPF2t5GcAIs=
github.com/go-chi/chi v1.5.4/go.mod h1:uaf8YgoFazUOkPBG7fxPftUylNumIev9awIWOENIuEg=
github.com/go-chi/chi/v5 v5.0.10 h1:rLz5avzKpjqxrYwXNfmjkrYYXOyLJd37pz53UFHC6vk=
github.com/go-chi/chi/v5 v5.0.10/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
golang.org/x/crypto v0.0.0-20190131182504-b8fe1690c613 h1:MQ/ZZiDsUapFFiMS+vzwXkCTeEKaum+Do5rINYJDmxc=
golang.org/x/crypto v0.0.0-20190131182504-b8fe1690c613/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb h1:mIKbk8weKhSeLH2GmUTrvx8CjkyJmnU1wFmg59CUjFA=
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
23 changes: 19 additions & 4 deletions internal/feed/feedConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
"path"
"time"

"github.com/Appboy/webpush-go"
"golang.org/x/exp/slog"
)

type FeedConfig struct {
Secret string `json:"secret"`
PIN *PIN `json:"pin,omitempty"`

feed *Feed
Secret string `json:"secret"`
PIN *PIN `json:"pin,omitempty"`
Subscriptions []webpush.Subscription
feed *Feed
}

type PIN struct {
Expand Down Expand Up @@ -118,6 +119,20 @@ func (config *FeedConfig) SetPIN(s string) error {
return nil
}

func (config *FeedConfig) AddSubscription(s webpush.Subscription) error {
for _, t := range config.Subscriptions {
if s.Endpoint == t.Endpoint && s.Keys.Auth == t.Keys.Auth && s.Keys.P256dh == t.Keys.P256dh {
return nil
}
}
config.Subscriptions = append(config.Subscriptions, s)
err := config.Write()
if err != nil {
return err
}
return nil
}

func (p *PIN) IsValid(s string) error {
if p.Expiration.Before(time.Now()) {
code := 401
Expand Down
Loading

0 comments on commit 7d2e9e4

Please sign in to comment.