forked from padok-team/github-actions-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
53 lines (42 loc) · 1.15 KB
/
main.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"fmt"
"log"
"net/http"
"strconv"
"github.com/padok-team/github-actions-tutorial/foobar"
)
const addr = ":8080"
func main() {
http.HandleFunc("/foobar", foobarHandler)
http.HandleFunc("/healthz", healthHandler)
log.Printf("Listening for requests on %s\n", addr)
err := http.ListenAndServe(addr, nil)
if err != nil {
log.Fatal(err)
}
}
// foobarHandler responds with a FooBar sequence.
// Expects a "length" parameter defining the size of the sequence.
func foobarHandler(w http.ResponseWriter, r *http.Request) {
lengthParam := r.URL.Query().Get("length")
if lengthParam == "" {
http.Error(w, "missing parameter: length", http.StatusBadRequest)
return
}
length, err := strconv.Atoi(lengthParam)
if err != nil {
http.Error(w, "invalid length", http.StatusBadRequest)
return
}
seq, err := foobar.Sequence(length)
if err != nil {
http.Error(w, fmt.Sprintf("failed to compute sequence: %s", err.Error()), http.StatusBadRequest)
return
}
fmt.Fprintf(w, "%s", seq)
}
// healthHandler reports on the server's health.
func healthHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Server is healthy :)")
}