-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain.go
48 lines (41 loc) · 1.09 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
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"github.com/Scalingo/go-handlers"
"github.com/Scalingo/go-utils/logger"
)
func main() {
log := logger.Default()
log.Info("Initializing app")
cfg, err := newConfig()
if err != nil {
log.WithError(err).Error("Fail to initialize configuration")
os.Exit(1)
}
log.Info("Initializing routes")
router := handlers.NewRouter(log)
router.HandleFunc("/ping", pongHandler)
// Initialize web server and configure the following routes:
// GET /repos
// GET /stats
log = log.WithField("port", cfg.Port)
log.Info("Listening...")
err = http.ListenAndServe(fmt.Sprintf(":%d", cfg.Port), router)
if err != nil {
log.WithError(err).Error("Fail to listen to the given port")
os.Exit(2)
}
}
func pongHandler(w http.ResponseWriter, r *http.Request, _ map[string]string) error {
log := logger.Get(r.Context())
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
err := json.NewEncoder(w).Encode(map[string]string{"status": "pong"})
if err != nil {
log.WithError(err).Error("Fail to encode JSON")
}
return nil
}