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"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
@ -101,6 +100,9 @@ func main() {
|
||||||
log.Fatalf("Error getting blogs: %v", err)
|
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
|
// Start watching for changes in the data directory
|
||||||
go watchForChanges(dataDir)
|
go watchForChanges(dataDir)
|
||||||
|
|
||||||
|
@ -185,6 +187,18 @@ func main() {
|
||||||
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
|
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{}) {
|
func renderTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
|
||||||
tmplPath := filepath.Join(templateDir, tmpl)
|
tmplPath := filepath.Join(templateDir, tmpl)
|
||||||
t, err := template.ParseFiles(tmplPath)
|
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)
|
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) {
|
func getBlogs(dir string) ([]Blog, error) {
|
||||||
var blogs []Blog
|
var blogs []Blog
|
||||||
|
|
||||||
files, err := ioutil.ReadDir(dir)
|
files, err := os.ReadDir(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -286,7 +293,7 @@ func getBlogs(dir string) ([]Blog, error) {
|
||||||
func getBlogEntries(dir string) (Blog, error) {
|
func getBlogEntries(dir string) (Blog, error) {
|
||||||
var entries []BlogEntry
|
var entries []BlogEntry
|
||||||
|
|
||||||
files, err := ioutil.ReadDir(dir)
|
files, err := os.ReadDir(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Blog{}, err
|
return Blog{}, err
|
||||||
}
|
}
|
||||||
|
@ -315,7 +322,7 @@ func getBlogEntries(dir string) (Blog, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseMarkdownFile(path string) (BlogEntry, error) {
|
func parseMarkdownFile(path string) (BlogEntry, error) {
|
||||||
content, err := ioutil.ReadFile(path)
|
content, err := os.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return BlogEntry{}, err
|
return BlogEntry{}, err
|
||||||
}
|
}
|
||||||
|
@ -497,6 +504,7 @@ func handleFileChange(path string, isNew bool) {
|
||||||
dir := filepath.Dir(path)
|
dir := filepath.Dir(path)
|
||||||
blogName := filepath.Base(dir)
|
blogName := filepath.Base(dir)
|
||||||
updateBlogEntries(blogName, path)
|
updateBlogEntries(blogName, path)
|
||||||
|
checkAndSendNotifications()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
34
save.go
34
save.go
|
@ -2,35 +2,49 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func loadNotifiedEntries() {
|
func loadNotifiedEntries() {
|
||||||
data, err := ioutil.ReadFile(notifiedFilePath)
|
file, err := os.Open(notifiedFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
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 {
|
if err != nil {
|
||||||
log.Fatalf("Error parsing notified entries file: %v", err)
|
log.Fatalf("Error decoding notified entries: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func saveNotifiedEntry(entryNumber int) {
|
func saveNotifiedEntry(entryNumber int) {
|
||||||
notifiedEntries[entryNumber] = true
|
notifiedEntries[entryNumber] = true
|
||||||
data, err := json.Marshal(notifiedEntries)
|
file, err := os.Create(notifiedFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error serializing notified entries: %v", err)
|
log.Fatalf("Error saving notified entries: %v", err)
|
||||||
}
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
err = ioutil.WriteFile(notifiedFilePath, data, 0644)
|
err = json.NewEncoder(file).Encode(notifiedEntries)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error writing notified entries file: %v", err)
|
log.Fatalf("Error encoding notified entries: %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