-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
106 lines (95 loc) · 2.91 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
102
103
104
105
106
package main
import (
"bufio"
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/rpsoftech/whatsapp-http-api/apis"
"github.com/rpsoftech/whatsapp-http-api/env"
"github.com/rpsoftech/whatsapp-http-api/interfaces"
"github.com/rpsoftech/whatsapp-http-api/middleware"
"github.com/rpsoftech/whatsapp-http-api/whatsapp"
)
var version string
// var app *fiber.App
func main() {
println(version)
if time.Now().Unix() > 1734512128 {
println("Please Update The Binary From Keyur Shah")
println("Press Any Key To Close")
input := bufio.NewScanner(os.Stdin)
input.Scan()
return
}
// args := os.Args
// if !slices.Contains(args, "--dev") && !slices.Contains(args, "--prod") {
// cmd := exec.Command(filepath.Join(FindAndReturnCurrentDir(), os.Args[0]), "--prod")
// cmd.Stdout = os.Stdout
// err := cmd.Start()
// if err != nil {
// log.Fatal(err)
// }
// log.Printf("Just ran subprocess %d, exiting\n", cmd.Process.Pid)
// // time.Sleep(5 * time.Second)
// return
// }
go func() {
os.RemoveAll("./tmp")
os.Mkdir("./tmp", 0777)
}()
outputLogFolderDir := filepath.Join(env.CurrentDirectory, "whatsapp_server_logs")
if _, err := os.Stat(outputLogFolderDir); errors.Is(err, os.ErrNotExist) {
os.MkdirAll(outputLogFolderDir, 0777)
}
whatsapp.OutPutFilePath = ReturnOutPutFilePath(env.CurrentDirectory)
whatsapp.InitSqlContainer()
if env.Env.AUTO_CONNECT_TO_WHATSAPP {
go func() {
for k := range env.ServerConfig.Tokens {
jidString := env.ServerConfig.JID[k]
whatsapp.ConnectToNumber(jidString, k)
}
}()
}
InitFiberServer()
}
func InitFiberServer() {
app := fiber.New(fiber.Config{
BodyLimit: 200 * 1024 * 1024,
ErrorHandler: func(c *fiber.Ctx, err error) error {
mappedError, ok := err.(*interfaces.RequestError)
if !ok {
println(err.Error())
return c.Status(500).JSON(interfaces.RequestError{
Code: interfaces.ERROR_INTERNAL_SERVER,
Message: "Some Internal Error",
Name: "Global Error Handler Function",
})
}
return c.Status(mappedError.StatusCode).JSON(mappedError)
},
})
app.Use(logger.New())
app.Static("/swagger", "./swagger")
apis.AddApis(app.Group("/v1", middleware.TokenDecrypter, middleware.AllowOnlyValidTokenMiddleWare))
app.Get("/scan/:id", apis.OpenBrowserWithQr)
app.Use(func(c *fiber.Ctx) error {
return c.Status(fiber.StatusNotFound).SendString("Sorry can't find that!")
})
hostAndPort := ""
if env.Env.APP_ENV == env.APP_ENV_LOCAL || env.Env.APP_ENV == env.APP_ENV_DEVELOPE {
hostAndPort = "127.0.0.1"
}
hostAndPort = hostAndPort + ":" + strconv.Itoa(env.Env.PORT)
app.Listen(hostAndPort)
}
func ReturnOutPutFilePath(currentDir string) string {
t := time.Now()
today := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, t.Nanosecond(), t.Location()).Unix()
return filepath.Join(currentDir, fmt.Sprintf("%d.log.csv", today))
}