Example #1
0
func main() {
	log.SetFlags(log.Ltime | log.Lshortfile)
	//
	// Parse command line
	//
	listenUrl := flag.String("url", "localhost:3000", "Host/port on which to run websocket listener")
	mongoUrl := flag.String("mongo", "localhost", "URL of MongoDB server")
	flag.Parse()
	// Extract DB name from DB URL, if present
	dbName := "tokenizer" // If no DB name specified, use "tokenizer"
	switch _, auth, _, err := mgourl.ParseURL(*mongoUrl); true {
	case err != nil:
		log.Fatal("Could not parse MongoDB URL:", err)
	case auth.Db != "":
		dbName = auth.Db
	}
	//
	// Setup MongoDB connection
	//
	log.Println("Connecting to MongoDB on", *mongoUrl)
	session, err := mgo.Dial(*mongoUrl)
	if err != nil {
		log.Fatalln(err)
	}
	db := session.DB(dbName)
	//
	// Initialize Tokenizer
	//
	t := tokenizer.NewMongoTokenizer(db)
	//
	// Register URLs
	//
	mux := pat.New()
	mux.Get("/v1/rest/tokenize/:string", rest.TokenizeHandler(t))
	mux.Get("/v1/rest/detokenize/:token", rest.DetokenizeHandler(t))
	mux.Get("/v1/ws/tokenize", websocket.Handler(ws.Tokenize(t)))
	mux.Get("/v1/ws/detokenize", websocket.Handler(ws.Detokenize(t)))
	mux.Get("/", http.HandlerFunc(HomePageHandler))
	http.Handle("/", mux)
	//
	// Start HTTP server
	//
	log.Println("Starting HTTP server on", *listenUrl)
	err = http.ListenAndServe(*listenUrl, nil)
	if err != nil {
		log.Fatalln("ListenAndServe: " + err.Error())
	}
}
Example #2
0
func main() {
	log.SetFlags(log.Ltime | log.Lshortfile)
	//
	// Setup environment from Heroku
	//
	port := os.Getenv("PORT")
	if port == "" {
		port = "5000"
	}
	//
	//
	pathRoot := os.Getenv("PWD") // Root of the application install
	//
	mcServers := os.Getenv("MEMCACHIER_SERVERS")
	mcUser := os.Getenv("MEMCACHIER_USERNAME")
	mcPasswd := os.Getenv("MEMCACHIER_PASSWORD")
	//
	//
	mongoUri := os.Getenv("MONGOLAB_URI")
	dbName := "jfuexample" // If no DB name specified, use "jfuexample"
	if mongoUri != "" {
		switch _, auth, _, err := mgourl.ParseURL(mongoUri); true {
		case err != nil:
			log.Fatal("Could not parse MongoDB URL:", err)
		case auth.Db != "":
			dbName = auth.Db
		}
	} else {
		mongoUri = "localhost"
	}
	//
	// Initialize MongoDB connection
	//
	log.Println("Connecting to MongoDB on", mongoUri)
	conn, err := mgo.Dial(mongoUri)
	if err != nil {
		log.Fatalln(err)
	}
	db := conn.DB(dbName)
	gfs := db.GridFS("test_foobar")
	store := jfu.NewMongoStore(gfs)
	//
	// Initialize Memcache connection
	//
	if mcServers == "" {
		mcServers = "localhost:11211"
	}
	client, err := mc.Dial("tcp", mcServers)
	if err != nil {
		log.Panic(err)
	}
	if mcUser != "" && mcPasswd != "" {
		client.Auth(mcUser, mcPasswd)
		if err != nil {
			log.Panic(err)
		}
	}
	//
	// Initialize UploadHandler
	//
	conf := jfu.DefaultConfig
	conf.MaxFileSize = 100 << 10 // 100kb
	uh := jfu.UploadHandler{
		Prefix: "/jfu",
		Store:  &store,
		Conf:   &conf,
		Cache:  client,
	}
	//
	// Register Handlers
	//
	http.Handle("/jfu", &uh)
	http.Handle("/jfu/", &uh)
	log.Println("Serve JFU")
	path := http.Dir(filepath.Join(pathRoot, "static"))
	http.Handle("/", http.FileServer(path))
	log.Println("Serve files from ", path)
	//
	// Start the webserver
	//
	addr := "0.0.0.0:" + port
	log.Println("Starting webserver on", addr, "...")
	log.Fatal(http.ListenAndServe(addr, nil))
}