74 lines
1.8 KiB
Go
Raw Permalink Normal View History

2023-10-13 14:37:41 +03:00
package main
import (
"os"
"github.com/Neur0toxine/wa-profile-api/internal/log"
"github.com/Neur0toxine/wa-profile-api/internal/middleware"
"github.com/Neur0toxine/wa-profile-api/internal/proxy"
"github.com/Neur0toxine/wa-profile-api/internal/waapi"
"github.com/go-playground/validator/v10"
"github.com/iris-contrib/swagger"
"github.com/iris-contrib/swagger/swaggerFiles"
"github.com/kataras/iris/v12"
_ "github.com/Neur0toxine/wa-profile-api/docs"
)
const (
EnvPort = "PORT"
EnvUseProxy = "USE_PROXY"
)
func registerRoutes(c *iris.APIContainer) {
log.Debug("initializing main route & proxies")
proxyProvider := proxy.NewDirectProvider()
if os.Getenv(EnvUseProxy) == "1" {
proxyProvider = proxy.NewRandomListProvider()
}
waapiClient := waapi.NewClient(proxyProvider)
c.Use(func(c iris.Context) {
c.Values().Set(proxy.ProviderKey, proxyProvider)
c.Values().Set(waapi.ClientKey, waapiClient)
c.Next()
})
c.Get("/api/v1/profile", middleware.GetProfileHandler)
log.Debug("done")
}
func registerAPIDocs(c *iris.APIContainer) {
log.Debug("initializing API docs...")
swaggerUI := swagger.Handler(swaggerFiles.Handler,
swagger.URL("/docs/swagger.json"),
swagger.DeepLinking(true),
swagger.Prefix("/docs"),
)
c.Get("/docs", swaggerUI)
c.Get("/docs/{any:path}", swaggerUI)
log.Debug("API documentation will be available at /docs")
}
// @title WhatsApp Profile API
// @version 1.0
// @description This API can help you get avatars and profile names using WhatsApp phone number.
// @BasePath /api/v1
func main() {
app := iris.New()
if !log.IsDebug {
app.Configure(iris.WithoutStartupLog)
}
app.Use(iris.Compression)
app.Validator = validator.New()
app.ConfigureContainer(registerAPIDocs, registerRoutes)
port := os.Getenv(EnvPort)
if port == "" {
port = "8090"
}
log.Debug("running the app now...")
app.Run(iris.Addr(":" + port))
}