示例#1
0
func main() {
	if !flag.Parsed() {
		flag.Parse()
	}

	router := mux.NewRouter()
	router.StrictSlash(true)
	for _, route := range routes {
		handler := http.Handler(http.HandlerFunc(route.HandlerFunc))
		switch route.Type {
		case "JSON":
			handler = handlers.ContentTypeHandler(handler, "application/json")
		case "":
			break
		default:
			log.Fatalf("invalid route type: %v", route.Type)
		}

		r := router.NewRoute()
		r.Name(route.Name).
			Path(route.Path).
			Methods(route.Methods).
			Handler(handler)
	}

	address := fmt.Sprintf(":%d", *port)
	handler := handlers.CombinedLoggingHandler(os.Stderr, router)
	log.Printf("Version: %s", version.DeploymentManagerVersion)
	log.Printf("Listening on port %d...", *port)
	log.Fatal(http.ListenAndServe(address, handler))
}
示例#2
0
func main() {
	db := sqlx.MustConnect("sqlite3", ":memory:")

	if err := createFooTable(db); err != nil {
		log.Fatal("couldn't create table: ", err)
	}

	for i := 0; i < 10; i++ {
		id, err := insertFoo(db, "hello world "+strconv.Itoa(i))
		if err != nil {
			log.Fatal("failed to insert value: ", err)
		}
		log.Print("inserted foo record ", id)
	}

	h := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fooListEndpoint(w, r, &FooStore{db: db})
	}))
	h = handlers.LoggingHandler(os.Stdout, h)
	h = handlers.ContentTypeHandler(h, "application/json")

	http.Handle("/foo/", h)

	flag.Parse()
	log.Print("starting http server on ", *addr)
	if err := http.ListenAndServe(*addr, nil); err != nil {
		log.Printf("http server failed: ", err)
	}
}