-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
114 lines (89 loc) · 2.63 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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
"github.com/go-chi/render"
"github.com/joho/godotenv"
)
func main() {
_, _, domainsString, err := loadEnvVars()
if err != nil {
panic(err)
}
domains := strings.Split(domainsString, ",")
for _, domain := range domains {
if !strings.Contains(domain, ".") {
panic(fmt.Sprintf("Domain %s does not seem like a valid domain", domain))
}
}
r := chi.NewRouter()
r.Use(AddDetails)
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(cors.AllowAll().Handler)
r.Use(render.SetContentType(render.ContentTypeJSON))
workDir, _ := os.Getwd()
filesDir := http.Dir(filepath.Join(workDir, "frontend"))
FileServer(r, "/", filesDir)
domainsJSON, err := json.Marshal(domains)
if err != nil {
panic(err)
}
r.Get("/api/domains", func(w http.ResponseWriter, r *http.Request) {
w.Write(domainsJSON)
})
r.Mount("/api/{domain}/mailboxes", mailboxesResource{}.Routes())
r.Mount("/api/{domain}/identities", identitiesResource{}.Routes())
log.Fatal(http.ListenAndServe(":5000", r))
}
func AddDetails(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
adminEmail, APIKey, _, err := loadEnvVars()
if err != nil {
panic(err)
}
ctx := context.WithValue(r.Context(), "adminEmail", adminEmail)
ctx = context.WithValue(ctx, "APIKey", APIKey)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func FileServer(r chi.Router, path string, root http.FileSystem) {
if strings.ContainsAny(path, "{}*") {
panic("FileServer does not permit any URL parameters.")
}
if path != "/" && path[len(path)-1] != '/' {
r.Get(path, http.RedirectHandler(path+"/", 301).ServeHTTP)
path += "/"
}
path += "*"
r.Get(path, func(w http.ResponseWriter, r *http.Request) {
rctx := chi.RouteContext(r.Context())
pathPrefix := strings.TrimSuffix(rctx.RoutePattern(), "/*")
fs := http.StripPrefix(pathPrefix, http.FileServer(root))
fs.ServeHTTP(w, r)
})
}
func loadEnvVars() (string, string, string, error) {
godotenv.Load()
adminEmail := os.Getenv("MIGADU_ADMIN_EMAIL")
apiKey := os.Getenv("MIGADU_API_KEY")
if adminEmail == "" || apiKey == "" {
return "", "", "", fmt.Errorf("MIGADU_ADMIN_EMAIL and MIGADU_API_KEY must be set")
}
domainsString := os.Getenv("MIGADU_DOMAINS")
if domainsString == "" {
return "", "", "", fmt.Errorf("MIGADU_DOMAINS needs to have at least one domain set")
}
return adminEmail, apiKey, domainsString, nil
}