Example #1
0
func main() {
	// Parse command line options
	flag.Parse()

	if flag.Arg(0) != "" {
		path = flag.Arg(0)
	}

	// setup pipeline
	pipeline := falcore.NewPipeline()

	// upstream filters
	// Serve index.html for root requests
	pipeline.Upstream.PushBack(falcore.NewRequestFilter(func(req *falcore.Request) *http.Response {
		if req.HttpRequest.URL.Path == "/" {
			req.HttpRequest.URL.Path = "/index.html"
		}
		return nil
	}))

	// Serve files
	pipeline.Upstream.PushBack(&static_file.Filter{
		BasePath: path,
	})

	// Serve SASS files
	pipeline.Upstream.PushBack(&sass_file.Filter{
		BasePath: path,
	})

	// Serve Coffee files
	pipeline.Upstream.PushBack(&coffee_file.Filter{
		BasePath: path,
	})

	if *singlePageApp {
		// Serve index.html no matter what
		pipeline.Upstream.PushBack(falcore.NewRequestFilter(func(req *falcore.Request) *http.Response {
			req.HttpRequest.URL.Path = "/index.html"
			return nil
		}))

		// Rewrite me !
		pipeline.Upstream.PushBack(&static_file.Filter{
			BasePath: path,
		})
	}

	// downstream
	pipeline.Downstream.PushBack(compression.NewFilter(nil))

	// setup server
	server := falcore.NewServer(*port, pipeline)

	// start the server
	// this is normally blocking forever unless you send lifecycle commands
	if err := server.ListenAndServe(); err != nil {
		fmt.Println("Could not start server:", err)
	}
}
Example #2
0
func main() {
	pid := syscall.Getpid()
	flag.Parse()

	// create the pipeline
	pipeline := falcore.NewPipeline()
	pipeline.Upstream.PushBack(falcore.NewRequestFilter(Filter))

	// create the server with the pipeline
	srv := falcore.NewServer(8090, pipeline)

	// if passed the socket file descriptor, setup the listener that way
	// if you don't have it, the default is to create the socket listener
	// with the data passed to falcore.NewServer above (happens in ListenAndServer())
	if *socketFd != -1 {
		// I know I'm a child process if I get here so I can signal the parent when I'm ready to take over
		go childReady(srv)
		fmt.Printf("%v Got socket FD: %v\n", pid, *socketFd)
		srv.FdListen(*socketFd)
	}

	// using signals to manage the restart lifecycle
	go handleSignals(srv)

	// start the server
	// this is normally blocking forever unless you send lifecycle commands
	if err := srv.ListenAndServe(); err != nil {
		fmt.Printf("%v Could not start server: %v", pid, err)
	}
	fmt.Printf("%v Exiting now\n", pid)
}
Example #3
0
func main() {

	port := 8080

	pipeline := falcore.NewPipeline()

	pipeline.Upstream.PushBack(
		falcore.NewRequestFilter(
			func(req *falcore.Request) *http.Response {
				return falcore.SimpleResponse(
					req.HttpRequest,
					200,
					nil,
					"Hello, World!")
			}))

	server := falcore.NewServer(port, pipeline)

	if err := server.ListenAndServe(); err != nil {
		fmt.Println("Could not start server:" + err.Error())
	}

}
Example #4
0
func init() {
	go func() {
		// falcore setup
		pipeline := falcore.NewPipeline()
		pipeline.Upstream.PushBack(falcore.NewRequestFilter(func(req *falcore.Request) *http.Response {
			for _, data := range serverData {
				if data.path == req.HttpRequest.URL.Path {
					header := make(http.Header)
					header.Set("Etag", data.etag)
					return falcore.SimpleResponse(req.HttpRequest, data.status, header, string(data.body))
				}
			}
			return falcore.SimpleResponse(req.HttpRequest, 404, nil, "Not Found")
		}))

		pipeline.Downstream.PushBack(new(Filter))

		srv = falcore.NewServer(0, pipeline)
		if err := srv.ListenAndServe(); err != nil {
			panic("Could not start falcore")
		}
	}()
}
Example #5
0
)

// Command line options
var (
	port = flag.Int("port", 8000, "the port to listen on")
)

func main() {
	// parse command line options
	flag.Parse()

	// setup pipeline
	pipeline := falcore.NewPipeline()

	// upstream
	pipeline.Upstream.PushBack(helloFilter)

	// setup server
	server := falcore.NewServer(*port, pipeline)

	// start the server
	// this is normally blocking forever unless you send lifecycle commands
	if err := server.ListenAndServe(); err != nil {
		fmt.Println("Could not start server:", err)
	}
}

var helloFilter = falcore.NewRequestFilter(func(req *falcore.Request) *http.Response {
	return falcore.SimpleResponse(req.HttpRequest, 200, nil, "hello world!")
})
Example #6
0
	// start the server
	// this is normally blocking forever unless you send lifecycle commands
	if err := server.ListenAndServe(); err != nil {
		fmt.Println("Could not start server:", err)
	}
}

// Example filter to show timing features
type delayFilter int

func (f delayFilter) FilterRequest(req *falcore.Request) *http.Response {
	status := rand.Intn(2) // random status 0 or 1
	if status == 0 {
		time.Sleep(time.Duration(rand.Int63n(100e6))) // random sleep between 0 and 100 ms
	}
	req.CurrentStage.Status = byte(status) // set the status to produce a unique signature
	return nil
}

// Example filter that returns a Response
type helloFilter int

func (f helloFilter) FilterRequest(req *falcore.Request) *http.Response {
	return falcore.SimpleResponse(req.HttpRequest, 200, nil, "hello world!\n")
}

var reqCB = falcore.NewRequestFilter(func(req *falcore.Request) *http.Response {
	req.Trace() // Prints detailed stats about the request to the log
	return nil
})