From 806cfb01bdbf4eafb6f110a28bb868a98efe3462 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Fri, 23 Aug 2024 21:09:36 +0200 Subject: [PATCH 1/4] Fixed bug where SYSTEMD_DIR is not defined --- update.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/update.sh b/update.sh index 8795306..448d6a1 100755 --- a/update.sh +++ b/update.sh @@ -6,6 +6,7 @@ CPOLIS_DIR=$EXTRACTION_DIR/cpolis TAILWINDCSS_REPO_URL=https://api.github.com/repos/tailwindlabs/tailwindcss/releases/latest TMP_DIR=/tmp BIN_DIR=/usr/local/bin +SYSTEMD_DIR=/etc/systemd/system check_dependency() { if ! which $1 >/dev/null 2>&1; then From 6a4a592714688ec8daca902cde10965daf1c9128 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Fri, 23 Aug 2024 21:14:25 +0200 Subject: [PATCH 2/4] Fixed bug where tailwindcss was using a relative path --- update.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/update.sh b/update.sh index 448d6a1..f2555d7 100755 --- a/update.sh +++ b/update.sh @@ -40,7 +40,7 @@ curl -s $TAILWINDCSS_REPO_URL | cut -d'"' -f4 | xargs -r curl -Lo $CPOLIS_DIR/tailwindcss chmod +x $CPOLIS_DIR/tailwindcss -$CPOLIS_DIR/tailwindcss -i web/static/css/input.css -o web/static/css/style.css +$CPOLIS_DIR/tailwindcss -i $CPOLIS_DIR/web/static/css/input.css -o $CPOLIS_DIR/web/static/css/style.css echo -e 'Building cpolis...' >&2 go build -o $TMP_DIR/cpolis $CPOLIS_DIR/cmd/main.go From b451da7e8eac73528d0e6d871c821e6129b302c3 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Fri, 23 Aug 2024 21:23:05 +0200 Subject: [PATCH 3/4] Fixed echo commands --- update.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/update.sh b/update.sh index f2555d7..bd2f4a0 100755 --- a/update.sh +++ b/update.sh @@ -26,14 +26,14 @@ check_dependency jq check_dependency tar check_dependency xargs -echo -e '\nDownloading cpolis...' >&2 +echo '\nDownloading cpolis...' >&2 rm -fr $CPOLIS_DIR/* latest_release=$(curl -s $CPOLIS_REPO_URL | jq -r '.[0].tag_name') curl -Lo $TMP_DIR/cpolis.tar.gz https://git.streifling.com/jason/cpolis/archive/$latest_release.tar.gz tar -xzf $TMP_DIR/cpolis.tar.gz -C $EXTRACTION_DIR rm $TMP_DIR/cpolis.tar.gz -echo -e '\nDownloading TailwindCSS...' >&2 +echo '\nDownloading TailwindCSS...' >&2 curl -s $TAILWINDCSS_REPO_URL | grep -F browser_download_url | grep -F linux-x64 | @@ -42,10 +42,10 @@ curl -s $TAILWINDCSS_REPO_URL | chmod +x $CPOLIS_DIR/tailwindcss $CPOLIS_DIR/tailwindcss -i $CPOLIS_DIR/web/static/css/input.css -o $CPOLIS_DIR/web/static/css/style.css -echo -e 'Building cpolis...' >&2 +echo '\nBuilding cpolis...' >&2 go build -o $TMP_DIR/cpolis $CPOLIS_DIR/cmd/main.go -echo -e 'Setting system files up...' >&2 +echo '\nSetting system files up...' >&2 sudo mv $TMP_DIR/cpolis $BIN_DIR/cpolis sudo chown root:root $BIN_DIR/cpolis chmod +x $BIN_DIR/cpolis From 365d5a68a1d0950c5f8f7989637a5478e4235ca3 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Fri, 23 Aug 2024 21:45:10 +0200 Subject: [PATCH 4/4] Added config for Firebase key --- cmd/backend/config.go | 28 ++++++++++++++++++---------- cmd/backend/firebase.go | 4 ++-- cmd/calls/pdf.go | 4 ++-- cmd/calls/rss.go | 2 +- cmd/calls/verification.go | 4 ++-- 5 files changed, 25 insertions(+), 17 deletions(-) diff --git a/cmd/backend/config.go b/cmd/backend/config.go index 9ef36a6..7f5441b 100644 --- a/cmd/backend/config.go +++ b/cmd/backend/config.go @@ -14,6 +14,7 @@ type Config struct { DBName string Description string Domain string + FirebaseKey string KeyFile string Link string LogFile string @@ -27,13 +28,14 @@ type Config struct { func newConfig() *Config { return &Config{ - DBName: "cpolis", - KeyFile: "/var/www/cpolis/cpolis.key", - LogFile: "/var/log/cpolis.log", - PDFDir: "/var/www/cpolis/pdfs", - PicsDir: "/var/www/cpolis/pics", - RSSFile: "/var/www/cpolis/cpolis.rss", - WebDir: "/var/www/cpolis/web", + DBName: "cpolis", + FirebaseKey: "/var/www/cpolis/serviceAccountKey.json", + KeyFile: "/var/www/cpolis/cpolis.key", + LogFile: "/var/log/cpolis.log", + PDFDir: "/var/www/cpolis/pdfs", + PicsDir: "/var/www/cpolis/pics", + RSSFile: "/var/www/cpolis/cpolis.rss", + WebDir: "/var/www/cpolis/web", } } @@ -76,19 +78,25 @@ func (c *Config) handleCliArgs() error { port := 8080 flag.StringVar(&c.DBName, "db", c.DBName, "DB name") - flag.StringVar(&c.Description, "desc", c.Description, "Channel description") + flag.StringVar(&c.Description, "desc", c.Description, "channel description") flag.StringVar(&c.Domain, "domain", c.Domain, "domain name") + flag.StringVar(&c.FirebaseKey, "firebase", c.FirebaseKey, "Firebase service account key file") flag.StringVar(&c.KeyFile, "key", c.KeyFile, "key file") - flag.StringVar(&c.Link, "link", c.Link, "Channel Link") + flag.StringVar(&c.Link, "link", c.Link, "channel Link") flag.StringVar(&c.LogFile, "log", c.LogFile, "log file") flag.StringVar(&c.PDFDir, "pdfs", c.PDFDir, "pdf directory") flag.StringVar(&c.PicsDir, "pics", c.PicsDir, "pictures directory") flag.StringVar(&c.RSSFile, "rss", c.RSSFile, "RSS file") - flag.StringVar(&c.Title, "title", c.Title, "Channel title") + flag.StringVar(&c.Title, "title", c.Title, "channel title") flag.StringVar(&c.WebDir, "web", c.WebDir, "web directory") flag.IntVar(&port, "port", port, "port") flag.Parse() + c.FirebaseKey, err = filepath.Abs(c.FirebaseKey) + if err != nil { + return fmt.Errorf("error finding absolute path for Firebase service account key file: %v", err) + } + c.KeyFile, err = filepath.Abs(c.KeyFile) if err != nil { return fmt.Errorf("error finding absolute path for key file: %v", err) diff --git a/cmd/backend/firebase.go b/cmd/backend/firebase.go index e58e8b4..71dd8d0 100644 --- a/cmd/backend/firebase.go +++ b/cmd/backend/firebase.go @@ -12,12 +12,12 @@ type Client struct { *auth.Client } -func NewClient() (*Client, error) { +func NewClient(c *Config) (*Client, error) { var err error client := new(Client) ctx := context.Background() - opt := option.WithCredentialsFile("path/to/serviceAccountKey.json") + opt := option.WithCredentialsFile(c.FirebaseKey) app, err := firebase.NewApp(ctx, nil, opt) if err != nil { diff --git a/cmd/calls/pdf.go b/cmd/calls/pdf.go index 7369b8e..8779634 100644 --- a/cmd/calls/pdf.go +++ b/cmd/calls/pdf.go @@ -12,7 +12,7 @@ import ( func ServePDFList(c *b.Config) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - if tokenIsVerified(w, r) { + if tokenIsVerified(w, r, c) { files, err := os.ReadDir(c.PDFDir) if err != nil { log.Println(err) @@ -37,7 +37,7 @@ func ServePDFList(c *b.Config) http.HandlerFunc { func ServePDF(c *b.Config) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - if tokenIsVerified(w, r) { + if tokenIsVerified(w, r, c) { http.ServeFile(w, r, fmt.Sprint(c.PDFDir, "/", r.PathValue("id"))) } } diff --git a/cmd/calls/rss.go b/cmd/calls/rss.go index f98c9e1..f8bc83a 100644 --- a/cmd/calls/rss.go +++ b/cmd/calls/rss.go @@ -8,7 +8,7 @@ import ( func ServeRSS(c *b.Config) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - if tokenIsVerified(w, r) { + if tokenIsVerified(w, r, c) { http.ServeFile(w, r, c.RSSFile) } } diff --git a/cmd/calls/verification.go b/cmd/calls/verification.go index 2a25665..ac8f109 100644 --- a/cmd/calls/verification.go +++ b/cmd/calls/verification.go @@ -8,7 +8,7 @@ import ( ) // tokenIsVerified verifies that a request is authorized. It returns a bool. -func tokenIsVerified(w http.ResponseWriter, r *http.Request) bool { +func tokenIsVerified(w http.ResponseWriter, r *http.Request, c *b.Config) bool { idToken := r.Header.Get("Authorization") if idToken == "" { log.Println("Authorization header missing") @@ -16,7 +16,7 @@ func tokenIsVerified(w http.ResponseWriter, r *http.Request) bool { return false } - client, err := b.NewClient() + client, err := b.NewClient(c) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError)