コード例 #1
0
ファイル: upstream.go プロジェクト: repos-go/falcore
func (u *Upstream) FilterRequest(request *falcore.Request) (res *http.Response) {
	var err os.Error
	req := request.HttpRequest

	// Force the upstream to use http
	if u.ForceHttp || req.URL.Scheme == "" {
		req.URL.Scheme = "http"
		req.URL.Host = req.Host
	}
	before := time.Nanoseconds()
	req.Header.Set("Connection", "Keep-Alive")
	res, err = u.transport.RoundTrip(req)
	diff := falcore.TimeDiff(before, time.Nanoseconds())
	if err != nil {
		if nerr, ok := err.(net.Error); ok && nerr.Timeout() {
			falcore.Error("%s Upstream Timeout error: %v", request.ID, err)
			res = falcore.SimpleResponse(req, 504, nil, "Gateway Timeout\n")
			request.CurrentStage.Status = 2 // Fail
		} else {
			falcore.Error("%s Upstream error: %v", request.ID, err)
			res = falcore.SimpleResponse(req, 502, nil, "Bad Gateway\n")
			request.CurrentStage.Status = 2 // Fail
		}
	}
	falcore.Debug("%s [%s] [%s%s] s=%d Time=%.4f", request.ID, req.Method, u.host, req.RawURL, res.StatusCode, diff)
	return
}
コード例 #2
0
ファイル: file_filter.go プロジェクト: repos-go/falcore
func (f *Filter) FilterRequest(req *falcore.Request) (res *http.Response) {
	// Clean asset path
	asset_path := filepath.Clean(filepath.FromSlash(req.HttpRequest.URL.Path))

	// Resolve PathPrefix
	if strings.HasPrefix(asset_path, f.PathPrefix) {
		asset_path = asset_path[len(f.PathPrefix):]
	} else {
		falcore.Debug("%v doesn't match prefix %v", asset_path, f.PathPrefix)
		res = falcore.SimpleResponse(req.HttpRequest, 404, nil, "Not found.")
		return
	}

	// Resolve FSBase
	if f.BasePath != "" {
		asset_path = filepath.Join(f.BasePath, asset_path)
	} else {
		falcore.Error("file_filter requires a BasePath")
		return falcore.SimpleResponse(req.HttpRequest, 500, nil, "Server Error\n")
	}

	var fileSize int64
	if stat, err := os.Stat(asset_path); err == nil {
		fileSize = stat.Size
	} else {
		falcore.Debug("Can't stat %v: %v", asset_path, err)
		return falcore.SimpleResponse(req.HttpRequest, 404, nil, "File not found\n")
	}

	// Open File
	if file, err := os.Open(asset_path); err == nil {
		// Make sure it's an actual file
		if stat, err := file.Stat(); err == nil && stat.IsRegular() {
			res = &http.Response{
				Request:       req.HttpRequest,
				StatusCode:    200,
				Proto:         "HTTP/1.1",
				Body:          file,
				Header:        make(http.Header),
				ContentLength: fileSize,
			}
			if ct := mime.TypeByExtension(filepath.Ext(asset_path)); ct != "" {
				res.Header.Set("Content-Type", ct)
			}
		} else {
			file.Close()
			return falcore.SimpleResponse(req.HttpRequest, 404, nil, "File not found\n")
		}
	} else {
		falcore.Debug("Can't open %v: %v", asset_path, err)
		res = falcore.SimpleResponse(req.HttpRequest, 404, nil, "File not found\n")
	}

	return
}
コード例 #3
0
ファイル: compression_test.go プロジェクト: repos-go/falcore
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("Content-Type", data.mime)
					header.Set("Content-Encoding", data.encoding)
					return falcore.SimpleResponse(req.HttpRequest, 200, header, string(data.body))
				}
			}
			return falcore.SimpleResponse(req.HttpRequest, 404, nil, "Not Found")
		}))

		pipeline.Downstream.PushBack(NewFilter(nil))

		srv = falcore.NewServer(0, pipeline)
		if err := srv.ListenAndServe(); err != nil {
			panic("Could not start falcore")
		}
	}()
}
コード例 #4
0
ファイル: hot_restart.go プロジェクト: repos-go/falcore
// very simple request filter
func Filter(request *falcore.Request) *http.Response {
	return falcore.SimpleResponse(request.HttpRequest, 200, nil, "OK\n")
}
コード例 #5
0
ファイル: hello_world.go プロジェクト: repos-go/falcore
)

// 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!")
})