-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.go
115 lines (99 loc) · 2.84 KB
/
app.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
package main
import (
"database/sql"
"embed"
"fmt"
"html/template"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/getsentry/sentry-go"
"github.com/joho/godotenv"
"github.com/mblayman/journal/entries"
"github.com/mblayman/journal/webhook"
_ "modernc.org/sqlite"
)
//go:embed templates
var templates embed.FS
var tmpl = template.Must(template.ParseFS(templates, "templates/index.html"))
func index(w http.ResponseWriter, r *http.Request) {
err := tmpl.Execute(w, nil)
if err != nil {
http.Error(w, "Failed to render template", http.StatusInternalServerError)
}
}
func up(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "ok")
}
func getWebhookAuth() (string, string) {
webhookSecret := os.Getenv("ANYMAIL_WEBHOOK_SECRET")
if webhookSecret == "" {
log.Fatal("ANYMAIL_WEBHOOK_SECRET not set.")
}
parts := strings.Split(webhookSecret, ":")
if len(parts) != 2 {
log.Fatalf("ANYMAIL_WEBHOOK_SECRET must be in format 'username:password', got: %s", webhookSecret)
}
username := parts[0]
password := parts[1]
return username, password
}
func main() {
logger := log.New(os.Stderr, "", log.LstdFlags)
err := godotenv.Load()
if err != nil {
logger.Printf("Could not load .env file: %v (continuing without it)", err)
}
sentryDsn := os.Getenv("SENTRY_DSN")
if sentryDsn != "" {
err := sentry.Init(sentry.ClientOptions{
Dsn: sentryDsn,
})
if err != nil {
log.Fatalf("sentry.Init: %s", err)
}
defer sentry.Flush(2 * time.Second)
logger.Println("Sentry is enabled.")
} else {
logger.Println("Sentry is disabled.")
}
requiredToAddress := os.Getenv("REQUIRED_TO_ADDRESS")
if requiredToAddress == "" {
log.Fatal("REQUIRED_TO_ADDRESS not set.")
}
mattEmailAddress := os.Getenv("MATT_EMAIL_ADDRESS")
if mattEmailAddress == "" {
log.Fatal("MATT_EMAIL_ADDRESS not set.")
}
dbPath := "./db.sqlite3"
if dir := os.Getenv("DB_DIR"); dir != "" {
dbPath = filepath.Join(dir, "db.sqlite3")
}
db, err := sql.Open("sqlite", dbPath)
if err != nil {
log.Fatalf("Failed to open database: %v", err)
}
defer db.Close()
logger.Printf("Opened database at %s.", dbPath)
// Email gateway setup
sendgridAPIKey := os.Getenv("SENDGRID_API_KEY")
if sendgridAPIKey == "" {
log.Fatal("SENDGRID_API_KEY not set.")
}
emailGateway := entries.NewSendGridGateway(sendgridAPIKey)
mux := http.NewServeMux()
mux.HandleFunc("/", index)
mux.HandleFunc("/up", up)
username, password := getWebhookAuth()
processor := entries.MakeEmailContentProcessor(requiredToAddress, db, logger)
mux.HandleFunc("/webhook", webhook.WebhookHandler(username, password, processor, logger))
entries.RunDailyEmailTask(db, emailGateway, requiredToAddress, mattEmailAddress, logger)
logger.Println("Server starting on port 8000...")
err = http.ListenAndServe(":8000", mux)
if err != nil {
logger.Printf("Server failed to start: %v\n", err)
}
}