2024-04-06 01:22:00 +07:00
|
|
|
package cli
|
|
|
|
|
2024-04-07 12:11:25 +07:00
|
|
|
import (
|
2024-04-09 14:30:54 +07:00
|
|
|
"io/fs"
|
2024-04-07 16:06:33 +07:00
|
|
|
"os"
|
|
|
|
|
2024-04-07 12:11:25 +07:00
|
|
|
"github.com/spf13/cobra"
|
2024-04-09 14:30:54 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/api"
|
2024-04-14 00:32:55 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/api/reddit"
|
2024-04-07 23:41:00 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/db"
|
2024-04-07 12:11:25 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/pkg/log"
|
2024-05-03 21:02:31 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/pkg/pubsub"
|
2024-04-12 11:37:07 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/pkg/telemetry"
|
2024-04-07 16:06:33 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/server"
|
2024-04-07 12:11:25 +07:00
|
|
|
)
|
2024-04-06 01:22:00 +07:00
|
|
|
|
2024-04-09 14:30:54 +07:00
|
|
|
var PublicDir fs.FS = os.DirFS("public")
|
|
|
|
|
2024-04-06 01:22:00 +07:00
|
|
|
var serveCmd = &cobra.Command{
|
|
|
|
Use: "serve",
|
|
|
|
Short: "Starts the HTTP Server",
|
|
|
|
SilenceUsage: true,
|
2024-04-07 16:06:33 +07:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2024-04-13 00:15:31 +07:00
|
|
|
tele, err := telemetry.New(cmd.Context(), cfg)
|
|
|
|
if err != nil {
|
|
|
|
log.New(cmd.Context()).Err(err).Error("failed to start telemetry")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-04-12 11:37:07 +07:00
|
|
|
defer tele.Close()
|
|
|
|
|
2024-04-25 13:05:41 +07:00
|
|
|
database, err := db.Open(cfg)
|
2024-04-07 23:41:00 +07:00
|
|
|
if err != nil {
|
2024-04-27 15:16:14 +07:00
|
|
|
log.New(cmd.Context()).Err(err).Error("failed to open connection to database")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2024-05-22 15:16:07 +07:00
|
|
|
pubsubDB, err := pubsub.NewSqlite(cfg)
|
2024-04-27 15:16:14 +07:00
|
|
|
if err != nil {
|
|
|
|
log.New(cmd.Context()).Err(err).Error("failed to open connection to pubsub database")
|
2024-04-07 23:41:00 +07:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2024-05-22 15:16:07 +07:00
|
|
|
publisher, err := pubsub.NewSQLPublisher(pubsubDB)
|
2024-05-03 21:02:31 +07:00
|
|
|
if err != nil {
|
|
|
|
log.New(cmd.Context()).Err(err).Error("failed to create publisher")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-05-22 15:16:07 +07:00
|
|
|
subscriber, err := pubsub.NewSQLSubscriber(cfg, pubsubDB)
|
2024-05-03 21:02:31 +07:00
|
|
|
if err != nil {
|
|
|
|
log.New(cmd.Context()).Err(err).Error("failed to create subscriber")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2024-04-27 15:16:14 +07:00
|
|
|
loggedDb := db.ApplyLogger(cfg, database)
|
2024-04-25 13:05:41 +07:00
|
|
|
if err != nil {
|
|
|
|
log.New(cmd.Context()).Err(err).Error("failed to connect database")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-04-14 00:32:55 +07:00
|
|
|
red := &reddit.Reddit{
|
2024-04-27 15:16:14 +07:00
|
|
|
Client: reddit.NewRedditHTTPClient(cfg),
|
2024-04-14 00:32:55 +07:00
|
|
|
Config: cfg,
|
|
|
|
}
|
|
|
|
|
|
|
|
api := api.New(api.Dependencies{
|
2024-05-03 21:02:31 +07:00
|
|
|
DB: loggedDb,
|
|
|
|
Config: cfg,
|
|
|
|
Reddit: red,
|
|
|
|
Publisher: publisher,
|
|
|
|
Subscriber: subscriber,
|
2024-04-14 00:32:55 +07:00
|
|
|
})
|
2024-04-07 23:41:00 +07:00
|
|
|
|
2024-04-09 14:30:54 +07:00
|
|
|
server := server.New(cfg, api, PublicDir)
|
2024-04-07 16:06:33 +07:00
|
|
|
|
2024-04-12 01:32:06 +07:00
|
|
|
if err := server.Start(cmd.Context().Done()); err != nil {
|
2024-04-08 15:48:45 +07:00
|
|
|
log.New(cmd.Context()).Err(err).Error("failed to start server")
|
2024-04-07 16:06:33 +07:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-04-06 01:22:00 +07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RootCmd.AddCommand(serveCmd)
|
|
|
|
}
|