· 8 years ago · Oct 16, 2017, 01:28 AM
1main(){
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
19package config
20
21import (
22 "net/http"
23
24 "github.com/gorilla/sessions"
25)
26
27var (
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
35type 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
42func Configure_Sessions() {
43 Store = sessions.NewCookieStore([]byte("adsads;fja;i4gna;nbeq09bjse"))
44 // Store.Options = &s.Options
45 // Name = s.Name
46}
47
48func 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
54func 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
61func (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
83patientplatypus:~/Documents/golang/src/github.com/patientplatypus/gorest:20:00:18$gorest
84Successfully connected~!
852017/10/15 20:00:19 Instatiate Cookiestore
862017/10/15 20:00:19 Instantiated
872017/10/15 20:00:36 username: patientplatypus
882017/10/15 20:00:36 password: Fvnjty0b
892017/10/15 20:00:36 Inside Login Function
902017/10/15 20:00:36 After session assignment in login function
912017/10/15 20:00:36 What is the value of session, err?
922017/10/15 20:00:36 Session: &{ map[] 0xc42015e330 true 0xc4200e1040 cookies4dndyo}
932017/10/15 20:00:36 Err: <nil>
942017/10/15 20:00:36 Set Value and Test
952017/10/15 20:00:36 session.Values[testing]: hellotheresailor
962017/10/15 20:00:36 Test with previous values:
972017/10/15 20:00:36 inside testingappen nil if statement
982017/10/15 20:00:36 hohoho
992017/10/15 20:00:36 loginjson: &{patientplatypus Fvnjty0b 0}
1002017/10/15 20:00:36 err: <nil>
1012017/10/15 20:00:36 Found username
1022017/10/15 20:00:36 username and password match!
1032017/10/15 20:00:36 Value of session.Values[authenticated], session.values[username]: true patientplatypus
1042017/10/15 20:00:59 username: theGreatDM
1052017/10/15 20:00:59 password: theGreatDM
1062017/10/15 20:00:59 Inside Login Function
1072017/10/15 20:00:59 After session assignment in login function
1082017/10/15 20:00:59 What is the value of session, err?
1092017/10/15 20:00:59 Session: &{ map[] 0xc4201ba2a0 true 0xc4200e1040 cookies4dndyo}
1102017/10/15 20:00:59 Err: <nil>
1112017/10/15 20:00:59 Set Value and Test
1122017/10/15 20:00:59 session.Values[testing]: hellotheresailor
1132017/10/15 20:00:59 Test with previous values:
1142017/10/15 20:00:59 inside testingappen nil if statement
1152017/10/15 20:00:59 hohoho
1162017/10/15 20:00:59 loginjson: &{theGreatDM theGreatDM 0}
1172017/10/15 20:00:59 err: <nil>
1182017/10/15 20:00:59 Found username
1192017/10/15 20:00:59 username and password match!
1202017/10/15 20:00:59 Value of session.Values[authenticated], session.values[username]: true theGreatDM