Browse Source

feat: add custom port & debugging

pull/13/head
Aldino Kemal 4 years ago
parent
commit
f4043c4269
  1. 96
      src/cmd/root.go
  2. 7
      src/config/settings.go
  3. 3
      src/go.mod
  4. 4
      src/go.sum
  5. 65
      src/main.go
  6. 8
      src/utils/whatsapp.go

96
src/cmd/root.go

@ -0,0 +1,96 @@
package cmd
import (
"fmt"
"github.com/aldinokemal/go-whatsapp-web-multidevice/config"
"github.com/aldinokemal/go-whatsapp-web-multidevice/controllers"
"github.com/aldinokemal/go-whatsapp-web-multidevice/middleware"
"github.com/aldinokemal/go-whatsapp-web-multidevice/services"
"github.com/aldinokemal/go-whatsapp-web-multidevice/utils"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/template/html"
"github.com/markbates/pkger"
_ "github.com/mattn/go-sqlite3"
"log"
"os"
"github.com/spf13/cobra"
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Short: "Send free whatsapp API",
Long: `This application is from clone https://github.com/aldinokemal/go-whatsapp-web-multidevice,
you can send whatsapp over http api but your whatsapp account have to multi device version`,
Run: runRest,
}
func init() {
rootCmd.CompletionOptions.DisableDefaultCmd = true
rootCmd.PersistentFlags().StringVarP(&config.AppPort, "port", "p", config.AppPort, "change port number with --port <number> | example: --port=8080")
rootCmd.PersistentFlags().BoolVarP(&config.AppDebug, "debug", "d", config.AppDebug, "hide or displaying log with --debug <true/false> | example: --debug=true")
}
func runRest(cmd *cobra.Command, args []string) {
if config.AppDebug {
config.WhatsappLogLevel = "DEBUG"
}
// TODO: Init Rest App
//preparing folder if not exist
err := utils.CreateFolder(config.PathQrCode, config.PathSendItems)
if err != nil {
log.Fatalln(err)
}
engine := html.NewFileSystem(pkger.Dir("/views"), ".html")
app := fiber.New(fiber.Config{
Views: engine,
BodyLimit: 10 * 1024 * 1024,
})
app.Static("/statics", "./statics")
app.Use(middleware.Recovery())
if config.AppDebug {
app.Use(logger.New())
}
app.Use(cors.New(cors.Config{
AllowOrigins: "*",
AllowHeaders: "Origin, Content-Type, Accept",
}))
db := utils.InitWaDB()
cli := utils.InitWaCLI(db)
// Service
appService := services.NewAppService(cli)
sendService := services.NewSendService(cli)
userService := services.NewUserService(cli)
// Controller
appController := controllers.NewAppController(appService)
sendController := controllers.NewSendController(sendService)
userController := controllers.NewUserController(userService)
appController.Route(app)
sendController.Route(app)
userController.Route(app)
app.Get("/", func(ctx *fiber.Ctx) error {
return ctx.Render("index", fiber.Map{"AppHost": fmt.Sprintf("%s://%s", ctx.Protocol(), ctx.Hostname())})
})
err = app.Listen(":" + config.AppPort)
if err != nil {
log.Fatalln("Failed to start: ", err.Error())
}
}
// Execute adds all child commands to the root command and sets flags appropriately.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}

7
src/config/settings.go

@ -2,11 +2,14 @@ package config
type Browser string
const (
AppPort string = "3000"
var (
AppPort string = "3000"
AppDebug bool = false
PathQrCode string = "statics/images/qrcode"
PathSendItems string = "statics/images/senditems"
DBName string = "hydrogenWaCli.db"
WhatsappLogLevel string = "ERROR"
)

3
src/go.mod

@ -10,6 +10,7 @@ require (
github.com/markbates/pkger v0.17.1
github.com/mattn/go-sqlite3 v1.14.11
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
github.com/spf13/cobra v1.4.0
github.com/stretchr/testify v1.7.0
go.mau.fi/whatsmeow v0.0.0-20220514092657-a05359d4385a
google.golang.org/protobuf v1.28.0
@ -22,8 +23,10 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gobuffalo/here v0.6.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/klauspost/compress v1.14.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.33.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect

4
src/go.sum

@ -253,6 +253,7 @@ github.com/hashicorp/serf v0.9.6/go.mod h1:TXZNMjZQijwlDvp+r0b63xZ45H7JmCmgg4gpT
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
@ -349,7 +350,10 @@ github.com/spf13/afero v1.3.3/go.mod h1:5KUK8ByomD5Ti5Artl0RtHeI5pTF7MIDuXL3yY52
github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
github.com/spf13/cast v1.4.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v1.3.0/go.mod h1:BrRVncBjOJa/eUcVVm9CE+oC6as8k+VYr4NY7WCi9V4=
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.10.0/go.mod h1:SoyBPwAtKDzypXNDFKN5kzH7ppppbGZtls1UpIy5AsM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

65
src/main.go

@ -1,64 +1,11 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package main
import (
"fmt"
"github.com/aldinokemal/go-whatsapp-web-multidevice/config"
"github.com/aldinokemal/go-whatsapp-web-multidevice/controllers"
"github.com/aldinokemal/go-whatsapp-web-multidevice/middleware"
"github.com/aldinokemal/go-whatsapp-web-multidevice/services"
"github.com/aldinokemal/go-whatsapp-web-multidevice/utils"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/template/html"
"github.com/markbates/pkger"
_ "github.com/mattn/go-sqlite3"
"log"
)
import "github.com/aldinokemal/go-whatsapp-web-multidevice/cmd"
func main() {
// preparing folder if not exist
err := utils.CreateFolder(config.PathQrCode, config.PathSendItems)
if err != nil {
log.Fatalln(err)
}
engine := html.NewFileSystem(pkger.Dir("/views"), ".html")
app := fiber.New(fiber.Config{
Views: engine,
BodyLimit: 10 * 1024 * 1024,
})
app.Static("/statics", "./statics")
app.Use(middleware.Recovery())
app.Use(logger.New())
app.Use(cors.New(cors.Config{
AllowOrigins: "*",
AllowHeaders: "Origin, Content-Type, Accept",
}))
db := utils.InitWaDB()
cli := utils.InitWaCLI(db)
// Service
appService := services.NewAppService(cli)
sendService := services.NewSendService(cli)
userService := services.NewUserService(cli)
// Controller
appController := controllers.NewAppController(appService)
sendController := controllers.NewSendController(sendService)
userController := controllers.NewUserController(userService)
appController.Route(app)
sendController.Route(app)
userController.Route(app)
app.Get("/", func(ctx *fiber.Ctx) error {
return ctx.Render("index", fiber.Map{"AppHost": fmt.Sprintf("%s://%s", ctx.Protocol(), ctx.Hostname())})
})
err = app.Listen(":" + config.AppPort)
if err != nil {
log.Fatalln("Failed to start: ", err.Error())
}
cmd.Execute()
}

8
src/utils/whatsapp.go

@ -27,8 +27,6 @@ var (
startupTime = time.Now().Unix()
)
const logLevel = "DEBUG"
func GetPlatformName(deviceID int) string {
switch deviceID {
case 2:
@ -79,8 +77,8 @@ func ParseJID(arg string) (types.JID, bool) {
func InitWaDB() *sqlstore.Container {
// Running Whatsapp
log = waLog.Stdout("Main", logLevel, true)
dbLog := waLog.Stdout("Database", logLevel, true)
log = waLog.Stdout("Main", config.WhatsappLogLevel, true)
dbLog := waLog.Stdout("Database", config.WhatsappLogLevel, true)
storeContainer, err := sqlstore.New("sqlite3", fmt.Sprintf("file:%s?_foreign_keys=off", config.DBName), dbLog)
if err != nil {
log.Errorf("Failed to connect to database: %v", err)
@ -99,7 +97,7 @@ func InitWaCLI(storeContainer *sqlstore.Container) *whatsmeow.Client {
store.CompanionProps.PlatformType = waProto.CompanionProps_CHROME.Enum()
store.CompanionProps.Os = proto.String("AldinoKemal")
cli = whatsmeow.NewClient(device, waLog.Stdout("Client", logLevel, true))
cli = whatsmeow.NewClient(device, waLog.Stdout("Client", config.WhatsappLogLevel, true))
cli.AddEventHandler(handler)
return cli

Loading…
Cancel
Save