本文整理匯總了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")
}
}()
}
示例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")
}
示例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")
}
示例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
}
示例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>",
)
},
)
}
示例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
}
示例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>",
)
}
示例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>",
)
}
示例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
}
示例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")
}
}()
}
示例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>",
)
}
示例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)
}
}
}
示例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
}
示例14: Filter
// very simple request filter
func Filter(request *falcore.Request) *http.Response {
return falcore.StringResponse(request.HttpRequest, 200, nil, "OK\n")
}
示例15: FilterRequest
func (f helloFilter) FilterRequest(req *falcore.Request) *http.Response {
return falcore.StringResponse(req.HttpRequest, 200, nil, "hello world!\n")
}