-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
41 lines (31 loc) · 816 Bytes
/
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
package main
import (
"fmt"
"log"
"net/http"
"regexp"
"strconv"
"time"
)
func main() {
log.Printf("Starting on port 8080")
http.HandleFunc("/register", handleRegister)
http.Handle("/", http.FileServer(http.Dir("./static")))
log.Fatal(http.ListenAndServe(":8080", nil))
}
func handleRegister(w http.ResponseWriter, r *http.Request) {
name := r.FormValue("name")
yearStr := r.FormValue("year")
reg := regexp.MustCompile(`^\d*$`)
if !reg.MatchString(yearStr) {
http.Error(w, "Year is invalid", http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
year, err := strconv.Atoi(yearStr)
if err != nil {
http.Error(w, "Year is invalid", http.StatusBadRequest)
return
}
w.Write([]byte(fmt.Sprintf("%s %d years old", name, time.Now().Year()-year)))
}