-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
101 lines (89 loc) · 2.32 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"fmt"
"log"
"net/http"
"net/url"
"os"
"github.com/go-chi/chi/v5"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/systemli/dereferrer/middleware"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var logger *zap.Logger
func init() {
logLevel := "info"
if os.Getenv("LOG_LEVEL") != "" {
logLevel = os.Getenv("LOG_LEVEL")
}
atomic := zap.NewAtomicLevel()
level, err := zapcore.ParseLevel(logLevel)
if err != nil {
log.Fatal(err)
}
atomic.SetLevel(level)
logger = zap.New(zapcore.NewCore(
zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
zapcore.Lock(os.Stdout),
atomic,
))
}
func main() {
listenAddr := ":8080"
if os.Getenv("LISTEN_ADDR") != "" {
listenAddr = os.Getenv("LISTEN_ADDR")
}
metricsAddr := ":8081"
if os.Getenv("METRICS_ADDR") != "" {
metricsAddr = os.Getenv("METRICS_ADDR")
}
logger.Info("Starting server", zap.String("listenAddr", listenAddr))
go func() {
m := chi.NewRouter()
m.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(metricsAddr, m))
}()
r := chi.NewRouter()
p := middleware.NewPrometheus()
r.Use(p)
r.Get("/", handler)
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(listenAddr, r))
}
func handler(w http.ResponseWriter, r *http.Request) {
if r.URL.RawQuery != "" {
decodedUrl, err := url.PathUnescape(r.URL.RawQuery)
if err != nil {
logger.Error("Error decoding URL", zap.Error(err), zap.String("query", r.URL.RawQuery))
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if !isUrl(decodedUrl) {
logger.Error("Invalid URL", zap.String("query", r.URL.RawQuery), zap.String("decoded_url", decodedUrl))
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte("400 Bad Request"))
return
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(fmt.Sprintf(`<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="refresh" content="0; URL=%s"/>
<title>Redirecting to %s</title>
</head>
<body>
<div align="center">
<p>Redirecting to <a href="%s">%s</a></p>
</div>
</body>
</html>`, decodedUrl, decodedUrl, decodedUrl, decodedUrl)))
return
}
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte("404 Not Found"))
}
func isUrl(str string) bool {
u, err := url.Parse(str)
return err == nil && u.Scheme != "" && u.Host != ""
}