-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.go
More file actions
39 lines (31 loc) · 737 Bytes
/
Copy pathapi.go
File metadata and controls
39 lines (31 loc) · 737 Bytes
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
38
39
package main
import (
"fmt"
"io"
"net/http"
)
func hello(w http.ResponseWriter, r *http.Request) {
// Default response
res := "Hello World!"
// Check for a name parameter in querystring
queryVals := r.URL.Query()
name := queryVals.Get("name")
if name != "" {
res = fmt.Sprintf("Hello %s!", name)
}
// Write response
io.WriteString(w, res)
}
func health(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Ok")
}
func main() {
// Create http handler mux
mux := http.NewServeMux()
// Set health and hello world routes
mux.HandleFunc("/", health)
mux.HandleFunc("/hello", hello)
// Start listening
fmt.Println("Sample API server listening on 0.0.0.0:8081")
http.ListenAndServe("0.0.0.0:8081", mux)
}