2021-11-10 19:06:12 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-11-29 17:16:27 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2024-03-23 16:41:34 +00:00
|
|
|
"net/http"
|
2021-11-10 19:06:12 +00:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/dcarrillo/whatismyip/internal/httputils"
|
|
|
|
"github.com/dcarrillo/whatismyip/internal/setting"
|
2023-03-20 15:33:45 +00:00
|
|
|
"github.com/dcarrillo/whatismyip/server"
|
2022-05-07 13:56:21 +00:00
|
|
|
"github.com/gin-contrib/secure"
|
2022-05-07 10:40:25 +00:00
|
|
|
|
2021-11-10 19:06:12 +00:00
|
|
|
"github.com/dcarrillo/whatismyip/router"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2021-11-29 17:16:27 +00:00
|
|
|
o, err := setting.Setup(os.Args[1:])
|
|
|
|
if err == flag.ErrHelp || err == setting.ErrVersion {
|
|
|
|
fmt.Print(o)
|
|
|
|
os.Exit(0)
|
|
|
|
} else if err != nil {
|
2023-03-16 18:59:04 +00:00
|
|
|
fmt.Println(err)
|
2021-11-29 17:16:27 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2023-03-20 15:33:45 +00:00
|
|
|
engine := setupEngine()
|
2021-11-10 19:06:12 +00:00
|
|
|
router.SetupTemplate(engine)
|
|
|
|
router.Setup(engine)
|
2024-03-23 16:41:34 +00:00
|
|
|
servers := setupHTTPServers(context.Background(), engine.Handler())
|
2021-11-10 19:06:12 +00:00
|
|
|
|
2024-03-23 16:41:34 +00:00
|
|
|
whatismyip := server.Setup(servers)
|
2023-03-20 15:33:45 +00:00
|
|
|
whatismyip.Run()
|
2021-11-10 19:06:12 +00:00
|
|
|
}
|
|
|
|
|
2023-03-20 15:33:45 +00:00
|
|
|
func setupEngine() *gin.Engine {
|
2021-11-10 19:06:12 +00:00
|
|
|
gin.DisableConsoleColor()
|
|
|
|
if os.Getenv(gin.EnvGinMode) == "" {
|
|
|
|
gin.SetMode(gin.ReleaseMode)
|
|
|
|
}
|
2023-03-20 15:33:45 +00:00
|
|
|
engine := gin.New()
|
2021-11-10 19:06:12 +00:00
|
|
|
engine.Use(gin.LoggerWithFormatter(httputils.GetLogFormatter))
|
|
|
|
engine.Use(gin.Recovery())
|
2022-04-02 16:10:48 +00:00
|
|
|
if setting.App.EnableSecureHeaders {
|
2022-05-07 13:56:21 +00:00
|
|
|
engine.Use(secure.New(secure.Config{
|
2022-04-02 16:10:48 +00:00
|
|
|
BrowserXssFilter: true,
|
|
|
|
ContentTypeNosniff: true,
|
|
|
|
FrameDeny: true,
|
2022-05-07 10:40:25 +00:00
|
|
|
}))
|
2022-04-02 16:10:48 +00:00
|
|
|
}
|
2022-05-07 10:40:25 +00:00
|
|
|
_ = engine.SetTrustedProxies(nil)
|
|
|
|
engine.TrustedPlatform = setting.App.TrustedHeader
|
2023-03-20 15:33:45 +00:00
|
|
|
|
|
|
|
return engine
|
2022-04-02 16:10:48 +00:00
|
|
|
}
|
2024-03-23 16:41:34 +00:00
|
|
|
|
|
|
|
func setupHTTPServers(ctx context.Context, handler http.Handler) []server.Server {
|
|
|
|
var servers []server.Server
|
|
|
|
|
|
|
|
if setting.App.BindAddress != "" {
|
|
|
|
tcpServer := server.NewTCPServer(ctx, &handler)
|
|
|
|
servers = append(servers, tcpServer)
|
|
|
|
}
|
|
|
|
|
|
|
|
if setting.App.TLSAddress != "" {
|
|
|
|
tlsServer := server.NewTLSServer(ctx, &handler)
|
|
|
|
servers = append(servers, tlsServer)
|
|
|
|
if setting.App.EnableHTTP3 {
|
|
|
|
quicServer := server.NewQuicServer(ctx, tlsServer)
|
|
|
|
servers = append(servers, quicServer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return servers
|
|
|
|
}
|