whatismyip/cmd/whatismyip.go

58 lines
1.3 KiB
Go
Raw Normal View History

2021-11-10 19:06:12 +00:00
package main
import (
"context"
"flag"
"fmt"
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"
"github.com/gin-contrib/secure"
2021-11-10 19:06:12 +00:00
"github.com/dcarrillo/whatismyip/models"
"github.com/dcarrillo/whatismyip/router"
"github.com/gin-gonic/gin"
)
func main() {
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)
os.Exit(1)
}
2021-11-10 19:06:12 +00:00
models.Setup(setting.App.GeodbPath.City, setting.App.GeodbPath.ASN)
2023-03-20 15:33:45 +00:00
engine := setupEngine()
2021-11-10 19:06:12 +00:00
router.SetupTemplate(engine)
router.Setup(engine)
2023-03-20 15:33:45 +00:00
whatismyip := server.Setup(context.Background(), engine.Handler())
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())
if setting.App.EnableSecureHeaders {
engine.Use(secure.New(secure.Config{
BrowserXssFilter: true,
ContentTypeNosniff: true,
FrameDeny: true,
}))
}
_ = engine.SetTrustedProxies(nil)
engine.TrustedPlatform = setting.App.TrustedHeader
2023-03-20 15:33:45 +00:00
return engine
}