当前位置: 首页>>代码示例>>Golang>>正文


Golang falcore.StringResponse函数代码示例

本文整理汇总了Golang中github.com/fitstar/falcore.StringResponse函数的典型用法代码示例。如果您正苦于以下问题:Golang StringResponse函数的具体用法?Golang StringResponse怎么用?Golang StringResponse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了StringResponse函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: init

func init() {
	go func() {
		// falcore setup
		pipeline := falcore.NewPipeline()
		pipeline.Upstream.PushBack(falcore.NewRequestFilter(func(req *falcore.Request) *http.Response {
			for _, data := range eserverData {
				if data.path == req.HttpRequest.URL.Path {
					header := make(http.Header)
					header.Set("Etag", data.etag)
					if data.chunked {
						buf := new(bytes.Buffer)
						buf.Write(data.body)
						res := falcore.SimpleResponse(req.HttpRequest, data.status, header, -1, ioutil.NopCloser(buf))
						res.TransferEncoding = []string{"chunked"}
						return res
					} else {
						return falcore.StringResponse(req.HttpRequest, data.status, header, string(data.body))
					}
				}
			}
			return falcore.StringResponse(req.HttpRequest, 404, nil, "Not Found")
		}))

		pipeline.Downstream.PushBack(new(EtagFilter))

		esrv = falcore.NewServer(0, pipeline)
		if err := esrv.ListenAndServe(); err != nil {
			panic("Could not start falcore")
		}
	}()
}
开发者ID:dcmrlee,项目名称:falcore,代码行数:31,代码来源:etag_test.go

示例2: FilterRequest

// Handle upload
func (f UploadFilter) FilterRequest(request *falcore.Request) *http.Response {
	multipartReader, error := request.HttpRequest.MultipartReader()

	if error == http.ErrNotMultipart {
		return falcore.StringResponse(request.HttpRequest, http.StatusBadRequest, nil, "Bad Request // Not Multipart")
	} else if error != nil {
		return falcore.StringResponse(request.HttpRequest, http.StatusInternalServerError, nil, "Upload Error: "+error.Error()+"\n")
	}

	length := request.HttpRequest.ContentLength
	fmt.Println("content length:", length)

	part, error := multipartReader.NextPart()
	if error != io.EOF {
		var read int64
		var percent, previousPercent float64
		var filename string

		// find non existing filename
		for i := 1; ; i++ {
			filename = fmt.Sprintf("uploadedFile%v.mov", i)
			_, err := os.Stat(filename)
			if os.IsNotExist(err) {
				break
			}
		}

		fmt.Println(filename)
		destination, error := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0644)
		if error != nil {
			return falcore.StringResponse(request.HttpRequest, http.StatusInternalServerError, nil, "Internal Server Error // Could not create file")
		}

		buffer := make([]byte, 1024) // 100 kB
		for {
			byteCount, error := part.Read(buffer)
			read += int64(byteCount)
			percent = math.Floor(float64(read)/float64(length)*100) + 1

			if error == io.EOF {
				break
			} else if read > maxUploadBytes {
				fmt.Println("file too big")
				return falcore.StringResponse(request.HttpRequest, http.StatusRequestEntityTooLarge, nil, "Request Entity Too Large")
			}

			if percent != previousPercent {
				previousPercent = percent
				fmt.Printf("progress %v%%, read %fmb, %v byte of %v\n", percent, (float64(read) / (1024 * 1024)), read, length)
			}
			destination.Write(buffer)
		}
	}

	return falcore.StringResponse(request.HttpRequest, http.StatusOK, nil, "upload finished\n")
}
开发者ID:newspeak,项目名称:newspeak-server,代码行数:57,代码来源:upload.go

示例3: FilterRequest

func (f helloFilter) FilterRequest(req *falcore.Request) *http.Response {
	if req.HttpRequest.URL.Path == "/fast" {
		return falcore.StringResponse(req.HttpRequest, 200, nil, string("Hello, world!"))
	}
	if req.HttpRequest.URL.Path == "/slow" {
		time.Sleep(5 * time.Second)
		return falcore.StringResponse(req.HttpRequest, 200, nil, string("Hello, world!"))
	}
	if req.HttpRequest.URL.Path == "/panic" {
		panic("There is no need for panic")
		return falcore.StringResponse(req.HttpRequest, 200, nil, string("Hello, world!"))
	}

	return falcore.StringResponse(req.HttpRequest, 404, nil, "Not Found")
}
开发者ID:deferpanic,项目名称:dpfalcore,代码行数:15,代码来源:example.go

示例4: SuccessResponse

// return response message with encoded json data and http status code 200 (OK)
func SuccessResponse(request *falcore.Request, jsonData interface{}) *http.Response {
	response, jsonError := falcore.JSONResponse(request.HttpRequest, http.StatusOK, nil, jsonData)
	if jsonError != nil {
		response = falcore.StringResponse(request.HttpRequest, http.StatusInternalServerError, nil, fmt.Sprintf("JSON error: %s", jsonError))
	}
	return response
}
开发者ID:newspeak,项目名称:newspeak-server,代码行数:8,代码来源:response_messages.go

示例5: NewLandingFilter

// NewLandingFilter generates a Falcore RequestFilter that produces a simple
// landing page.
func NewLandingFilter() falcore.RequestFilter {
	log().Debug("registering a new landing filter")

	return falcore.NewRequestFilter(
		func(req *falcore.Request) *http.Response {
			log().Info("running landing filter")

			return falcore.StringResponse(
				req.HttpRequest,
				200,
				nil,
				"<html><head><title>"+
					"Pullcord Landing Page"+
					"</title></head><body><h1>"+
					"Pullcord Landing Page"+
					"</h1><p>"+
					"This is the landing page for Pullcord, "+
					"a reverse proxy for cloud-based web apps "+
					"that allows the servers the web apps run on "+
					"to be turned off when not in use."+
					"</p><p>"+
					"If you are unsure of how to proceed, "+
					"please contact the site administrator."+
					"</p></body></html>",
			)
		},
	)
}
开发者ID:stuphlabs,项目名称:pullcord,代码行数:30,代码来源:landing.go

示例6: FilterRequest

func (f *FileFilter) 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.StringResponse(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.StringResponse(req.HttpRequest, 500, nil, "Server Error\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.Mode()&os.ModeType == 0 {
			res = &http.Response{
				Request:       req.HttpRequest,
				StatusCode:    200,
				Proto:         "HTTP/1.1",
				ProtoMajor:    1,
				ProtoMinor:    1,
				Body:          file,
				Header:        make(http.Header),
				ContentLength: stat.Size(),
			}
			if ct := mime.TypeByExtension(filepath.Ext(asset_path)); ct != "" {
				res.Header.Set("Content-Type", ct)
			}
		} else {
			file.Close()
		}
	} else {
		falcore.Finest("Can't open %v: %v", asset_path, err)
	}
	return
}
开发者ID:kelsieflynn,项目名称:falcore,代码行数:46,代码来源:file_filter.go

示例7: notImplementedError

func notImplementedError(request *falcore.Request) *http.Response {
	return falcore.StringResponse(
		request.HttpRequest,
		501,
		nil,
		"<html><body><h1>Not Implemented</h1>"+
			"<p>The requested behavior has not yet been implemented."+
			"Please contact your system administrator.</p></body></html>",
	)
}
开发者ID:stuphlabs,项目名称:pullcord,代码行数:10,代码来源:login_handler.go

示例8: internalServerError

func internalServerError(request *falcore.Request) *http.Response {
	return falcore.StringResponse(
		request.HttpRequest,
		500,
		nil,
		"<html><body><h1>Internal Server Error</h1>"+
			"<p>An internal server error occured."+
			"Please contact your system administrator.</p></body></html>",
	)
}
开发者ID:stuphlabs,项目名称:pullcord,代码行数:10,代码来源:login_handler.go

示例9: FilterRequest

func (f *FileFilter) 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 {
		// The requested path doesn't fall into the scope of paths that are supposed to be handled by this filter
		return
	}

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

	// Open File
	if file, err := os.Open(asset_path); err == nil {
		// If it's a directory, try opening the directory index
		if stat, err := file.Stat(); f.DirectoryIndex != "" && err == nil && stat.Mode()&os.ModeDir > 0 {
			file.Close()

			asset_path = filepath.Join(asset_path, f.DirectoryIndex)
			if file, err = os.Open(asset_path); err != nil {
				return
			}
		}

		// Make sure it's an actual file
		if stat, err := file.Stat(); err == nil && stat.Mode()&os.ModeType == 0 {
			res = &http.Response{
				Request:       req.HttpRequest,
				StatusCode:    200,
				Proto:         "HTTP/1.1",
				ProtoMajor:    1,
				ProtoMinor:    1,
				Body:          file,
				Header:        make(http.Header),
				ContentLength: stat.Size(),
			}
			if ct := mime.TypeByExtension(filepath.Ext(asset_path)); ct != "" {
				res.Header.Set("Content-Type", ct)
			}
		} else {
			file.Close()
		}
	} else {
		falcore.Finest("Can't open %v: %v", asset_path, err)
	}
	return
}
开发者ID:dcmrlee,项目名称:falcore,代码行数:55,代码来源:file_filter.go

示例10: init

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

		pipeline.Downstream.PushBack(new(EtagFilter))

		esrv = falcore.NewServer(0, pipeline)
		if err := esrv.ListenAndServe(); err != nil {
			panic("Could not start falcore")
		}
	}()
}
开发者ID:dgrijalva,项目名称:falcore,代码行数:23,代码来源:etag_test.go

示例11: loginPage

func loginPage(
	request *falcore.Request,
	sesh Session,
	badCreds bool,
) *http.Response {
	return falcore.StringResponse(
		request.HttpRequest,
		501,
		nil,
		"<html><body><h1>Not Implemented</h1>"+
			"<p>The requested behavior has not yet been implemented."+
			"Please contact your system administrator.</p></body></html>",
	)
}
开发者ID:stuphlabs,项目名称:pullcord,代码行数:14,代码来源:login_handler.go

示例12: TestUpstreamThrottle

func TestUpstreamThrottle(t *testing.T) {
	// Start a test server
	sleepPipe := falcore.NewPipeline()
	sleepPipe.Upstream.PushBack(falcore.NewRequestFilter(func(req *falcore.Request) *http.Response {
		time.Sleep(time.Second)
		return falcore.StringResponse(req.HttpRequest, 200, nil, "OK")
	}))
	sleepSrv := falcore.NewServer(0, sleepPipe)
	go func() {
		sleepSrv.ListenAndServe()
	}()
	<-sleepSrv.AcceptReady

	// Build Upstream
	up := NewUpstream(NewUpstreamTransport("localhost", sleepSrv.Port(), 0, nil))
	// pipe := falcore.NewPipeline()
	// pipe.Upstream.PushBack(up)

	resCh := make(chan *http.Response, 10)
	var i int64 = 1
	for ; i < 12; i++ {
		start := time.Now()
		up.SetMaxConcurrent(i)

		for j := 0; j < 10; j++ {
			go func() {
				req, _ := http.NewRequest("GET", "/", nil)
				_, res := falcore.TestWithRequest(req, up, nil)
				resCh <- res
				// fmt.Println("OK")
			}()
		}
		for j := 0; j < 10; j++ {
			res := <-resCh
			if res.StatusCode != 200 {
				t.Fatalf("Error: %v", res)
			}
		}

		duration := time.Since(start)
		seconds := float64(duration) / float64(time.Second)
		goal := math.Ceil(10.0 / float64(i))
		// fmt.Println(i, "Time:", seconds, "Goal:", goal)
		if seconds < goal {
			t.Errorf("%v: Too short: %v < %v", i, seconds, goal)
		}
	}

}
开发者ID:kelsieflynn,项目名称:falcore,代码行数:49,代码来源:upstream_test.go

示例13: ErrorResponse

// return response message with encoded json error message and a custom error code
// if logMessage is nil, do not write to log
func ErrorResponse(request *falcore.Request, status int, error string, errorDescription string, logMessage error) *http.Response {
	if logMessage != nil {
		log.Println(fmt.Sprintf("%s: %s, %s", error, errorDescription, logMessage))
	}

	type Json struct {
		Error            string
		ErrorDescription string
	}
	json := Json{error, errorDescription}

	response, jsonError := falcore.JSONResponse(request.HttpRequest, status, nil, json)
	if jsonError != nil {
		response = falcore.StringResponse(request.HttpRequest, http.StatusInternalServerError, nil, fmt.Sprintf("JSON error: %s", jsonError))
		log.Println(fmt.Sprintf("%s: %s, %s, json error:", error, errorDescription, logMessage, jsonError))
	}
	return response
}
开发者ID:newspeak,项目名称:newspeak-server,代码行数:20,代码来源:response_messages.go

示例14: Filter

// very simple request filter
func Filter(request *falcore.Request) *http.Response {
	return falcore.StringResponse(request.HttpRequest, 200, nil, "OK\n")
}
开发者ID:dcmrlee,项目名称:falcore,代码行数:4,代码来源:hot_restart.go

示例15: FilterRequest

func (f helloFilter) FilterRequest(req *falcore.Request) *http.Response {
	return falcore.StringResponse(req.HttpRequest, 200, nil, "hello world!\n")
}
开发者ID:dcmrlee,项目名称:falcore,代码行数:3,代码来源:blog_example.go


注:本文中的github.com/fitstar/falcore.StringResponse函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。