xref: /linux-tools/misc/apps/simpleserver.go (revision 5f143f12281e06f4fb2e612b9489f3514f80f83e)
1package main
2
3import (
4  "io/ioutil"
5  "fmt"
6  "log"
7  "net/http"
8)
9
10var (
11  hostname string
12  port = "8080"
13)
14
15
16func serveID(w http.ResponseWriter, r *http.Request) {
17  fmt.Fprintf(w, hostname)
18}
19
20func startServer() {
21  raw, err := ioutil.ReadFile("/etc/hostname")
22  if err != nil {
23    log.Fatal("Fail to read hostname")
24  }
25  hostname = string(raw)
26  http.HandleFunc("/serverid", serveID)
27  http.HandleFunc("/hello", serveID)
28  log.Fatal(http.ListenAndServe("0.0.0.0:"+port, nil))
29}
30
31
32func main() {
33    startServer()
34}
35
36