init
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
echo.exe
|
36
main.go
Normal file
36
main.go
Normal file
@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
var name string
|
||||
|
||||
func main() {
|
||||
hostname, err := os.Hostname()
|
||||
if err != nil {
|
||||
log.Println("got hostname error:", err)
|
||||
}
|
||||
|
||||
name = hostname
|
||||
|
||||
http.HandleFunc("/echo", echoHandler)
|
||||
|
||||
fmt.Println("Starting server on :8080")
|
||||
if err := http.ListenAndServe(":8080", nil); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func echoHandler(w http.ResponseWriter, r *http.Request) {
|
||||
clientIp := getRemoteIP(r)
|
||||
fmt.Fprintf(w, "hello %s Host %s provides services", clientIp, name)
|
||||
}
|
||||
|
||||
func getRemoteIP(r *http.Request) string {
|
||||
return r.RemoteAddr
|
||||
}
|
Reference in New Issue
Block a user