all repos — cameraman @ e9af61f938b31b2bb9589e5bb079370941294021

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}
 31
 32const (
 33	dataDir                   = "data"
 34	dbFile                    = "occurrences.db"
 35	defaultNotificationWindow = 3
 36	defaultSleepDuration      = 1
 37	defaultPort               = "3000"
 38)
 39
 40var (
 41	db   *gorm.DB
 42	port string
 43)
 44
 45func initDB() {
 46	if _, err := os.Stat(dataDir); os.IsNotExist(err) {
 47		err := os.Mkdir(dataDir, os.ModePerm)
 48		if err != nil {
 49			log.Fatal("Failed to create directory:", err)
 50		}
 51	}
 52
 53	var err error
 54	db, err = gorm.Open(sqlite.Open(path.Join(dataDir, dbFile)), &gorm.Config{})
 55	if err != nil {
 56		log.Fatal("Failed to connect to database:", err)
 57	}
 58
 59	db.AutoMigrate(&Occurrence{})
 60}
 61
 62func loadEnv() {
 63	err := godotenv.Load()
 64	if err != nil {
 65		log.Println("Error loading .env file")
 66	}
 67
 68	NotificationWindow, err = strconv.Atoi(os.Getenv("DAYS_BEFORE_NOTIFICATION"))
 69	if err != nil {
 70		NotificationWindow = defaultNotificationWindow
 71	}
 72	log.Println("Notification window (days):", NotificationWindow)
 73
 74	loadedSleepDuration, err := strconv.Atoi(os.Getenv("HOURS_BETWEEN_CHECKS"))
 75	if err != nil {
 76		SleepDuration = defaultSleepDuration * time.Hour
 77	} else {
 78		SleepDuration = time.Duration(loadedSleepDuration) * time.Hour
 79	}
 80	log.Println("Sleep duration:", SleepDuration)
 81
 82	port = os.Getenv("PORT")
 83	if port == "" {
 84		port = defaultPort
 85	}
 86}
 87
 88func main() {
 89	loadEnv()
 90	initDB()
 91	ParseTemplates()
 92
 93	go CheckOccurrences()
 94
 95	http.HandleFunc("GET /", ShowIndexPage)
 96
 97	http.HandleFunc("GET /occurrences", getOccurrences)
 98	http.HandleFunc("POST /occurrences", addOccurrence)
 99	http.HandleFunc("DELETE /occurrences/{id}", deleteOccurrence)
100
101	log.Println("Starting server at port " + port + "...")
102	if err := http.ListenAndServe(":"+port, nil); err != nil {
103		fmt.Println("Server failed to start:", err)
104	}
105}