-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
67 lines (59 loc) · 1.46 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
package main
import (
"embed"
"fmt"
"html/template"
"io"
"log"
"net/http"
"net/url"
"os"
"strings"
)
//go:embed templates/*
var resources embed.FS
var t = template.Must(template.ParseFS(resources, "templates/*"))
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
slackToken := os.Getenv("SLACK_TOKEN")
if slackToken == "" {
fmt.Println("Env SLACK_TOKEN is required")
os.Exit(1)
}
slackAddress := os.Getenv("SLACK_ADDRESS")
if slackAddress == "" {
fmt.Println("Env SLACK_ADDRESS is required. Example: pymi.slack.com")
os.Exit(1)
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
data := map[string]string{}
t.ExecuteTemplate(w, "index.html.tmpl", data)
})
http.HandleFunc("/invite", func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
log.Fatal(err)
}
log.Printf("Form: %v", r.Form)
email := strings.Join(r.Form["email"], "")
log.Printf("Got email %s", email)
resp, err := http.PostForm(fmt.Sprintf("https://%s/api/users.admin.invite", slackAddress), url.Values{
"email": {email},
"token": {slackToken},
"set_active": {"true"},
})
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
log.Printf("Response body: %s", body)
data := map[string]string{}
t.ExecuteTemplate(w, "invite.html.tmpl", data)
})
log.Println("listening on", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}