-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpGo.go
110 lines (95 loc) · 2.43 KB
/
httpGo.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
package main
import (
"bytes"
"fmt"
"html/template"
"log"
"net/http"
"net/smtp"
"os"
"os/signal"
"syscall"
)
var auth smtp.Auth
func setupResponse(w *http.ResponseWriter, req *http.Request) {
(*w).Header().Set("Access-Control-Allow-Origin", "*")
(*w).Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
(*w).Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
}
func main() {
//Logging
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
pathToLogFile := dir + "/app.log"
f, err := os.OpenFile(pathToLogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
defer f.Close()
log.SetOutput(f)
log.Println("App Started..")
fs := http.FileServer(http.Dir("../book"))
http.Handle("/", http.StripPrefix("/", fs))
//HTTP server endpoints
http.HandleFunc("/api", sendFreeBookViaEmail)
http.HandleFunc("/api/payment/done", wayForPayHandler)
http.HandleFunc("/success", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "../book/success.html")
//fmt.Fprint(w, "Success Page")
})
http.HandleFunc("/order-book", sendPhysicalCopyOfBook)
port := ":5446"
reloadable()
fmt.Println("Server is listening... on port" + port)
start := http.ListenAndServe(port, nil)
log.Fatal(start)
}
func reloadable() {
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGHUP)
go func() {
for {
<-s
fmt.Println("Reloaded")
}
}()
}
//Request struct
type Request struct {
from string
to []string
subject string
body string
}
func NewRequest(to []string, subject, body string) *Request {
return &Request{
to: to,
subject: subject,
body: body,
}
}
func (r *Request) SendEmail() (bool, error) {
mime := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n"
subject := "Subject: " + r.subject + "!\n"
msg := []byte(subject + mime + "\n" + r.body)
addr := "smtp.gmail.com:587"
if err := smtp.SendMail(addr, auth, "[email protected]", r.to, msg); err != nil {
fmt.Println(err)
return false, err
}
return true, nil
}
func (r *Request) ParseTemplate(templateFileName string, data interface{}) error {
t, err := template.ParseFiles(templateFileName)
if err != nil {
return err
}
buf := new(bytes.Buffer)
if err = t.Execute(buf, data); err != nil {
return err
}
r.body = buf.String()
return nil
}