Example #1
0
func init() {
	var err error
	if db, err = sql.Open("sqlite3", ":memory:"); err != nil {
		log.Fatalf("Cannot open db connection: %s", err)
	}

	if err = model.InitSqlite3(db); err != nil {
		log.Fatalf("Cannot init table scheme: %s", err)
	}

	redis := os.Getenv("REDIS")
	if redis == "" {
		log.Fatalf("Please set environment variable REDIS.")
	}

	cfg = map[string]string{
		"RedisAddr":  redis,
		"SessSecret": "1234567890",
		"SessName":   "mdpadtest",
	}

	_sf, err := common.BuildSession(cfg)
	if err != nil {
		log.Fatalf("Cannot init session factory: %s", err)
	}
	sf = _sf
}
Example #2
0
func main() {
	data, err := ioutil.ReadFile(jsonFile)
	if err != nil {
		log.Fatalf("Cannot read data from %s: %s", jsonFile, err)
	}

	cfg, err := common.JSONConfig(data)
	if err != nil {
		log.Fatalf("Cannot read configuration: %s", err)
	}

	db, err := cfg.DB()
	if err != nil {
		log.Fatalf("Cannot open db connection: %s", err)
	}

	if err := initDB(db, cfg["DBType"]); err != nil {
		log.Fatalf("Cannot initialize database: %s", err)
	}

	sf, err := common.BuildSession(cfg)
	if err != nil {
		log.Fatalf("Cannot prepare session store: %s", err)
	}

	if data, err = ioutil.ReadFile(cfg["GoogleKeyFile"]); err != nil {
		log.Fatalf("Cannot read data from %s: %s", cfg["GoogleKeyFile"], err)
	}

	googleConfig, err := google.ConfigFromJSON(data, "https://www.googleapis.com/auth/plus.login", "email")
	if err != nil {
		log.Fatalf("Cannot prepare google login info: %s", err)
	}

	whale := &controller.Whale{sf}
	jsonapi.HandleFunc("/api/whale", whale.Whale)

	ac := &controller.Auth{googleConfig, sf, cfg}
	http.HandleFunc("/auth/google", ac.Google)
	http.HandleFunc("/auth/google/oauth2callback", ac.GoogleCallback)
	http.HandleFunc("/api/paths", ac.Paths)
	http.HandleFunc("/api/logout", ac.Logout)

	uc := &controller.User{sf, cfg}
	jsonapi.HandleFunc("/api/me", uc.Me)
	jsonapi.HandleFunc("/api/user", uc.User)
	jsonapi.HandleFunc("/api/users", uc.Users)

	pc := &controller.Pad{db, sf, cfg}
	jsonapi.HandleFunc("/api/create", pc.Create)
	jsonapi.HandleFunc("/api/pad/", pc.View)
	jsonapi.HandleFunc("/api/pads", pc.List)
	jsonapi.HandleFunc("/api/delete/", pc.Delete)
	jsonapi.HandleFunc("/api/edit/", pc.Edit)

	sc := &controller.Static{Config: cfg}
	http.HandleFunc("/", sc.File)

	if err := http.ListenAndServe(cfg["Listen"], context.ClearHandler(http.DefaultServeMux)); err != nil {
		log.Fatalf("Cannot start http server at %s: %s", cfg["Listen"], err)
	}
}