fixed incorrect notifications and removed deprecated lib
This commit is contained in:
parent
0bc34bdbf0
commit
469903dc9c
2 changed files with 43 additions and 21 deletions
30
main.go
30
main.go
|
@ -6,7 +6,6 @@ import (
|
|||
"flag"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
@ -101,6 +100,9 @@ func main() {
|
|||
log.Fatalf("Error getting blogs: %v", err)
|
||||
}
|
||||
|
||||
// Start the periodic notification checker
|
||||
go startNotificationChecker(10 * time.Minute)
|
||||
|
||||
// Start watching for changes in the data directory
|
||||
go watchForChanges(dataDir)
|
||||
|
||||
|
@ -185,6 +187,18 @@ func main() {
|
|||
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
|
||||
}
|
||||
|
||||
func startNotificationChecker(interval time.Duration) {
|
||||
ticker := time.NewTicker(interval)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
checkAndSendNotifications()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func renderTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
|
||||
tmplPath := filepath.Join(templateDir, tmpl)
|
||||
t, err := template.ParseFiles(tmplPath)
|
||||
|
@ -253,19 +267,12 @@ func renderBlogEntry(w http.ResponseWriter, r *http.Request, blogName string, en
|
|||
}
|
||||
|
||||
renderTemplate(w, "news.html", pageData)
|
||||
|
||||
// Handle notifications if they haven't been sent yet
|
||||
if !entry.Notified {
|
||||
sendNotifications(entry)
|
||||
entry.Notified = true
|
||||
saveNotifiedEntry(entry.Number)
|
||||
}
|
||||
}
|
||||
|
||||
func getBlogs(dir string) ([]Blog, error) {
|
||||
var blogs []Blog
|
||||
|
||||
files, err := ioutil.ReadDir(dir)
|
||||
files, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -286,7 +293,7 @@ func getBlogs(dir string) ([]Blog, error) {
|
|||
func getBlogEntries(dir string) (Blog, error) {
|
||||
var entries []BlogEntry
|
||||
|
||||
files, err := ioutil.ReadDir(dir)
|
||||
files, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
return Blog{}, err
|
||||
}
|
||||
|
@ -315,7 +322,7 @@ func getBlogEntries(dir string) (Blog, error) {
|
|||
}
|
||||
|
||||
func parseMarkdownFile(path string) (BlogEntry, error) {
|
||||
content, err := ioutil.ReadFile(path)
|
||||
content, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return BlogEntry{}, err
|
||||
}
|
||||
|
@ -497,6 +504,7 @@ func handleFileChange(path string, isNew bool) {
|
|||
dir := filepath.Dir(path)
|
||||
blogName := filepath.Base(dir)
|
||||
updateBlogEntries(blogName, path)
|
||||
checkAndSendNotifications()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
36
save.go
36
save.go
|
@ -2,35 +2,49 @@ package main
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func loadNotifiedEntries() {
|
||||
data, err := ioutil.ReadFile(notifiedFilePath)
|
||||
file, err := os.Open(notifiedFilePath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return // No file, no entries notified yet
|
||||
return
|
||||
}
|
||||
log.Fatalf("Error reading notified entries file: %v", err)
|
||||
log.Fatalf("Error loading notified entries: %v", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
err = json.Unmarshal(data, ¬ifiedEntries)
|
||||
err = json.NewDecoder(file).Decode(¬ifiedEntries)
|
||||
if err != nil {
|
||||
log.Fatalf("Error parsing notified entries file: %v", err)
|
||||
log.Fatalf("Error decoding notified entries: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func saveNotifiedEntry(entryNumber int) {
|
||||
notifiedEntries[entryNumber] = true
|
||||
data, err := json.Marshal(notifiedEntries)
|
||||
file, err := os.Create(notifiedFilePath)
|
||||
if err != nil {
|
||||
log.Fatalf("Error serializing notified entries: %v", err)
|
||||
log.Fatalf("Error saving notified entries: %v", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
err = json.NewEncoder(file).Encode(notifiedEntries)
|
||||
if err != nil {
|
||||
log.Fatalf("Error encoding notified entries: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(notifiedFilePath, data, 0644)
|
||||
if err != nil {
|
||||
log.Fatalf("Error writing notified entries file: %v", err)
|
||||
func checkAndSendNotifications() {
|
||||
for _, blog := range blogs {
|
||||
for _, entry := range blog.Entries {
|
||||
if !entry.Notified && !time.Now().Before(entry.Date) {
|
||||
sendNotifications(entry)
|
||||
entry.Notified = true
|
||||
saveNotifiedEntry(entry.Number)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue