-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
125 lines (108 loc) · 2.89 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
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httputil"
"os"
)
var postURL = "https://faxcaster.fly.dev/"
const (
ButtonActionPost = "post"
ButtonActionPostRedirect = "post_redirect"
ButtonActionLink = "link"
ButtonActionMint = "mint"
ButtonActionTx = "tx"
)
type Button struct {
Label string
Action string
Target string
PostURL string
}
type Frame struct {
FormatText string
Buttons []Button
Input string
}
var frames = []Frame{
// p0: landing page
{
FormatText: "Faxcaster!\n\nClick the button below to get started.",
Buttons: []Button{
{
Label: "Let us fax!",
Action: ButtonActionPost,
},
},
},
}
func main() {
var bind string
// for Fly.io
port := os.Getenv("PORT")
if port == "" {
bind = "localhost:9080"
postURL = "http://localhost:9080/"
} else {
bind = ":" + port
}
// landing page handler
http.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
// generate the HTML for the landing page
htmlContent, err := generateFrameHTML(postURL, frames[0])
if err != nil {
log.Println("failed to generate HTML:", err)
http.Error(w, "Internal error", http.StatusInternalServerError)
return
}
// header(s) and output
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, htmlContent)
})
// POST handler
http.HandleFunc("POST /", func(w http.ResponseWriter, r *http.Request) {
// dump out the HTTP request, including headers
dump, err := httputil.DumpRequest(r, true)
if err != nil {
http.Error(w, "Internal error", http.StatusInternalServerError)
return
}
log.Println("Request received:\n" + string(dump))
// parse body into DataRepresentation
var data DataRepresentation
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
log.Println("failed to decode JSON:", err)
http.Error(w, "Internal error", http.StatusInternalServerError)
return
}
// decode message bytes
message, err := decodeMessageBytes(data.TrustedData.MessageBytes)
if err != nil {
log.Println("failed to decode message bytes:", err)
http.Error(w, "Internal error", http.StatusInternalServerError)
return
}
// marshal to JSON and log the message
messageJSON, err := json.MarshalIndent(message, "", " ")
if err != nil {
log.Println("failed to marshal message to JSON:", err)
http.Error(w, "Internal error", http.StatusInternalServerError)
return
}
log.Printf("Message: %s", string(messageJSON))
// generate the HTML for the required page
htmlContent, err := generateFrameHTML(postURL, frames[0])
if err != nil {
log.Println("failed to generate HTML:", err)
http.Error(w, "Internal error", http.StatusInternalServerError)
return
}
// header(s) and output
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, htmlContent)
})
log.Println("listening on", bind)
log.Fatal(http.ListenAndServe(bind, nil))
}