Advertisement
Guest User

Untitled

a guest
Feb 14th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.87 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "encoding/json"
  5.     "fmt"
  6.     "gnd.la/crypto/password"
  7.     "io/ioutil"
  8.     "net/http"
  9. )
  10.  
  11. type User struct {
  12.     Id       string
  13.     Username string
  14.     Password password.Password
  15.     Active   bool
  16. }
  17.  
  18. func registerHandler(w http.ResponseWriter, r *http.Request) {
  19.  
  20.     user := User{
  21.         Id:       "testid",
  22.         Username: r.FormValue("username"),
  23.         Password: password.New(r.FormValue("password")),
  24.         Active:   true,
  25.     }
  26.  
  27.     b, _ := json.Marshal(user)
  28.     s := string(b)
  29.     fmt.Println(s)
  30.  
  31.     output, err := json.MarshalIndent(&user, "", "\t\t")
  32.     if err != nil {
  33.         fmt.Println("Error marshalling to JSON:", err)
  34.         return
  35.     }
  36.     err = ioutil.WriteFile(user.Id + ".json", output, 0644)
  37.     if err != nil {
  38.         fmt.Println("Error writing JSON to file:", err)
  39.         return
  40.     }
  41.  
  42. }
  43.  
  44. func main() {
  45.     http.HandleFunc("/register", registerHandler)
  46.     http.ListenAndServe(":8080", nil)
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement