func init() { f, err := os.Open(os.Getenv("GOPATH") + "/config/database.json") errors.CheckFatalErr(err) decoder := json.NewDecoder(f) dbConfig := db.DBConfig{} err = decoder.Decode(&dbConfig) errors.CheckFatalErr(err) f, err = os.Open(os.Getenv("GOPATH") + "/config/store.json") errors.CheckFatalErr(err) decoder = json.NewDecoder(f) storeConfig := StoreConfig{} err = decoder.Decode(&storeConfig) errors.CheckFatalErr(err) SessionName = storeConfig.Name Store, err = mysqlstore.NewMySQLStore( fmt.Sprint( dbConfig.User, ":", dbConfig.Password, "@/", dbConfig.Database, "?parseTime=true"), "session", "/", 3600, []byte(storeConfig.Key)) errors.CheckFatalErr(err) fmt.Println("Session store created.") }
func main() { var store, _ = mysqlstore.NewMySQLStore( "root:root@tcp(localhost:3306)/mygoji?parseTime=true&loc=Local", "mysession", "/", 3600, []byte("12345678")) defer store.Close() var sessTest = func(w http.ResponseWriter, r *http.Request) { session, err := store.Get(r, "foobar") session.Values["bar"] = "baz" session.Values["baz"] = "foo" err = session.Save(r, w) w.Header().Set("Content-Type", "text/plain") fmt.Fprintf(w, "%#v\n", session) fmt.Fprintln(w, err) } http.HandleFunc("/", sessTest) http.ListenAndServe(":8080", nil) }