示例#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)

	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)
	}
}