Refactor + Tidy up code #48

Merged
fnetX merged 11 commits from :refactor-code into hackathon 4 months ago
  1. 2
      .woodpecker.yml
  2. 30
      main.go
  3. 27
      main_test.go

@ -5,4 +5,4 @@ pipeline:
- go get codeberg.org/codeberg/member-registration
- make check
- go install mvdan.cc/gofumpt@latest
- "[ $(gofumpt -extra -l . | wc -l) != 0 ] && { echo 'code not formated'; exit 1; }"
- [ $(gofumpt -extra -l . | wc -l) = 0 ] || { echo 'code not formated'; exit 1 }

@ -40,7 +40,7 @@ func (serverCtx *serverContext) getClientIP() string {
}
func (serverCtx *serverContext) logLine(msg string) {
fmt.Println(fmt.Sprintf("%s: %s", serverCtx.req.RemoteAddr, msg))
fmt.Printf("%s: %s\n", serverCtx.req.RemoteAddr, msg)
}
func (serverCtx *serverContext) serverError(msg string) {
@ -136,17 +136,15 @@ func (val *validator) checkField(serverCtx *serverContext, datum string) error {
return errorMessage(serverCtx.locale, REQUIRED_FIELD)
}
if val.validator != nil {
err := val.validator(serverCtx.locale, datum)
if err != nil {
return err
}
return val.validator(serverCtx.locale, datum)
}
return nil
}
var emailPattern = regexp.MustCompile(`^\S+@\S+$`)
func checkEmail(locale gettext.Catalog, email string) error {
emailPat := regexp.MustCompile("^\\S+@\\S+$")
if !emailPat.MatchString(email) {
if !emailPattern.MatchString(email) {
return errorMessage(locale, BAD_EMAIL)
}
return nil
@ -176,9 +174,6 @@ func renderTemplate(serverCtx *serverContext, fname string, renderCtx renderingC
"Options": func(input ...string) []string {
return input
},
"Raw": func(input string) template.HTML {
return template.HTML(input)
},
})
templ, err := templ.ParseFiles("templates/" + fname)
if err != nil {
@ -205,7 +200,7 @@ func parseMoney(strAmount string) (float64, error) {
n := len(fields)
if n == 0 {
return 0, fmt.Errorf("Bad Money Amount")
return 0, fmt.Errorf("bad amount of money")
}
if n == 1 {
return strconv.ParseFloat(strAmount, 64)
@ -285,11 +280,10 @@ func startRegistration(serverCtx *serverContext, json []byte) error {
output, err := cmd.CombinedOutput()
if err != nil {
serverCtx.logLine("gpg command failed: " + err.Error() + " / " + string(output))
return err
}
sendMail(serverCtx, recipient, "User registration", string(output))
return err
return sendMail(serverCtx, recipient, "User registration", string(output))
}
func sendRegistrationEmail(serverCtx *serverContext, formData map[string]string) error {
@ -383,7 +377,7 @@ func sendMail(serverCtx *serverContext, to, subject, msg string) error {
return nil
}
func handlePost(serverCtx *serverContext, renderCtx renderingContext) {
func handlePost(serverCtx *serverContext, _ renderingContext) {
serverCtx.logLine("handlePost")
writer, req := serverCtx.writer, serverCtx.req
err := req.ParseForm()
@ -393,7 +387,7 @@ func handlePost(serverCtx *serverContext, renderCtx renderingContext) {
}
formData, errors := validate(serverCtx, req.PostForm)
if len(errors) != 0 {
renderCtx = make(renderingContext)
renderCtx := make(renderingContext)
fnetX marked this conversation as resolved
Review

why is it recreated anyway?

why is it recreated anyway?
Review

No idea, I assume to remove existing context that is passed along, (not fill it back in?) to just render the error.

No idea, I assume to remove existing context that is passed along, (not fill it back in?) to just render the error.
renderCtx["errors"] = errors
renderForm(serverCtx, renderCtx)
@ -432,7 +426,7 @@ func dispatcher(writer http.ResponseWriter, req *http.Request) {
serverCtx := newServerContext(writer, req)
defer func() {
if r := recover(); r != nil {
serverCtx.serverError("Unhandled exception")
serverCtx.serverError(fmt.Sprintf("Unhandled exception: %v", r))
return
}
}()
@ -451,5 +445,5 @@ func main() {
fmt.Println("Listening on port 5000")
http.HandleFunc("/", dispatcher)
http.ListenAndServe(":5000", nil)
fmt.Printf("http.ListenAndServe: %v\n", http.ListenAndServe(":5000", nil))
}

@ -136,9 +136,7 @@ func (mockResponseWriter) Write([]byte) (int, error) {
return 1, nil
}
func (mockResponseWriter) WriteHeader(int) {
return
}
func (mockResponseWriter) WriteHeader(int) {}
func Test_validate(t *testing.T) {
catalog := localeDomain.Locale([]string{"en"}...)
@ -258,3 +256,26 @@ func Test_validate(t *testing.T) {
})
}
}
func Test_parseEmail(t *testing.T) {
tests := []struct {
input string
ok bool
}{
{input: "example@gmail.com", ok: true},
{input: "example+firstname+lastname@email.com", ok: true},
{input: "example@234.234.234.234", ok: true},
{input: "A special string!", ok: false},
}
for _, tt := range tests {
locale := localeDomain.Locale("en")
t.Run(tt.input, func(t *testing.T) {
err := checkEmail(locale, tt.input)
if tt.ok && err != nil {
t.Errorf("input is bad, expected to be good.")
} else if !tt.ok && err == nil {
t.Errorf("input is good, expected to be bad.")
}
})
}
}

Loading…
Cancel
Save