mirror of
https://github.com/valyala/fasthttp.git
synced 2026-06-14 15:56:44 +03:00
Added HelloWorld server example
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
# Code examples
|
||||
|
||||
* [HelloWorld server](helloworldserver)
|
||||
* [Static file server](fileserver)
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
helloworldserver: clean
|
||||
go get -u github.com/valyala/fasthttp
|
||||
go build
|
||||
|
||||
clean:
|
||||
rm -f helloworldserver
|
||||
@@ -0,0 +1,15 @@
|
||||
# HelloWorld server example
|
||||
|
||||
Displays various request info.
|
||||
|
||||
# How to build
|
||||
|
||||
```
|
||||
make
|
||||
```
|
||||
|
||||
# How to run
|
||||
|
||||
```
|
||||
./helloworldserver -addr=tcp.addr.to.listen:to
|
||||
```
|
||||
@@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
var addr = flag.String("addr", ":8080", "TCP address to listen to")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
if err := fasthttp.ListenAndServe(*addr, requestHandler); err != nil {
|
||||
log.Fatalf("Error in ListenAndServe: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func requestHandler(ctx *fasthttp.RequestCtx) {
|
||||
fmt.Fprintf(ctx, "Hello, world!\n\n")
|
||||
|
||||
fmt.Fprintf(ctx, "Request method is %q\n", ctx.Method())
|
||||
fmt.Fprintf(ctx, "RequestURI is %q\n", ctx.RequestURI())
|
||||
fmt.Fprintf(ctx, "Requested path is %q\n", ctx.Path())
|
||||
fmt.Fprintf(ctx, "Host is %q\n", ctx.Host())
|
||||
fmt.Fprintf(ctx, "Query string is %q\n", ctx.QueryArgs())
|
||||
fmt.Fprintf(ctx, "User-Agent is %q\n", ctx.Request.Header.UserAgent())
|
||||
fmt.Fprintf(ctx, "Connection has been established at %s\n", ctx.ConnTime())
|
||||
fmt.Fprintf(ctx, "Request has been started at %s\n", ctx.Time())
|
||||
fmt.Fprintf(ctx, "Serial request number for the current connection is %d\n", ctx.ConnRequestNum())
|
||||
fmt.Fprintf(ctx, "Your ip is %q\n\n", ctx.RemoteIP())
|
||||
|
||||
fmt.Fprintf(ctx, "Raw request is:\n---CUT---\n%s\n---CUT---", &ctx.Request)
|
||||
|
||||
ctx.SetContentType("text/plain; charset=utf8")
|
||||
}
|
||||
Reference in New Issue
Block a user