This repository has been archived by the owner on Jan 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registration.go
137 lines (114 loc) · 3.13 KB
/
registration.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
package main
import (
"html/template"
"log"
"net/http"
"os"
"regexp"
"time"
"github.com/golanghr/goer/lib/content"
)
const (
registrationSuccessful = "You succesfuly registred."
)
// RegistrationStorer for separation of domain in storing data of registration
type RegistrationStorer interface {
Store(reg Registration) (err error)
}
// Registration data structure
type Registration struct {
Name string
Surname string
Email string
Created time.Time
Storer RegistrationStorer
InfoFile string
}
// ServeHTTP bind to http
// Will display form on GET and expects form process on POST
func (reg *Registration) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
reg.displayForm(w, r)
case "POST":
reg.processForm(w, r)
default:
http.Error(w, "Method not allowed.", http.StatusMethodNotAllowed)
}
}
func (reg *Registration) displayForm(w http.ResponseWriter, r *http.Request) {
p := make(map[string]interface{})
if len(reg.InfoFile) > 0 {
infoTextFile, err := os.Open(reg.InfoFile)
if err != nil {
log.Fatal(err)
}
defer infoTextFile.Close()
info, err := content.HTML(infoTextFile)
if err != nil {
log.Printf("Error in info text generation %s\n", err)
http.Error(w, "Error in rendering.", http.StatusInternalServerError)
return
}
p["info"] = template.HTML(info)
}
tpls["registration"].Execute(w, p)
}
func (reg *Registration) processForm(w http.ResponseWriter, r *http.Request) {
name := r.FormValue("name")
surname := r.FormValue("surname")
email := r.FormValue("email")
if len(name) == 0 ||
len(surname) == 0 ||
len(email) == 0 ||
!validateEmail(email) {
http.Error(w, "Form submition not valid.", http.StatusBadRequest)
}
reg.Name = name
reg.Surname = surname
reg.Email = email
reg.Created = time.Now()
err := reg.Storer.Store(*reg)
if err != nil {
log.Printf("Error %s while storing registration %+v\n", err, reg)
http.Error(w, "Registration storage failed.", http.StatusInternalServerError)
return
}
p := make(map[string]interface{})
p["success_msg"] = registrationSuccessful
if len(reg.InfoFile) > 0 {
infoTextFile, err := os.Open(reg.InfoFile)
if err != nil {
log.Fatal(err)
}
defer infoTextFile.Close()
info, err := content.HTML(infoTextFile)
if err != nil {
log.Printf("Error in info text generation %s\n", err)
http.Error(w, "Error in rendering.", http.StatusInternalServerError)
return
}
p["info"] = template.HTML(info)
}
tpls["registration"].Execute(w, p)
}
// RegTxtStorage will save registrations in form of basic TXT file
type RegTxtStorage struct {
Filename string
}
// Store implements RegistrationStorer interface
func (r *RegTxtStorage) Store(reg Registration) (err error) {
f, err := os.OpenFile(r.Filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
return err
}
defer f.Close()
if _, err = f.WriteString(reg.Created.String() + " | " + reg.Name + " | " + reg.Surname + " | " + reg.Email + "\n"); err != nil {
return err
}
return nil
}
func validateEmail(email string) bool {
Re := regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}$`)
return Re.MatchString(email)
}