summaryrefslogtreecommitdiff
path: root/template/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'template/main.go')
-rw-r--r--template/main.go76
1 files changed, 76 insertions, 0 deletions
diff --git a/template/main.go b/template/main.go
new file mode 100644
index 0000000..86783ff
--- /dev/null
+++ b/template/main.go
@@ -0,0 +1,76 @@
+package main
+
+import (
+ "fmt"
+ "log"
+ "net/http"
+
+ "git.simponic.xyz/simponic/whois/api"
+ "git.simponic.xyz/simponic/whois/args"
+ "git.simponic.xyz/simponic/whois/database"
+ "git.simponic.xyz/simponic/whois/scheduler"
+ "git.simponic.xyz/simponic/whois/ntfy"
+ "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("🚀🚀 whois 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
+ }
+}