phoneassistant/main.go

80 lines
1.6 KiB
Go
Raw Permalink Normal View History

2025-01-13 02:09:34 -05:00
package main
import (
"fmt"
"log"
"net/http"
"git.simponic.xyz/simponic/phoneassistant/api"
"git.simponic.xyz/simponic/phoneassistant/args"
2025-01-14 00:39:54 -05:00
"git.simponic.xyz/simponic/phoneassistant/assistant"
2025-01-13 02:09:34 -05:00
"git.simponic.xyz/simponic/phoneassistant/database"
"git.simponic.xyz/simponic/phoneassistant/ntfy"
"git.simponic.xyz/simponic/phoneassistant/scheduler"
"github.com/joho/godotenv"
)
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
err := godotenv.Load()
if err != nil {
log.Println("could not load .env file:", err)
}
argv, err := args.GetArgs()
if err != nil {
log.Fatal(err)
}
dbConn := database.MakeConn(&argv.DatabasePath)
defer dbConn.Close()
if argv.Migrate {
_, err = database.Migrate(dbConn)
if err != nil {
log.Fatal(err)
}
log.Println("database migrated successfully")
}
if argv.NtfyListener {
ntfy := ntfy.MakeNtfyWatcher(argv.NtfyEndpoint, argv.NtfyTopics)
notifications := ntfy.Watch()
go func() {
for notification := range notifications {
2025-01-14 00:10:50 -05:00
err := assistant.Do(notification.Message)
if err != nil {
log.Println("error when sending command: ", err)
}
2025-01-13 02:09:34 -05:00
}
}()
}
if argv.Scheduler {
go func() {
scheduler.StartScheduler(dbConn, argv)
}()
}
if argv.Server {
mux := api.MakeMux(argv, dbConn)
log.Println("🚀🚀 phoneassistant API listening on port", argv.Port)
go func() {
server := &http.Server{
Addr: ":" + fmt.Sprint(argv.Port),
Handler: mux,
}
err = server.ListenAndServe()
if err != nil {
log.Fatal(err)
}
}()
}
if argv.Server || argv.Scheduler || argv.NtfyListener {
select {} // block forever
}
}