-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
62 lines (49 loc) · 1.34 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
package main
import (
"net/http"
"golang.org/x/crypto/acme/autocert"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
func main() {
cfg := getConfig()
e := echo.New()
e.Use(middleware.CORS())
e.GET("/", handleRequest)
if cfg.TLS {
autoTLSManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
Cache: autocert.DirCache(cfg.CertCache),
HostPolicy: autocert.HostWhitelist(cfg.Hostnames...),
}
httpServer := &http.Server{
Handler: autoTLSManager.HTTPHandler(e),
Addr: ":80",
}
go httpServer.ListenAndServe()
s := http.Server{
Addr: cfg.Bind,
Handler: e,
TLSConfig: autoTLSManager.TLSConfig(),
}
if err := s.ListenAndServeTLS("", ""); err != http.ErrServerClosed {
e.Logger.Fatal(err)
}
} else {
e.Logger.Fatal(e.Start(cfg.Bind))
}
}
//IP is just a tring but called IP for XML conversion
type IP string
func handleRequest(c echo.Context) error {
if c.QueryParam("format") == "json" {
return c.JSON(http.StatusOK, map[string]string{"ip": c.RealIP()})
}
if c.QueryParam("format") == "jsonp" && c.QueryParam("callback") != "" {
return c.JSONP(http.StatusOK, c.QueryParam("callback"), map[string]string{"ip": c.RealIP()})
}
if c.QueryParam("format") == "xml" {
return c.XML(http.StatusOK, IP(c.RealIP()))
}
return c.String(http.StatusOK, c.RealIP())
}