Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "context"
  5. "fmt"
  6. "html/template"
  7. "net/http"
  8. "simplystdatastore/config"
  9. "strconv"
  10.  
  11. "github.com/gorilla/mux"
  12.  
  13. "cloud.google.com/go/datastore"
  14. )
  15.  
  16. type user struct {
  17. ID int64
  18. UserName string
  19. Password string
  20. First string
  21. Last string
  22. }
  23.  
  24. var tpl *template.Template
  25.  
  26. func init() {
  27.  
  28. tpl = template.Must(template.ParseGlob("templates/*"))
  29.  
  30. }
  31.  
  32. func main() {
  33.  
  34. http.HandleFunc("/", index)
  35. http.HandleFunc("/signup", signup)
  36. http.HandleFunc("/edit", signup)
  37. http.Handle("/favicon.ico", http.NotFoundHandler())
  38. http.ListenAndServe(":8080", nil)
  39. }
  40.  
  41. func index(w http.ResponseWriter, req *http.Request) {
  42. var users []*user
  43.  
  44. ctx := context.Background()
  45. // Create a query to fetch all Task entities, ordered by "created".
  46. query := datastore.NewQuery("user")
  47. _, err := config.Userkind.GetAll(ctx, query, &users)
  48. if err != nil {
  49. fmt.Println(err)
  50. }
  51. fmt.Println(query)
  52. fmt.Println(&users)
  53. //fmt.Println(keys)
  54. tpl.ExecuteTemplate(w, "index.html", users)
  55.  
  56. }
  57.  
  58. func edit(w http.ResponseWriter, req *http.Request) {
  59. var user *user
  60. ctx := context.Background()
  61. id, err := strconv.ParseInt(mux.Vars(req)["id"], 10, 64)
  62.  
  63. if err != nil {
  64. fmt.Println(err)
  65. }
  66. k := datastore.IDKey("user", id, nil)
  67.  
  68. if _, err := config.Userkind.Put(ctx, k, user); err != nil {
  69. fmt.Println(err)
  70. }
  71.  
  72. tpl.ExecuteTemplate(w, "edit.html", user)
  73.  
  74. }
  75.  
  76. func userFromForm(r *http.Request) (*user, error) {
  77. user := &user{
  78. UserName: r.FormValue("username"),
  79. Password: r.FormValue("password"),
  80. First: r.FormValue("firstname"),
  81. Last: r.FormValue("lastname"),
  82. }
  83. return user, nil
  84.  
  85. }
  86.  
  87. func signup(w http.ResponseWriter, req *http.Request) {
  88.  
  89. // process form submission
  90. if req.Method == http.MethodPost {
  91. user, err := userFromForm(req)
  92.  
  93. if err != nil {
  94. fmt.Println(err)
  95. }
  96.  
  97. ctx := context.Background()
  98. k := datastore.IncompleteKey("user", nil)
  99. k, err = config.Userkind.Put(ctx, k, user)
  100. if err != nil {
  101. fmt.Println(err)
  102. }
  103.  
  104. //err = config.Userscollection.Insert(user)
  105.  
  106. // redirect
  107. http.Redirect(w, req, "/", http.StatusSeeOther)
  108. return
  109. }
  110.  
  111. tpl.ExecuteTemplate(w, "signup.html", nil)
  112. }
  113.  
  114. package config
  115.  
  116. import (
  117. "context"
  118. "fmt"
  119.  
  120. "cloud.google.com/go/datastore"
  121. )
  122.  
  123. //Userkind datastore table
  124. var Userkind *datastore.Client
  125.  
  126. func init() {
  127.  
  128. projectID := "simplyst-test"
  129.  
  130. ctx := context.Background()
  131. client, err := datastore.NewClient(ctx, projectID)
  132. if err != nil {
  133. fmt.Println("Error from init method client call ===>", err)
  134.  
  135. }
  136. t, err := client.NewTransaction(ctx)
  137. if err != nil {
  138. fmt.Println("Error from init method NewTransaction call ===>", err)
  139.  
  140. }
  141. if err := t.Rollback(); err != nil {
  142. fmt.Println("datastoredb: could not connect", err)
  143. }
  144.  
  145. Userkind = client
  146. fmt.Println("You connected to your google datastore.")
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement