示例#1
0
func NewPixelApi(cc ContainerCreator) *PixelApi {
	pa := &PixelApi{
		RWMutex:  &sync.RWMutex{},
		Messages: make(chan *Message),
		pixels:   make(map[string]*Pixel),
		cc:       cc,
	}
	h := httptools.NewRegexpSwitch(map[string]http.Handler{
		"/": httptools.MethodSwitch{
			"GET":  http.HandlerFunc(pa.ListPixels),
			"POST": http.HandlerFunc(pa.CreatePixel),
		},
		"/([a-z0-9]+)(/.+)?": httptools.L{
			httptools.DiscardPathElements(1),
			httptools.SilentHandler(http.HandlerFunc(pa.ValidatePixelId)),
			httptools.NewRegexpSwitch(map[string]http.Handler{
				"/content": httptools.MethodSwitch{"GET": http.HandlerFunc(pa.GetPixelContent)},
				"/logs":    httptools.MethodSwitch{"GET": http.HandlerFunc(pa.GetPixelLogs)},
				"/fs":      httptools.MethodSwitch{"GET": http.HandlerFunc(pa.GetPixelFs)},
				"/": httptools.MethodSwitch{
					"GET":    http.HandlerFunc(pa.ShowPixel),
					"PUT":    http.HandlerFunc(pa.UpdatePixel),
					"DELETE": http.HandlerFunc(pa.DeletePixel),
				},
			}),
		},
	})
	pa.Handler = h
	return pa
}
示例#2
0
func main() {
	var (
		listen = flag.String("listen", "localhost:5000", "Address to bind webserver to")
		static = flag.String("static", "./static", "Path to static folder")
	)

	flag.Parse()

	log.Printf("Starting webserver on %s...", *listen)
	err := http.ListenAndServe(*listen, httptools.NewRegexpSwitch(map[string]http.Handler{
		"/jobs": httptools.MethodSwitch{
			"GET":  http.HandlerFunc(listJobs),
			"POST": http.HandlerFunc(createJob),
		},
		"/jobs/[0-9a-f-]+": httptools.List{
			httptools.DiscardPathElements(1),
			httptools.SilentHandlerFunc(extractJobId),
			httptools.MethodSwitch{
				"GET":    http.HandlerFunc(showJob),
				"DELETE": http.HandlerFunc(deleteJob),
			},
		},
		"/images/[0-9a-f-]+": httptools.List{
			httptools.DiscardPathElements(1),
			httptools.SilentHandlerFunc(extractJobId),
			httptools.MethodSwitch{
				"GET": http.HandlerFunc(listImages),
			},
		},
		"/images/[0-9a-f-]+/[^/]+": httptools.List{
			httptools.DiscardPathElements(1),
			httptools.SilentHandlerFunc(extractJobId),
			httptools.DiscardPathElements(1),
			httptools.SilentHandlerFunc(extractImageId),
			httptools.MethodSwitch{
				"GET":    http.HandlerFunc(serveImage),
				"DELETE": http.HandlerFunc(deleteImage),
			},
		},
		"/": http.FileServer(http.Dir(*static)),
	}))
	if err != nil {
		log.Fatalf("Error starting webserver on %s: %s", *listen, err)
	}
}
示例#3
0
func main() {
	goptions.ParseAndFail(&options)

	goriot.SetAPIKey(options.APIKey)

	session, err := mgo.Dial(options.MongoDB)
	if err != nil {
		log.Fatalf("Could not connect to MongoDB: %s", err)
	}
	db = session.DB("")

	r := httptools.NewRegexpSwitch(map[string]http.Handler{
		"/([a-z]+)/([0-9]+)/parse": httptools.L{
			httptools.SilentHandler(http.HandlerFunc(whitelistHandler)),
			httptools.L{
				httptools.SilentHandler(http.HandlerFunc(parseMatchHistory)),
				http.HandlerFunc(dumpMatchHistory),
			},
		},
		"/([a-z]+)/([0-9]+)": httptools.L{
			httptools.SilentHandler(http.HandlerFunc(whitelistHandler)),
			httptools.MethodSwitch{
				"POST": httptools.L{
					httptools.SilentHandler(http.HandlerFunc(parseMatchHistory)),
					http.HandlerFunc(saveMatchHistory),
				},
				"GET": httptools.L{
					httptools.SilentHandler(http.HandlerFunc(queryMatchHistory)),
					http.HandlerFunc(dumpMatchHistory),
				},
			},
		},
		"/.*": http.FileServer(http.Dir(options.StaticContent)),
	})

	addr := fmt.Sprintf("0.0.0.0:%d", options.Port)
	log.Printf("Starting webserver on %s...", addr)
	if err := http.ListenAndServe(addr, r); err != nil {
		log.Fatalf("Could not start webserver: %s", err)
	}
}
示例#4
0
func main() {
	goptions.ParseAndFail(&options)

	if options.Lxc {
		log.Fatalf("LXC support not implemented yet")
	}

	pa := NewPixelApi(NewLocalContainerCreator())

	r := httptools.NewRegexpSwitch(map[string]http.Handler{
		"/ws": NewStreamingHandler(pa),
		"/templates/.*": httptools.L{
			httptools.DiscardPathElements(1),
			templateRenderer{
				Dir:  options.TemplateDir,
				Data: TemplateData(),
			},
		},
		"/pixels(/.*)?": httptools.L{
			httptools.DiscardPathElements(1),
			pa,
		},
		"/handshake": http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			io.WriteString(w, "PIXELPIXEL OK")
		}),
		"/.*": httptools.L{
			http.FileServer(http.Dir(options.StaticDir)),
		},
	})

	log.Printf("Starting webserver on %s...", options.Listen)
	err := http.ListenAndServe(options.Listen, r)
	if err != nil {
		log.Fatalf("Could not start webserver: %s", err)
	}
}