Esempio n. 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
}
Esempio n. 2
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)
	}
}