Example #1
0
// Setup http routes.
func setupRoutes(config *Configuration) {
	r := pat.New()

	// Websocket requests.
	r.Get(config.WebsocketRoute+":room_id", stack.New(hasRoom, hasAuth).Then(webSocketHandler))

	// Homepage.
	r.Get("/", http.HandlerFunc(indexPage))

	// Other pages.
	r.Get("/pages/:page_id", http.HandlerFunc(staticPage))

	// Room related routes.
	r.Post(config.RoomRoute+"create", http.HandlerFunc(createRoom))
	r.Get(config.RoomRoute+":room_id", stack.New(hasRoom).Then(roomPage))
	r.Post(config.RoomRoute+":room_id/login", stack.New(hasRoom).Then(login))
	r.Post(config.RoomRoute+":room_id/dispose", stack.New(hasRoom, hasAuth).Then(disposeRoom))

	// Debug stats.
	r.Get("/debug", http.HandlerFunc(debugPage))

	http.Handle("/", r)
}
Example #2
0
func main() {
	var err error

	port := flag.String("port", "8080", "Listen port")
	dictHost := flag.String("dicthost", "localhost", "Dict server name")
	dictPort := flag.String("dictport", "2628", "Dict server port")
	gzip := flag.Bool("gzip", false, "Support gzip compression")
	deflate := flag.Bool("deflate", false, "Support DEFLATE compression")

	flag.Parse()

	dictServer = *dictHost + ":" + *dictPort

	client, err := getDictClient()
	if err != nil {
		os.Exit(1)
	}

	// Get the global list of databases
	dicts, err := getDictionaries(client)
	if err != nil {
		log.Println("Unable to get database list")
		os.Exit(1)
	}

	for _, d := range dicts {
		log.Printf("Available dictionary %s: %s", d.Name, d.Desc)
	}
	// No need for it until a request comes in
	client.Close()

	// Define our routes
	router := httprouter.New()
	router.GET("/define/:word", dictDefine)
	router.GET("/databases", dictDatabases)
	router.GET("/db", dictDatabases)

	// Define our middlewares

	// Going to need some CORS headers
	cors := cors.New(cors.Options{
		AllowedHeaders: []string{
			"Accept", "Content-Type", "Origin",
		},
		AllowedMethods: []string{
			"GET", "OPTIONS",
		},
	})

	stdChain := stack.New(stack.Adapt(cors.Handler), stack.Adapt(Logger))
	if *gzip {
		stdChain = stdChain.Append(Gzip)
		log.Println("Adding support for Gzip compression")
	}

	if *deflate {
		stdChain = stdChain.Append(Deflate)
		log.Println("Adding support for DEFLATE compression")
	}

	chain := stdChain.ThenHandler(router)

	listen := ":" + *port

	srv := &graceful.Server{
		Timeout: 5 * time.Second,
		Server:  &http.Server{Addr: listen, Handler: chain},
	}

	log.Printf("Listening at %s", listen)
	srv.ListenAndServe()
}