/
main.go
59 lines (53 loc) · 1.23 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
54
55
56
57
58
59
package main
import (
"fmt"
"net/http"
"os"
"strings"
"github.com/chyeh/pubip"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/rs/zerolog/hlog"
"github.com/rs/zerolog/log"
)
func HelloServer(w http.ResponseWriter, r *http.Request) {
hlog.FromRequest(r).Info().
Str("method", r.Method).
Stringer("url", r.URL).
Msg("")
fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:])
if r.URL.RawQuery == "debug" {
fmt.Fprintf(w, "\n\n\n-- env:\n\n")
for _, e := range os.Environ() {
fmt.Fprintf(w, "%s\n", e)
}
fmt.Fprintf(w, "\n\n-- headers:\n\n")
for k, v := range r.Header {
fmt.Fprintf(w, "%s: %s\n", k, strings.Join(v, ","))
}
}
if r.URL.RawQuery == "ip" {
fmt.Fprintf(w, "\n\n\n-- my public ip: ")
ip, err := pubip.Get()
if err != nil {
fmt.Fprintf(w, "%s\n", err)
} else {
fmt.Fprintf(w, "%s\n", ip)
}
}
}
func main() {
port := os.Getenv("PORT")
if port == "" {
log.Warn().Msg("PORT is undefined, assuming 80")
port = "80"
}
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Get("/*", HelloServer)
r.Get("/db/{dbPrefix}", TestDB)
fmt.Println("Listening to port", port)
if err := http.ListenAndServe(":"+port, r); err != nil {
log.Fatal().Err(err).Msg("ListenAndServe")
}
}