-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
145 lines (121 loc) · 3.74 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"context"
"database/sql"
"embed"
"flag"
"fmt"
"github.com/gin-gonic/gin"
"io/fs"
"kratomTracker/doses"
"kratomTracker/notificationmanager"
"kratomTracker/notificationmanager/services"
"kratomTracker/remindersManager"
"net/http"
"os"
"strconv"
"time"
)
import (
_ "github.com/mattn/go-sqlite3"
)
//go:embed frontend/dist/assets/*
var publicAssets embed.FS
//go:embed frontend/dist/index.html
var indexHTML string
func handleRepoErr(err error) {
if err != nil {
panic(err)
}
}
// printLocalTime prints the local time to the console.
func printLocalTime() {
fmt.Println("Local Time: ", time.Now().Local().Format("2006-01-02 15:04:05"))
}
func fsHandler() http.Handler {
sub, err := fs.Sub(publicAssets, "frontend/dist")
if err != nil {
panic(err)
}
return http.FileServer(http.FS(sub))
}
func main() {
RESEND_API_KEY, resendAPIKeyExists := os.LookupEnv("RESEND_API_KEY")
RESEND_FROM_EMAIL, resendFromExists := os.LookupEnv("RESEND_FROM_EMAIL")
ctx, cancel := context.WithCancel(context.Background())
// Print the local time to the console.
printLocalTime()
// Get Flags
portFlag := flag.Int("port", 8080, "Port to run the server on")
flag.Parse()
// Setup Database
db, err := sql.Open("sqlite3", "kratom_tracker_app.db?_time_format=sqlite")
if err != nil {
panic(err)
}
defer db.Close()
// Setup Repositories
notifRepo, err := notificationmanager.NewSqliteNotificationRepository(db)
handleRepoErr(err)
defer notifRepo.Close()
// Setup Notification Manager
notifManager := notificationmanager.NewNotificationManager(notifRepo)
notifManager.AddService(&services.ConsoleNotificationServce{})
var emailService notificationmanager.NotificationService
// Setup Email Notification Service
if !resendAPIKeyExists || !resendFromExists {
fmt.Println("Email notification service not configured. Please set RESEND_API_KEY and RESEND_FROM_EMAIL environment variables.")
} else {
fmt.Println("Email notification service configured.")
emailService = services.NewEmailNotificationService(RESEND_API_KEY, RESEND_FROM_EMAIL)
//emailService.AddEmail("jonathon@jonathonchambers.com")
err = notifManager.AddService(emailService)
if err != nil {
fmt.Printf("Error adding email: %s due to an error: %s", emailService, err)
}
}
// Setup Dose Repository
doseRepo, err := doses.NewSqliteDoseRepository(db, notifManager)
handleRepoErr(err)
defer doseRepo.Close()
// Setup Reminder Manager
reminderManager := remindersManager.NewReminderManager()
go reminderManager.Start(ctx)
nextDoseTime, err := doseRepo.GetNextDoseTime()
if err != nil {
fmt.Println("Error getting next dose time: ", err)
} else {
reminderManager.SetReminder("Take Kratom", nextDoseTime)
}
// Setup API Server
devMode := os.Getenv("DEV_MODE") == "true"
if devMode {
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
}
router := gin.Default()
router.GET("/assets/*filepath", gin.WrapH(fsHandler()))
router.GET("/", func(context *gin.Context) {
context.Writer.WriteString(indexHTML)
})
router.Use(func(context *gin.Context) {
context.Header("Access-Control-Allow-Origin", "*")
context.Header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
context.Header("Access-Control-Allow-Headers", "Content-Type")
})
g := router.Group("/api")
g.GET("/doses", doses.GetAllDoses(doseRepo))
g.GET("/doses/today", doses.GetAllDosesToday(doseRepo))
g.GET("/doses/next", doses.GetNextDoseTime(doseRepo))
g.POST("/doses", doses.AddDose(doseRepo))
g.POST("/doses/now", doses.AddDoseNow(doseRepo))
portNumber := *portFlag
portNumberStr := strconv.Itoa(portNumber)
fmt.Println("Starting server on port " + portNumberStr)
apiErr := router.Run(":" + portNumberStr)
if apiErr != nil {
panic(apiErr)
}
cancel()
}