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"
|
|
|
|
"os/signal"
|
|
|
|
|
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-07 23:41:00 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/db"
|
2024-04-09 14:30:54 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/db/queries"
|
2024-04-07 12:11:25 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/pkg/log"
|
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-07 23:41:00 +07:00
|
|
|
db, err := db.Open(cfg)
|
|
|
|
if err != nil {
|
2024-04-08 15:48:45 +07:00
|
|
|
log.New(cmd.Context()).Err(err).Error("failed to connect database")
|
2024-04-07 23:41:00 +07:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2024-04-09 14:30:54 +07:00
|
|
|
queries := queries.New(db)
|
2024-04-07 23:41:00 +07:00
|
|
|
|
|
|
|
api := &api.API{
|
2024-04-09 14:30:54 +07:00
|
|
|
Queries: queries,
|
|
|
|
DB: db,
|
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
|
|
|
|
|
|
|
exit := make(chan struct{}, 1)
|
2024-04-07 12:11:25 +07:00
|
|
|
|
2024-04-07 16:06:33 +07:00
|
|
|
go func() {
|
|
|
|
sig := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sig, os.Interrupt)
|
|
|
|
<-sig
|
|
|
|
exit <- struct{}{}
|
|
|
|
}()
|
2024-04-07 12:11:25 +07:00
|
|
|
|
2024-04-07 16:06:33 +07:00
|
|
|
if err := server.Start(exit); 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)
|
|
|
|
}
|