Exemplo n.º 1
0
func NewHandler(dir string) *Handler {
	handler := &Handler{
		Dir:      dir,
		requests: make(chan *request),
	}

	go func() {
		for {
			req := <-handler.requests
			handler.p = NewProcfile(handler.Dir, portmanager)
			handler.p.SetConcurrency("web=2")
			handler.p.LaunchAll()

			addrs := handler.p.AddrsFor("web")
			if len(addrs) == 0 {
				break
			}

			handlers := make([]http.Handler, 0, len(addrs))
			for _, addr := range addrs {
				<-Listening(addr)
				handlers = append(handlers, proxy.New(addr))
			}

			loadBalancer := balancer.New(handlers...)

		RequestLoop:
			for {
				go func(req *request) {
					loadBalancer.ServeHTTP(req.W, req.R)
					req.done <- true
				}(req)

				select {
				case req = <-handler.requests:
					// Continue Loop
				case <-time.After(10 * time.Second):
					fmt.Println("Exiting!!!")
					handler.p.Kill()
					break RequestLoop
				}
			}
		}
	}()

	return handler
}
Exemplo n.º 2
0
func load(path string, routes *router.Router) {
	eachStatement(path, func(tokens []string) {
		switch strings.ToUpper(tokens[0]) {
		case "PROXY":
			fmt.Println("prx...", tokens[1:])
			if len(tokens) != 3 {
				log.Fatal("Proxy requires 2 arguments: host and target")
			}
			routes.Add(tokens[1], proxy.New(tokens[2]))
		case "PROCFILE":
			fmt.Println("procfile...", tokens[1:])
			routes.Add(tokens[1], procfile.NewHandler(tokens[2]))
		case "ALIAS":
			fmt.Println("ala...", tokens[1:])
			routes.Alias(tokens[1], tokens[2])
		}
	})
}