Skip to content

Latest commit

 

History

History
51 lines (38 loc) · 963 Bytes

hs1.md

File metadata and controls

51 lines (38 loc) · 963 Bytes

Http server

1. Create http server by compiling golang binary.

package main

import (
	"flag"
	"fmt"
	"log"
	"net/http"
	"time"
)

var (
	PORT  = flag.Int("port", 80, "Server port")
	DELAY = flag.Int("delay", 150, "delay in millisecond")
	LOGS  = flag.Bool("logs", false, "Show logs")
)

func main() {

	flag.Parse()

	http.HandleFunc("/", handler)

	err := http.ListenAndServe(fmt.Sprintf(":%d", *PORT), nil)

	fmt.Printf("ListenAndServe: %v\n", err)
}

func handler(w http.ResponseWriter, r *http.Request) {

	logs("start")

	time.Sleep(time.Duration(*DELAY) * time.Millisecond)

	logs("end")

	fmt.Fprint(w, "quack")
}

func logs(format string, v ...interface{}) {
	if *LOGS {
		log.Printf(format, v...)
	}
}