summaryrefslogtreecommitdiff
path: root/main.go
blob: b721f90c85019bf8bb19073b8e6e64d6f93e1aca (plain)
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
package main

import (
	"fmt"
	"log"
	"net/http"

	"git.simponic.xyz/simponic/phoneassistant/api"
	"git.simponic.xyz/simponic/phoneassistant/args"
	"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 {
				message := notification.Message
				log.Println("got message", message)
			}
		}()
	}

	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
	}
}