Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "encoding/json"
- "fmt"
- "gnd.la/crypto/password"
- "io/ioutil"
- "net/http"
- )
- type User struct {
- Id string
- Username string
- Password password.Password
- Active bool
- }
- func registerHandler(w http.ResponseWriter, r *http.Request) {
- user := User{
- Id: "testid",
- Username: r.FormValue("username"),
- Password: password.New(r.FormValue("password")),
- Active: true,
- }
- b, _ := json.Marshal(user)
- s := string(b)
- fmt.Println(s)
- output, err := json.MarshalIndent(&user, "", "\t\t")
- if err != nil {
- fmt.Println("Error marshalling to JSON:", err)
- return
- }
- err = ioutil.WriteFile(user.Id + ".json", output, 0644)
- if err != nil {
- fmt.Println("Error writing JSON to file:", err)
- return
- }
- }
- func main() {
- http.HandleFunc("/register", registerHandler)
- http.ListenAndServe(":8080", nil)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement