-
Notifications
You must be signed in to change notification settings - Fork 0
/
mynona.go
188 lines (156 loc) · 4.4 KB
/
mynona.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"golang.org/x/exp/slices"
"rsc.io/quote"
)
// Config
type Config struct {
Credentials *Credentials `json:"credentials"`
Domains []string `json:"domains"`
}
var config Config
var migadu *MigaduAPI
type Credentials struct {
User string `json:"user"`
APIKey string `json:"apiKey"`
}
// Error handling
func main() {
// Error handling
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
fmt.Printf("ERROR: %v\n", err)
os.Exit(1)
}
}
// Logic
func run() error {
fmt.Println(quote.Go())
// Register API handlers
http.HandleFunc("/api/", apiHandler)
http.HandleFunc("/api/domains", domainsHandler)
// Read config
// var config Config
configBytes, err := ioutil.ReadFile("config.json")
if err != nil {
log.Fatal(err)
return err
}
json.Unmarshal(configBytes, &config)
// Create new API instance
migadu = NewMigaduAPI(config.Credentials.User, config.Credentials.APIKey)
migadu.POST("/domains/prestige-worldwi.de/mailboxes/demo/identities")
// Serve static files from the frontend/dist directory.
fs := http.FileServer(http.Dir("./frontend/dist"))
http.Handle("/", fs)
// Start the server.
fmt.Println("Server listening on port 3000")
log.Panic(
http.ListenAndServe(":3000", nil),
)
return nil
}
// Migadu API
type MigaduAPI struct {
user string
apiKey string
httpClient http.Client
}
func NewMigaduAPI(user string, apiKey string) *MigaduAPI {
return &MigaduAPI{user, apiKey, http.Client{}}
}
func (api MigaduAPI) GET(path string) string {
url := MigaduAPIEndpoint + path
request, _ := http.NewRequest("GET", url, nil)
request.SetBasicAuth(config.Credentials.User, config.Credentials.APIKey)
httpClient := http.Client{}
response, err := httpClient.Do(request)
if err != nil {
log.Fatal(err)
return ""
}
responseBodyBytes, _ := ioutil.ReadAll(response.Body)
responseBodyString := string(responseBodyBytes)
// var addressAliases = new(AddressAliases)
// if err := json.Unmarshal(responseBodyBytes, &addressAliases); err != nil {
// log.Fatal(err)
// return ""
// }
return responseBodyString
}
func (api MigaduAPI) POST(path string) string {
fmt.Println("CALL migadu.POST()")
url := MigaduAPIEndpoint + path
type Identity struct {
Name string `json:"name"`
Local_part string `json:"local_part"`
Password string `json:"password"`
}
identity := Identity{Name: "foobar.com", Local_part: "foobar", Password: "superpass"}
fmt.Println(identity)
identityJson, err := json.Marshal(identity)
fmt.Println(identityJson)
if err != nil {
log.Fatal(err)
}
request, _ := http.NewRequest("POST", url, bytes.NewBuffer(identityJson))
request.SetBasicAuth(config.Credentials.User, config.Credentials.APIKey)
request.Header.Set("Content-Type", "application/json")
fmt.Println(request)
httpClient := http.Client{}
response, err := httpClient.Do(request)
if err != nil {
log.Fatal(err)
return ""
}
responseBodyBytes, _ := ioutil.ReadAll(response.Body)
responseBodyString := string(responseBodyBytes)
// var addressAliases = new(AddressAliases)
// if err := json.Unmarshal(responseBodyBytes, &addressAliases); err != nil {
// log.Fatal(err)
// return ""
// }
fmt.Println("response Status:", response.Status)
fmt.Println("response Headers:", response.Header)
body, _ := ioutil.ReadAll(response.Body)
fmt.Println("response Body:", string(body))
return responseBodyString
}
// REST API Handlers
func apiHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println("Requested path:", r.URL.Path)
path := strings.Split(r.URL.Path, "/")[2:]
allowedPaths := []string{"mailboxes", "identities", "aliases", "rewrites"}
switch r.Method {
case "GET":
fmt.Println("GET")
if 3 <= len(path) && len(path) <= 4 && slices.Contains(allowedPaths, path[2]) {
fmt.Println("GET index aliases for", path[1])
if slices.Contains(config.Domains, path[1]) {
// Passthrough INDEX request
response := migadu.GET(strings.Join(path, "/"))
fmt.Println((response))
fmt.Fprintln(w, response)
} else {
http.Error(w, "Invalid Domain", http.StatusNotFound)
}
} else {
http.Error(w, "Not Found", http.StatusNotFound)
}
case "POST":
fmt.Println("POST")
default:
http.Error(w, "Method not supported", http.StatusBadRequest)
}
}
func domainsHandler(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(config.Domains)
}