Beispiel #1
0
	"fmt"
	"net/http"

	"github.com/golang/glog"
	"github.com/gorilla/mux"
	"github.com/wujiang/chatable"
	"github.com/wujiang/chatable/datastore"
	"github.com/wujiang/chatable/rds"
	"github.com/wujiang/chatable/router"

	goerrors "github.com/go-errors/errors"
)

var (
	store   = datastore.NewDataStore(nil)
	rdsPool = rds.NewRdsPool(nil)
)

func Handler() *mux.Router {
	m := router.API()

	m.Get(router.Register).Handler(handler(serveRegister))
	m.Get(router.CreateAuthToken).Handler(handler(serveCreateAuthToken))
	m.Get(router.DeactivateAuthToken).
		Handler(Authenticate(handler(serveDeactivateAuthToken)))
	m.Get(router.GetInbox).Handler(Authenticate(handler(serveGetThreads)))
	m.Get(router.GetThread).Handler(Authenticate(handler(serveGetEnvelopes)))

	m.Get(router.WSConnect).Handler(Authenticate(handler(serveWSConnect)))

	return m
Beispiel #2
0
func main() {
	// command usage
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, `
chatable

Usage:
        chatable [options] command [arg...]

Commands
`)
		for _, cmd := range subcmds {
			fmt.Fprintf(os.Stderr, "\t%s - %s\n", cmd.name, cmd.desc)
		}
		fmt.Fprintf(os.Stderr, `
Use "chatable command -h" for command help.

Options:
`)
		flag.PrintDefaults()
		os.Exit(1)
	}
	flag.Parse()
	if flag.NArg() == 0 {
		flag.Usage()
	}

	// parse the configuration file
	err := cfg.parse(*configFile)
	if err != nil {
		glog.Fatal(err)
	}

	// initialize database connection
	datastore.Init(cfg.Postgres)
	defer datastore.Exit()

	// initialize redis
	rds.Init(cfg.RedisHost)

	rdsPool := rds.NewRdsPool(nil)
	rdsPool.AddToQM(cfg.QueueManagerKey, cfg.MessageQueueKey)
	defer rdsPool.RemoveFromQM(cfg.QueueManagerKey, cfg.MessageQueueKey)

	go api.Hub.Run(cfg.SharedQueueKey)

	qm := api.QueueManager{}
	go qm.Dispatch(cfg.SharedQueueKey, cfg.QueueManagerKey)
	go qm.Pop(cfg.MessageQueueKey)

	subcmd := flag.Arg(0)
	for _, c := range subcmds {
		if c.name == subcmd {
			c.exec(flag.Args()[1:])
			return
		}
	}

	fmt.Fprintf(os.Stderr, "Unknown command %q\n", subcmd)
	flag.Usage()
	os.Exit(1)
}