示例#1
0
func init() {
	mapnikRenderer, _ = render.NewMapnik()
	if mapnikRenderer != nil {
		wd, _ := os.Getwd()
		mapnikRenderer.RegisterFonts(wd)
	}
	mapserverRenderer, _ = render.NewMapServer()
}
示例#2
0
func main() {
	if os.Getenv("GOMAXPROCS") == "" {
		runtime.GOMAXPROCS(runtime.NumCPU())
	}

	var listenAddr = flag.String("listen", "localhost:7070", "listen address")
	var configFile = flag.String("config", DefaultConfigFile, "config")
	var builderType = flag.String("builder", "mapnik", "builder type {mapnik,mapserver}")
	var version = flag.Bool("version", false, "print version and exit")

	flag.Parse()

	if *version {
		fmt.Println(magnacarto.Version)
		os.Exit(0)
	}

	conf, err := config.Load(*configFile)
	if *configFile == DefaultConfigFile && os.IsNotExist(err) {
		// ignore error for missing default config
	} else if err != nil {
		log.Fatal(err)
	}

	builderCache := builder.NewCache(conf.Locator)
	if conf.OutDir != "" {
		if err := os.MkdirAll(conf.OutDir, 0755); err != nil && !os.IsExist(err) {
			log.Fatal("unable to create outdir: ", err)
		}
		builderCache.SetDestination(conf.OutDir)
	}

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	go func() {
		for _ = range c {
			builderCache.ClearAll()
			os.Exit(1)
		}
	}()

	r := mux.NewRouter()
	handler := magnaserv{config: conf, builderCache: builderCache}
	handler.mapnikMaker = mapnikBuilder.Maker2

	mapnikRenderer, err := render.NewMapnik()
	if err != nil {
		log.Print("Mapnik plugin: ", err)
	} else {
		log.Print("Mapnik plugin available")
		for _, fontDir := range conf.Mapnik.FontDirs {
			mapnikRenderer.RegisterFonts(fontDir)
		}
		if is3, _ := mapnikRenderer.Is3(); is3 {
			handler.mapnikMaker = mapnikBuilder.Maker3
		}
		handler.mapnikRenderer = mapnikRenderer
	}

	mapserverRenderer, err := render.NewMapServer()
	if err != nil {
		log.Print("MapServer plugin: ", err)
	} else {
		log.Print("MapServer plugin available")
		handler.mapserverRenderer = mapserverRenderer
	}

	switch *builderType {
	case "mapnik", "mapnik2", "mapnik3":
		handler.defaultMaker = handler.mapnikMaker
	case "mapserver":
		handler.defaultMaker = mapserver.Maker
	default:
		log.Fatal("unknown -builder ", *builderType)
	}

	v1 := r.PathPrefix("/api/v1").Subrouter()
	v1.HandleFunc("/map", handler.render)
	v1.HandleFunc("/projects/{path:.*?}.mml", handler.mml)
	v1.HandleFunc("/projects/{path:.*?}.mcp", handler.mcp)
	v1.HandleFunc("/projects", handler.projects)
	v1.Handle("/changes", websocket.Handler(handler.changes))

	appDir := findAppDir()
	r.Handle("/magnacarto/{path:.*}", http.StripPrefix("/magnacarto/", http.FileServer(http.Dir(appDir))))

	r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		http.Redirect(w, r, "/magnacarto/", 302)
	})

	log.Printf("listening on http://%s", *listenAddr)

	log.Fatal(http.ListenAndServe(*listenAddr, handlers.LoggingHandler(os.Stdout, r)))
}