-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
40 lines (32 loc) · 809 Bytes
/
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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
)
type Status struct {
Status string `json:"status"`
}
func main() {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Get("/hello/{username}", func(w http.ResponseWriter, r *http.Request) {
username := chi.URLParam(r, "username")
fmt.Fprintf(w, "Hello %s", username)
})
r.Get("/status", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
status := Status{Status: "UP"}
jsonBytes, err := json.Marshal(status)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(jsonBytes)
})
fmt.Println("Listening on port :8080")
log.Fatal(http.ListenAndServe(":8080", r))
}