Commit 11492c5a authored by tnias's avatar tnias

Initial commit

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.
<p>There is no transport security in this example, yet.</p>
<form action="/" method="post">
<td><input type="text" name="name"></td>
<td><input type="text" name="email"></td>
<td><input type="date" name="birthday"></td>
<td><input type="submit" value="Login"></td>
package main
import (
var registerTmp *template.Template
type Server struct {
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 {
} else {
io.WriteString(w, "processing data..\n")
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 {
registerTmp = t
// open user file
uf, err := os.OpenFile("users.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0755)
if err != nil {
// 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