summaryrefslogtreecommitdiff
path: root/main.go
blob: bda2d8766fd2ce8576163e40227f346672bd4300 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main

import (
	"flag"
	"io"
	"log"
	"net/http"
)

type handler struct{}

func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/plain")
	io.WriteString(w, "Hello world!")
}

func main() {
	port := flag.String("port", "8080", "Default localhost port")
	flag.Parse()

	mux := http.NewServeMux()

	mux.Handle("/{$}", &handler{})

	log.Fatal(runServer(mux, *port))
}

func runServer(h http.Handler, port string) error {
	srv := http.Server{
		Addr:    ":" + port,
		Handler: h,
	}

	log.Printf("Serving on port %s...\n", port)

	return srv.ListenAndServe()
}