-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
128 lines (110 loc) · 3.36 KB
/
handlers.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
package contactqr
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"path/filepath"
"github.com/aphexddb/contactqr/vcard"
)
// VCardRequest represents a request for a new vCard
type VCardRequest struct {
First string `json:"first"`
Last string `json:"last"`
CompanyName string `json:"company_name"`
Title string `json:"title"`
Email string `json:"email"`
CellPhone string `json:"cell_phone"`
Street string `json:"street"`
City string `json:"city"`
State string `json:"state"`
PostalCode string `json:"postal_code"`
FacebookURL string `json:"facebook_url"`
TwitterHandle string `json:"twitter_handle"`
URL string `json:"url"`
Note string `json:"note"`
}
// VCardResponse represents the response for creating a new vCard
type VCardResponse struct {
Success bool `json:"success"`
Errors string `json:"errors"`
VCardText string `json:"vcard_text"`
PngBase64 string `json:"png_base64"`
}
// writeVCardResponse writes a response to vcard actions
func writeVCardResponse(w http.ResponseWriter, text, errors, pngBase64 string) {
resp := VCardResponse{
Success: true,
Errors: errors,
VCardText: text,
PngBase64: pngBase64,
}
if len(errors) > 0 {
w.WriteHeader(http.StatusBadRequest)
resp.Success = false
resp.VCardText = ""
}
respBytes, _ := json.Marshal(resp)
w.Write(respBytes)
}
// CreateVCardHandler creates a new VCard
func CreateVCardHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var in VCardRequest
body, _ := ioutil.ReadAll(r.Body)
jsonErr := json.Unmarshal(body, &in)
// validate request
if jsonErr != nil {
log.Println("Error creating new vCard:", jsonErr.Error())
writeVCardResponse(w, "", "Invalid request", "")
return
}
vc, vcErr := vcard.New(
vcard.First(in.First),
vcard.Last(in.Last),
vcard.Org(in.CompanyName),
vcard.Title(in.Title),
vcard.Email(in.Email),
vcard.CellPhone(in.CellPhone),
vcard.HomeAddress(vcard.Address{
Street: in.Street,
City: in.City,
State: in.State,
PostalCode: in.PostalCode,
}),
vcard.FacebookProfileURL(in.FacebookURL),
vcard.TwitterHandle(in.TwitterHandle),
vcard.URL(in.URL),
vcard.Note(in.Note),
)
if vcErr != nil {
writeVCardResponse(w, "", vcErr.Error(), "")
return
}
log.Printf("Creating vCard for %s %s\n", vc.First(), vc.Last())
w.WriteHeader(http.StatusOK)
writeVCardResponse(w, vc.String(), "", vc.QRCode(200, 200))
}
// StaticHTMLHandler handles static html file requests
func StaticHTMLHandler(filePath, indexFile string) func(w http.ResponseWriter, r *http.Request) {
fn := func(w http.ResponseWriter, r *http.Request) {
// service index.html
if r.RequestURI == "/" || r.URL.Path[1:] == "/" {
http.ServeFile(w, r, filepath.Join(filePath, indexFile))
return
}
http.ServeFile(w, r, filepath.Join(filePath, r.URL.Path[1:]))
}
return http.HandlerFunc(fn)
}
// HealthCheckHandler provides a simple health check
func HealthCheckHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"healthy": true}`))
}
// NotFoundHandler handles 404 requests
func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(`{"error": "404 Not Found"}`))
}