Exemple #1
0
func init() {
	var err error

	files, _ := ioutil.ReadDir("data/foos")
	for _, file := range files {
		if !file.IsDir() {
			err = os.Remove("data/foos/" + file.Name())
			if err != nil {
				fmt.Println("Failed to remove file", err)
			}
		}
	}

	fieldsToIndex := make(map[string][]string)
	fieldsToIndex["foos"] = []string{"tags", "bar"}

	db, err = ivy.OpenDB("data", fieldsToIndex)
	if err != nil {
		fmt.Println("Failed to open database:", err)
		os.Exit(1)
	}
}
Exemple #2
0
func main() {
	var port string

	hostname, err := os.Hostname()
	if err != nil {
		fmt.Println("Error getting hostname:", err)
	}

	if hostname == "pythia" {
		port = ":80"
	} else {
		port = ":8080"
	}

	fieldsToIndex := make(map[string][]string)
	fieldsToIndex["answers"] = []string{"tags"}
	fieldsToIndex["users"] = []string{"login"}

	db, err := ivy.OpenDB("data", fieldsToIndex)
	if err != nil {
		fmt.Println("Database initialization failed:", err)
	}

	defer db.Close()

	store := sessions.NewCookieStore([]byte("pythia-is-awesome"))

	gv := global_vars.GlobalVars{MyDB: db, SessionStore: store}

	fs := http.FileServer(http.Dir("static"))
	http.Handle("/static/", http.StripPrefix("/static/", fs))

	r := mux.NewRouter()
	r.HandleFunc("/", makeHandler(answers_handler.Index, &gv)).Methods("GET")

	r.HandleFunc("/answers", makeHandler(answers_handler.Index, &gv)).Methods("GET")
	r.HandleFunc("/answers/search", makeHandler(answers_handler.Index, &gv)).Methods("POST")
	r.HandleFunc("/answers/{id:[0-9]+}", makeHandler(answers_handler.View, &gv)).Methods("GET")
	r.HandleFunc("/answers/new", makeHandler(answers_handler.New, &gv)).Methods("GET")
	r.HandleFunc("/answers/create", makeHandler(answers_handler.Create, &gv)).Methods("POST")
	r.HandleFunc("/answers/{id:[0-9]+}/edit", makeHandler(answers_handler.Edit, &gv)).Methods("GET")
	r.HandleFunc("/answers/update", makeHandler(answers_handler.Update, &gv)).Methods("POST")
	r.HandleFunc("/answers/{id:[0-9]+}/delete", makeHandler(answers_handler.Delete, &gv)).Methods("GET")
	r.HandleFunc("/answers/destroy", makeHandler(answers_handler.Destroy, &gv)).Methods("POST")

	r.HandleFunc("/users", makeHandler(users_handler.Index, &gv)).Methods("GET")
	r.HandleFunc("/users/{id:[0-9]+}", makeHandler(users_handler.View, &gv)).Methods("GET")
	r.HandleFunc("/users/new", makeHandler(users_handler.New, &gv)).Methods("GET")
	r.HandleFunc("/users/create", makeHandler(users_handler.Create, &gv)).Methods("POST")
	r.HandleFunc("/users/{id:[0-9]+}/edit", makeHandler(users_handler.Edit, &gv)).Methods("GET")
	r.HandleFunc("/users/update", makeHandler(users_handler.Update, &gv)).Methods("POST")
	r.HandleFunc("/users/{id:[0-9]+}/delete", makeHandler(users_handler.Delete, &gv)).Methods("GET")
	r.HandleFunc("/users/destroy", makeHandler(users_handler.Destroy, &gv)).Methods("POST")

	r.HandleFunc("/logins/new", makeHandler(logins_handler.New, &gv)).Methods("GET")
	r.HandleFunc("/logins/create", makeHandler(logins_handler.Create, &gv)).Methods("POST")
	r.HandleFunc("/logout", makeHandler(logins_handler.Logout, &gv)).Methods("GET")

	http.Handle("/", r)

	csrfHandler := nosurf.New(http.DefaultServeMux)

	csrfHandler.SetFailureHandler(http.HandlerFunc(failHand))

	http.ListenAndServe(port, csrfHandler)
}
Exemple #3
0
func main() {
	// Specify which tables Ivy should build indexes for.  Index fields must be of type string.
	fieldsToIndex := make(map[string][]string)
	fieldsToIndex["planes"] = []string{"tags", "name", "enginetype"}

	//
	// Open DB
	//
	db, err := ivy.OpenDB("data", fieldsToIndex)
	if err != nil {
		fmt.Println("Failed to open database:", err)
		os.Exit(1)
	}

	defer db.Close()

	plane := Plane{}

	//
	// Find
	//
	err = db.Find("planes", &plane, "1")
	if err != nil {
		fmt.Println("Find failed:", err)
	}

	fmt.Print("\n======================= Plane with id '1' =======================================================================\n\n")
	fmt.Printf("%#v\n", plane.Name)

	//
	// FindAllIdsForTags
	//
	ids, err := db.FindAllIdsForTags("planes", []string{"german"})
	if err != nil {
		fmt.Println("FindAllIdsForTags failed:", err)
	}

	fmt.Print("\n======================= Planes with tag 'german' ==================================================================\n\n")
	for _, id := range ids {
		plane = Plane{}

		err = db.Find("planes", &plane, id)
		if err != nil {
			fmt.Println("Find failed:", err)
		}

		fmt.Printf("%#v\n", plane.Name)
	}

	//
	// FindFirstIdForField
	//
	id, err := db.FindFirstIdForField("planes", "name", "P-51D")
	if err != nil {
		fmt.Println("FindFirstIdForField failed:", err)
	}

	fmt.Print("\n======================= Plane with name 'P-51D' ==================================================================\n\n")

	plane = Plane{}

	err = db.Find("planes", &plane, id)
	if err != nil {
		fmt.Println("Find failed:", err)
	}

	fmt.Printf("%#v\n", plane.Name)

	//
	// FindAllIdsForField
	//
	ids, err = db.FindAllIdsForField("planes", "enginetype", "radial")
	if err != nil {
		fmt.Println("FindAllIdsForField failed:", err)
	}

	fmt.Print("\n======================= Planes with enginetype 'radial' ===========================================================\n\n")
	for _, id := range ids {
		plane = Plane{}

		err = db.Find("planes", &plane, id)
		if err != nil {
			fmt.Println("Find failed:", err)
		}

		fmt.Printf("%#v\n", plane.Name)
	}

	//
	// Create
	//
	plane = Plane{Name: "Test", Speed: 111, Range: 111, EngineType: "radial", Tags: []string{"test"}}
	id, err = db.Create("planes", plane)
	if err != nil {
		fmt.Println("Create failed:", err)
	}

	err = db.Find("planes", &plane, id)
	if err != nil {
		fmt.Println("Find failed:", err)
	}

	fmt.Printf("\n======================= New Plane with id '%v' ================================================================\n", id)
	fmt.Printf("%#v\n", plane.Name)

	//
	// Update
	//
	plane.Speed = 999
	err = db.Update("planes", plane, id)
	if err != nil {
		fmt.Println("Update failed:", err)
	}

	plane = Plane{}

	err = db.Find("planes", &plane, id)
	if err != nil {
		fmt.Println("Find failed:", err)
	}

	fmt.Printf("\n======================= Plane with updated speed ==============================================================\n")
	fmt.Printf("%#v\n", plane.Speed)

	//
	// Delete
	//
	err = db.Delete("planes", id)
	if err != nil {
		fmt.Println("Delete failed:", err)
	}
}