Commit 11492c5a authored by tnias's avatar tnias

Initial commit

parents
A minimalistic register form without input validation. It just dumps all data
to an easily parsable file.
For security reasons `'` and `\n` characters in the user supplied data will be
removed before data gets writing to file.
<html>
<head>
<title>REGISTER</title>
</head>
<body>
<h2>REGISTER</h2>
<p>There is no transport security in this example, yet.</p>
<form action="/" method="post">
<table>
<tr>
<td>name</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>email</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td>birthday</td>
<td><input type="date" name="birthday"></td>
</tr>
<tr>
<td><input type="submit" value="Login"></td>
</tr>
</table>
</form>
</body>
</html>
package main
import (
"fmt"
"html/template"
"io"
"log"
"net/http"
"os"
"strings"
"sync"
)
var registerTmp *template.Template
type Server struct {
sync.Mutex
users *os.File
}
func cleanStr(in string) string {
in = strings.Replace(in, "'", "", -1)
in = strings.Replace(in, "\n", "", -1)
return in
}
func (s *Server) myform(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
err := registerTmp.Execute(w, nil)
if err != nil {
log.Fatal(err)
}
} else {
io.WriteString(w, "processing data..\n")
r.ParseForm()
s.Lock()
defer s.Unlock()
for k, v := range r.Form {
fmt.Fprintf(s.users, "'%v': '%v', ",
cleanStr(k), cleanStr(strings.Join(v, " ")))
}
fmt.Fprintf(s.users, "\n")
io.WriteString(w, "done.")
}
}
func main() {
// load template
t, err := template.ParseFiles("register.html")
if err != nil {
log.Fatal(err)
}
registerTmp = t
// open user file
uf, err := os.OpenFile("users.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0755)
if err != nil {
log.Fatal(err)
}
// register handlers
s := &Server{users: uf}
http.HandleFunc("/", s.myform)
// start
http.ListenAndServe(":8000", nil)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment