Ejemplo n.º 1
0
func main() {
	viper.SetDefault("addr", "127.0.0.1")
	viper.SetDefault("httpListen", ":3000")
	viper.SetConfigName("config")
	viper.AddConfigPath(".")
	err := viper.ReadInConfig()
	if err != nil {
		fmt.Printf("Fatal error config file: %s \n,now using default value", err)
	}
	addr := viper.GetString("addr")
	httpListen := viper.GetString("httpListen")
	m := staticbin.Classic(handler.Asset)
	zc := zk.New(addr)
	m.Map(zc)
	m.Use(render.Renderer(render.Options{
		Delims: render.Delims{"{[{", "}]}"}, // Sets delimiters to the specified strings.
	}))
	m.Get("/", handler.Tree)
	m.Get("/lookup", handler.LookUp)
	m.Get("/nodes", handler.Childrens)
	m.Get("/node", handler.NodeData)
	m.Get("/state", handler.State)
	m.Post("/create", handler.Create)
	m.Post("/edit", handler.Edit)
	m.Post("/rmr", handler.Rmr)
	m.Post("/delete", handler.Delete)
	m.Get("/export", handler.Export)
	m.Post("/import", handler.Import)
	m.Get("/find", handler.FindByPath)
	m.RunOnAddr(httpListen)
}
Ejemplo n.º 2
0
func runWebUI(addr string, perfData []perflog.PerfLogEntry) {
	m := staticbin.Classic(gopnikwebstatic.Asset)

	m.Map(log.New(os.Stderr, "", log.LstdFlags))

	m.Use(rendergold.Renderer(rendergold.Options{Asset: Asset}))

	m.Get("/", func(r rendergold.Render) {

		totalStats := getStats(perfData, nil)
		var zoomsStats []zoomStats
		zrng := getZooms(perfData)
		for z := zrng.MinZoom; z <= zrng.MaxZoom; z++ {
			zoomsStats = append(zoomsStats, zoomStats{
				Zoom:  z,
				Stats: getStats(perfData, &z),
			})
		}

		r.HTML(
			http.StatusOK,
			"index",
			map[string]interface{}{
				"Page":       "Results",
				"TotalStats": totalStats,
				"ZoomsStats": zoomsStats,
			},
		)
	})

	m.Get("/heatmap", func(r rendergold.Render) {
		r.HTML(
			http.StatusOK,
			"heatmap",
			map[string]interface{}{
				"Page": "Heat",
			},
		)
	})

	m.Get("/heattiles_zooms", func(res http.ResponseWriter) {
		var zooms struct {
			Min uint64
			Max uint64
		}

		if len(perfData) > 0 {
			zooms.Min = perfData[0].Coord.Zoom
			zooms.Max = perfData[0].Coord.Zoom

			for i := 1; i < len(perfData); i++ {
				zoom := perfData[i].Coord.Zoom
				if zoom < zooms.Min {
					zooms.Min = zoom
				}
				if zoom > zooms.Max {
					zooms.Max = zoom
				}
			}
		}

		enc := json.NewEncoder(res)
		if err := enc.Encode(zooms); err != nil {
			http.Error(res, err.Error(), 500)
			return
		}
	})

	m.Get("/heattiles/:zoom_orig/:zoom/:x/:y.png", func(params martini.Params, res http.ResponseWriter) {
		var coord gopnik.TileCoord

		coord.Size = 1
		_, err := fmt.Sscan(params["zoom"], &coord.Zoom)
		if err != nil {
			http.Error(res, err.Error(), 400)
			return
		}
		_, err = fmt.Sscan(params["x"], &coord.X)
		if err != nil {
			http.Error(res, err.Error(), 400)
			return
		}
		_, err = fmt.Sscan(params["y"], &coord.Y)
		if err != nil {
			http.Error(res, err.Error(), 400)
			return
		}
		var zoomOrig uint64
		_, err = fmt.Sscan(params["zoom_orig"], &zoomOrig)
		if err != nil {
			http.Error(res, err.Error(), 400)
			return
		}

		tile, err := genPerfTile(perfData, coord, zoomOrig)
		if err != nil {
			http.Error(res, err.Error(), 500)
			return
		}

		res.Header().Set("Content-Type", "image/png")
		res.Write(tile)
	})

	log.Printf("Starting WebUI on %v", addr)
	log.Fatal(http.ListenAndServe(addr, m))
}
Ejemplo n.º 3
0
func runWebUI(addr string, p *coordinator, cache gopnik.CachePluginInterface) {
	m := staticbin.Classic(gopnikwebstatic.Asset)

	var logger webUILogger
	m.Map(stdlog.New(&logger, "[martini] ", stdlog.LstdFlags))

	m.Use(rendergold.Renderer(rendergold.Options{Asset: Asset}))

	m.Get("/", func(r rendergold.Render) {
		done, total := p.DoneTasks()
		progress := float64(done) / float64(total) * 100.0

		r.HTML(
			http.StatusOK,
			"status",
			map[string]interface{}{
				"Page":     "Status",
				"Total":    total,
				"Done":     done,
				"Progress": fmt.Sprintf("%.02f", progress),
			},
		)
	})

	m.Get("/nodes", func(r rendergold.Render) {
		type node struct {
			Addr   string
			Status bool
		}
		addrs := p.Nodes()
		nodes := make([]node, len(addrs))
		for i, addr := range addrs {
			nodes[i].Addr = addr
			nodes[i].Status = p.NodeStatus(addr)
		}

		r.HTML(
			http.StatusOK,
			"nodes",
			map[string]interface{}{
				"Page":  "Nodes",
				"Nodes": nodes,
			},
		)
	})

	m.Get("/charts/:addr", func(params martini.Params, r rendergold.Render) {
		addr := params["addr"]
		mon := p.NodeMonitor(addr)
		if mon == nil {
			r.Redirect("/nodes")
		}
		r.HTML(
			http.StatusOK,
			"charts",
			map[string]interface{}{
				"Page":    "Charts",
				"Addr":    addr,
				"Metrics": mon.Metrics(),
			},
		)
	})

	m.Get("/chart/:addr", func(params martini.Params, res http.ResponseWriter, req *http.Request) {
		addr := params["addr"]
		mon := p.NodeMonitor(addr)
		if mon == nil {
			res.WriteHeader(400)
			return
		}
		metricArgs := req.URL.Query()["metric"]
		if len(metricArgs) != 1 {
			res.WriteHeader(400)
			return
		}
		metric := metricArgs[0]
		points := mon.Points(metric)
		enc := json.NewEncoder(res)
		err := enc.Encode(points)
		if err != nil {
			log.Error("%v", err)
		}
	})

	m.Get("/map", func(r rendergold.Render) {
		r.HTML(
			http.StatusOK,
			"map",
			map[string]interface{}{
				"Page": "Map",
			},
		)
	})

	m.Get("/tiles/:z/:x/:y.png", func(params martini.Params) []byte {
		z, _ := strconv.ParseUint(params["z"], 10, 64)
		x, _ := strconv.ParseUint(params["x"], 10, 64)
		y, _ := strconv.ParseUint(params["y"], 10, 64)
		coord := gopnik.TileCoord{x, y, z, 1, nil}

		res, err := cache.Get(coord)
		if err != nil {
			log.Error("%v", err)
			return nil
		}
		return res
	})

	log.Info("Starting WebUI on %v", addr)
	log.Fatal(http.ListenAndServe(addr, m))
}