all repos — cameraman @ ab6a293f1e350018013a6eb54105b1e9210b0cc1

main.go (view raw)

 1package main
 2
 3import (
 4	"log"
 5	"os"
 6	"path"
 7	"time"
 8
 9	"github.com/gin-gonic/gin"
10	"github.com/joho/godotenv"
11	"gorm.io/driver/sqlite"
12	"gorm.io/gorm"
13)
14
15// Occurrence represents a scheduled event
16type Occurrence struct {
17	ID          uint      `gorm:"primaryKey" json:"id"`
18	Month       int       `json:"month"`
19	Day         int       `json:"day"`
20	Name        string    `json:"name"`
21	Description string    `json:"description"`
22	Notify      bool      `json:"notify"`
23	Notified    bool      `json:"notified"`
24	CreatedAt   time.Time `json:"-"`
25	UpdatedAt   time.Time `json:"-"`
26}
27
28var db *gorm.DB
29
30const (
31	dataDir = "data"
32	dbFile  = "occurrences.db"
33)
34
35func initDB() {
36	if _, err := os.Stat(dataDir); os.IsNotExist(err) {
37		err := os.Mkdir(dataDir, os.ModePerm)
38		if err != nil {
39			log.Fatal("Failed to create directory:", err)
40		}
41	}
42
43	var err error
44	db, err = gorm.Open(sqlite.Open(path.Join(dataDir, dbFile)), &gorm.Config{})
45	if err != nil {
46		log.Fatal("Failed to connect to database:", err)
47	}
48
49	db.AutoMigrate(&Occurrence{})
50}
51
52func loadEnv() {
53	if err := godotenv.Load(); err != nil {
54		log.Println("Error loading .env file")
55	}
56}
57
58func main() {
59	loadEnv()
60	initDB()
61	ParseTemplates()
62
63	go CheckOccurrences()
64
65	router := gin.Default()
66	router.POST("/occurrences", addOccurrence)
67	router.GET("/occurrences", getOccurrences)
68	router.DELETE("/occurrences/:id", deleteOccurrence)
69	router.GET("/", ShowIndexPage)
70
71	router.Run(":3000")
72}