all repos — cameraman @ 33229b4ef241f4fc2776ea94d2fb47dbf1e039e5

main.go (view raw)

  1package main
  2
  3import (
  4	"fmt"
  5	"log"
  6	"net/http"
  7	"os"
  8	"path"
  9	"strconv"
 10	"time"
 11
 12	"github.com/joho/godotenv"
 13	"gorm.io/driver/sqlite"
 14	"gorm.io/gorm"
 15)
 16
 17// Occurrence represents a scheduled event
 18type Occurrence struct {
 19	ID        uint      `json:"id" gorm:"primaryKey"`
 20	CreatedAt time.Time `json:"created_at"`
 21	UpdatedAt time.Time `json:"updated_at"`
 22
 23	Day          uint   `json:"day"`
 24	Month        uint   `json:"month"`
 25	Year         *uint  `json:"year"`
 26	Name         string `json:"name"`
 27	Description  string `json:"description"`
 28	Notify       bool   `json:"notify"`
 29	Notified     bool   `json:"notified"`
 30	NotifiedSoft bool   `json:"notified_soft"`
 31}
 32
 33const (
 34	dataDir                       = "data"
 35	dbFile                        = "occurrences.db"
 36	defaultNotificationWindow     = 5
 37	defaultSoftNotificationWindow = 2
 38	defaultSleepDuration          = 1
 39	defaultPort                   = "3000"
 40)
 41
 42var (
 43	db   *gorm.DB
 44	port string
 45)
 46
 47func initDB() {
 48	if _, err := os.Stat(dataDir); os.IsNotExist(err) {
 49		err := os.Mkdir(dataDir, os.ModePerm)
 50		if err != nil {
 51			log.Fatal("Failed to create directory:", err)
 52		}
 53	}
 54
 55	var err error
 56	db, err = gorm.Open(sqlite.Open(path.Join(dataDir, dbFile)), &gorm.Config{})
 57	if err != nil {
 58		log.Fatal("Failed to connect to database:", err)
 59	}
 60
 61	db.AutoMigrate(&Occurrence{})
 62}
 63
 64func loadEnv() {
 65	err := godotenv.Load()
 66	if err != nil {
 67		log.Println("Error loading .env file")
 68	}
 69
 70	NotificationWindow, err = strconv.Atoi(os.Getenv("DAYS_BEFORE_NOTIFICATION"))
 71	if err != nil {
 72		NotificationWindow = defaultNotificationWindow
 73	}
 74	log.Println("Notification window (days):", NotificationWindow)
 75
 76	SoftNotificationWindow, err = strconv.Atoi(os.Getenv("DAYS_BEFORE_SOFT_NOTIFICATION"))
 77	if err != nil {
 78		SoftNotificationWindow = defaultSoftNotificationWindow
 79	}
 80	log.Println("Soft notification window (days):", SoftNotificationWindow)
 81
 82	loadedSleepDuration, err := strconv.Atoi(os.Getenv("HOURS_BETWEEN_CHECKS"))
 83	if err != nil {
 84		SleepDuration = defaultSleepDuration * time.Hour
 85	} else {
 86		SleepDuration = time.Duration(loadedSleepDuration) * time.Hour
 87	}
 88	log.Println("Sleep duration:", SleepDuration)
 89
 90	port = os.Getenv("PORT")
 91	if port == "" {
 92		port = defaultPort
 93	}
 94}
 95
 96func main() {
 97	loadEnv()
 98	initDB()
 99	ParseTemplates()
100
101	go CheckOccurrences()
102
103	http.HandleFunc("GET /", ShowIndexPage)
104
105	http.HandleFunc("GET /occurrences", getOccurrences)
106	http.HandleFunc("POST /occurrences", addOccurrence)
107	http.HandleFunc("DELETE /occurrences/{id}", deleteOccurrence)
108
109	log.Println("Starting server at port " + port + "...")
110	if err := http.ListenAndServe(":"+port, nil); err != nil {
111		fmt.Println("Server failed to start:", err)
112	}
113}