Advertisement
Guest User

Untitled

a guest
Oct 15th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. main(){
  2. <OTHER CODE>
  3. log.Print("Instatiate Cookiestore")
  4. config.Configure_Sessions()
  5. log.Print("Instantiated")
  6. <MORE CODE>
  7.  
  8. router := NewRouter()
  9. os.Setenv("ORIGIN_ALLOWED", "*")
  10. headersOk := handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type"})
  11. originsOk := handlers.AllowedOrigins([]string{os.Getenv("ORIGIN_ALLOWED")})
  12. methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"})
  13.  
  14. // start server listen
  15. // with error handling
  16. log.Fatal(http.ListenAndServe(":8080", handlers.CORS(originsOk, headersOk, methodsOk)(router)))
  17. }
  18.  
  19. package config
  20.  
  21. import (
  22. "net/http"
  23.  
  24. "github.com/gorilla/sessions"
  25. )
  26.  
  27. var (
  28. // Store is the cookie store
  29. Store *sessions.CookieStore
  30. // Name is the session name
  31. Name string
  32. )
  33.  
  34. // Session stores session level information
  35. type Session struct {
  36. Options sessions.Options `json:"Options"` // Pulled from: http://www.gorillatoolkit.org/pkg/sessions#Options
  37. Name string `json:"Name"` // Name for: http://www.gorillatoolkit.org/pkg/sessions#CookieStore.Get
  38. SecretKey string `json:"SecretKey"` // Key for: http://www.gorillatoolkit.org/pkg/sessions#CookieStore.New
  39. }
  40.  
  41. // Configure the session cookie store
  42. func Configure_Sessions() {
  43. Store = sessions.NewCookieStore([]byte("adsads;fja;i4gna;nbeq09bjse"))
  44. // Store.Options = &s.Options
  45. // Name = s.Name
  46. }
  47.  
  48. func Instance(r *http.Request) (*sessions.Session, error) {
  49. session, err := Store.Get(r, "cookies4dndyo")
  50. return session, err
  51. }
  52.  
  53. // Empty deletes all the current session values
  54. func Empty(sess *sessions.Session) {
  55. // Clear out all stored values in the cookie
  56. for k := range sess.Values {
  57. delete(sess.Values, k)
  58. }
  59. }
  60.  
  61. func (incomingjson *User) Login(w http.ResponseWriter, r *http.Request) {
  62. log.Print("Inside Login Function")
  63. session, err := config.Instance(r)
  64. log.Print("After session assignment in login function")
  65. log.Print("What is the value of session, err?")
  66. log.Print("Session: ", session)
  67. log.Print("Err: ", err)
  68. log.Print("Set Value and Test")
  69. session.Values["testing"] = "hellotheresailor"
  70. log.Print("session.Values[testing]: ", session.Values["testing"])
  71. log.Print("Test with previous values: ")
  72. if session.Values["testingappend"] == nil {
  73. log.Print("inside testingappen nil if statement")
  74. session.Values["testingappend"] = "hohoho"
  75. log.Print(session.Values["testingappend"])
  76. } else {
  77. log.Print("inside testingappend not nil else statement")
  78. session.Values["testingappend"] = session.Values["testingappend"].(string) + session.Values["testing"].(string)
  79. log.Print("Value of session.Values[testingappend]: ", session.Values["testingappend"])
  80. }
  81. session.Save(r, w)
  82.  
  83. patientplatypus:~/Documents/golang/src/github.com/patientplatypus/gorest:20:00:18$gorest
  84. Successfully connected~!
  85. 2017/10/15 20:00:19 Instatiate Cookiestore
  86. 2017/10/15 20:00:19 Instantiated
  87. 2017/10/15 20:00:36 username: patientplatypus
  88. 2017/10/15 20:00:36 password: Fvnjty0b
  89. 2017/10/15 20:00:36 Inside Login Function
  90. 2017/10/15 20:00:36 After session assignment in login function
  91. 2017/10/15 20:00:36 What is the value of session, err?
  92. 2017/10/15 20:00:36 Session: &{ map[] 0xc42015e330 true 0xc4200e1040 cookies4dndyo}
  93. 2017/10/15 20:00:36 Err: <nil>
  94. 2017/10/15 20:00:36 Set Value and Test
  95. 2017/10/15 20:00:36 session.Values[testing]: hellotheresailor
  96. 2017/10/15 20:00:36 Test with previous values:
  97. 2017/10/15 20:00:36 inside testingappen nil if statement
  98. 2017/10/15 20:00:36 hohoho
  99. 2017/10/15 20:00:36 loginjson: &{patientplatypus Fvnjty0b 0}
  100. 2017/10/15 20:00:36 err: <nil>
  101. 2017/10/15 20:00:36 Found username
  102. 2017/10/15 20:00:36 username and password match!
  103. 2017/10/15 20:00:36 Value of session.Values[authenticated], session.values[username]: true patientplatypus
  104. 2017/10/15 20:00:59 username: theGreatDM
  105. 2017/10/15 20:00:59 password: theGreatDM
  106. 2017/10/15 20:00:59 Inside Login Function
  107. 2017/10/15 20:00:59 After session assignment in login function
  108. 2017/10/15 20:00:59 What is the value of session, err?
  109. 2017/10/15 20:00:59 Session: &{ map[] 0xc4201ba2a0 true 0xc4200e1040 cookies4dndyo}
  110. 2017/10/15 20:00:59 Err: <nil>
  111. 2017/10/15 20:00:59 Set Value and Test
  112. 2017/10/15 20:00:59 session.Values[testing]: hellotheresailor
  113. 2017/10/15 20:00:59 Test with previous values:
  114. 2017/10/15 20:00:59 inside testingappen nil if statement
  115. 2017/10/15 20:00:59 hohoho
  116. 2017/10/15 20:00:59 loginjson: &{theGreatDM theGreatDM 0}
  117. 2017/10/15 20:00:59 err: <nil>
  118. 2017/10/15 20:00:59 Found username
  119. 2017/10/15 20:00:59 username and password match!
  120. 2017/10/15 20:00:59 Value of session.Values[authenticated], session.values[username]: true theGreatDM
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement