From 05bdca538b304f55447575bdea2dfe3bbb723729 Mon Sep 17 00:00:00 2001 From: Dhruv Shah Date: Mon, 5 Feb 2024 12:56:03 +0530 Subject: [PATCH] refactor: cli and web apps --- .gitignore | 5 +- vitty-backend-api/api/initialize.go | 26 ++- vitty-backend-api/cli/commands/initialize.go | 10 ++ .../commands}/timetableCommands.go | 2 +- .../cli/commands/userCommands.go | 149 ++++++++++++++++++ vitty-backend-api/cli/initialize.go | 28 ++++ .../cmd/management/createSuperuser.go | 30 ---- .../cmd/management/deleteUser.go | 51 ------ vitty-backend-api/cmd/management/getUsers.go | 55 ------- vitty-backend-api/cmd/{cliApp.go => root.go} | 102 ++++++------ 10 files changed, 259 insertions(+), 199 deletions(-) create mode 100644 vitty-backend-api/cli/commands/initialize.go rename vitty-backend-api/{cmd/management => cli/commands}/timetableCommands.go (98%) create mode 100644 vitty-backend-api/cli/commands/userCommands.go create mode 100644 vitty-backend-api/cli/initialize.go delete mode 100644 vitty-backend-api/cmd/management/createSuperuser.go delete mode 100644 vitty-backend-api/cmd/management/deleteUser.go delete mode 100644 vitty-backend-api/cmd/management/getUsers.go rename vitty-backend-api/cmd/{cliApp.go => root.go} (53%) diff --git a/.gitignore b/.gitignore index 471406f..8dd092a 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,7 @@ .db data credentials -test/ \ No newline at end of file +test/ +backups/ +prod/ +.DS_Store \ No newline at end of file diff --git a/vitty-backend-api/api/initialize.go b/vitty-backend-api/api/initialize.go index 65b388d..493adec 100644 --- a/vitty-backend-api/api/initialize.go +++ b/vitty-backend-api/api/initialize.go @@ -4,23 +4,41 @@ import ( v1 "github.com/GDGVIT/vitty-backend/vitty-backend-api/api/v1" v2 "github.com/GDGVIT/vitty-backend/vitty-backend-api/api/v2" "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/cors" + "github.com/gofiber/fiber/v2/middleware/logger" ) -func InitializeApi(f *fiber.App) { +func NewWebApi() *fiber.App { + // New fiber app + fiberApp := fiber.New() + + fiberApp = fiber.New() + fiberApp.Use(logger.New()) + fiberApp.Use(cors.New( + cors.Config{ + AllowOrigins: "*", + AllowHeaders: "*", + AllowCredentials: true, + AllowMethods: "GET,POST,DELETE,PATCH,PUT,OPTIONS", + }, + )) + // Root endpoint - f.Get("/", func(c *fiber.Ctx) error { + fiberApp.Get("/", func(c *fiber.Ctx) error { return c.Status(fiber.StatusOK).SendString("Welcome to VITTY API!🎉") }) // Ping endpoint - f.Get("/ping", func(c *fiber.Ctx) error { + fiberApp.Get("/ping", func(c *fiber.Ctx) error { return c.Status(fiber.StatusOK).JSON( fiber.Map{ "detail": "pong", }) }) - api := f.Group("/api") + api := fiberApp.Group("/api") v1.V1Handler(api) v2.V2Handler(api) + + return fiberApp } diff --git a/vitty-backend-api/cli/commands/initialize.go b/vitty-backend-api/cli/commands/initialize.go new file mode 100644 index 0000000..cde1341 --- /dev/null +++ b/vitty-backend-api/cli/commands/initialize.go @@ -0,0 +1,10 @@ +package commands + +import "github.com/urfave/cli/v2" + +// AddCommands adds the commands to the app +func AddCommands(cliApp *cli.App) { + // Add the commands to the app + cliApp.Commands = append(cliApp.Commands, userCommands...) + cliApp.Commands = append(cliApp.Commands, TimetableCommands...) +} diff --git a/vitty-backend-api/cmd/management/timetableCommands.go b/vitty-backend-api/cli/commands/timetableCommands.go similarity index 98% rename from vitty-backend-api/cmd/management/timetableCommands.go rename to vitty-backend-api/cli/commands/timetableCommands.go index efa01a3..2ae3fc2 100644 --- a/vitty-backend-api/cmd/management/timetableCommands.go +++ b/vitty-backend-api/cli/commands/timetableCommands.go @@ -1,4 +1,4 @@ -package management +package commands import ( "fmt" diff --git a/vitty-backend-api/cli/commands/userCommands.go b/vitty-backend-api/cli/commands/userCommands.go new file mode 100644 index 0000000..6688ae3 --- /dev/null +++ b/vitty-backend-api/cli/commands/userCommands.go @@ -0,0 +1,149 @@ +package commands + +import ( + "errors" + "fmt" + + "github.com/GDGVIT/vitty-backend/vitty-backend-api/internal/database" + "github.com/GDGVIT/vitty-backend/vitty-backend-api/internal/models" + "github.com/GDGVIT/vitty-backend/vitty-backend-api/internal/utils" + "github.com/urfave/cli/v2" +) + +var userCommands = []*cli.Command{ + { + Name: "createsuperuser", + Aliases: []string{"csu"}, + Usage: "Create a superuser", + Action: createSuperuser, + }, + { + Name: "createadminuser", + Aliases: []string{"cau"}, + Usage: "Create an admin user", + Action: createAdminuser, + }, + { + Name: "getusers", + Aliases: []string{"gu"}, + Usage: "Get users", + Action: getUsers, + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "filter", + Aliases: []string{"f"}, + Usage: "Filter users by", + Required: false, + }, + &cli.StringFlag{ + Name: "value", + Aliases: []string{"v"}, + Usage: "Value of filter", + Required: false, + }, + }, + }, + { + Name: "deleteuser", + Aliases: []string{"du"}, + Usage: "Delete user", + Action: deleteUser, + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "key", + Aliases: []string{"k"}, + Usage: "Key of user to delete", + Required: false, + }, + &cli.StringFlag{ + Name: "value", + Aliases: []string{"v"}, + Usage: "Value of key", + Required: false, + }, + }, + }, +} + +func createSuperuser(c *cli.Context) error { + fmt.Println("Enter username: ") + var username string + fmt.Scanln(&username) + + if !utils.CheckUserExists(username) { + return errors.New("user does not exist") + } + user := utils.GetUserByUsername(username) + user.Role = "superuser" + err := database.DB.Save(&user).Error + if err != nil { + return err + } + fmt.Println("Superuser created successfully") + return nil +} + +func createAdminuser(c *cli.Context) error { + fmt.Println("Enter username: ") + var username string + fmt.Scanln(&username) + + if !utils.CheckUserExists(username) { + return errors.New("user does not exist") + } + user := utils.GetUserByUsername(username) + user.Role = "admin" + err := database.DB.Save(&user).Error + if err != nil { + return err + } + fmt.Println("Admin user created successfully") + return nil +} + +func getUsers(c *cli.Context) error { + // Take arguements as name of filter and value of filter + // If no arguements are given, return all users + // If arguements are given, return users that match the filter + + filter := c.String("filter") + value := c.String("value") + + var users []models.User + filter_query := fmt.Sprintf("%s = ?", filter) + + if filter == "" || value == "" { + database.DB.Find(&users) + } else { + database.DB.Where(filter_query, value).Find(&users) + } + + // Display Users in a table + fmt.Println("ID\tUsername\t\tRole\tRegNo\tEmail\tPicture") + for _, user := range users { + fmt.Printf("\t%s\t\t%s\t\t%s\t\t%s\t\t%s\n", user.Username, user.Role, user.RegNo, user.Email, user.Picture) + } + return nil +} + +func deleteUser(c *cli.Context) error { + key := c.String("key") + value := c.String("value") + + if key == "" || value == "" { + fmt.Println("Enter key: ") + fmt.Scanln(&key) + fmt.Println("Enter value: ") + fmt.Scanln(&value) + } + + var users models.User + filter_query := fmt.Sprintf("%s = ?", key) + err := database.DB.Where(filter_query, value).Delete(&users).Error + if err != nil { + fmt.Printf("Error: %s\n", err) + return err + } + fmt.Println("User deleted successfully") + return nil +} diff --git a/vitty-backend-api/cli/initialize.go b/vitty-backend-api/cli/initialize.go new file mode 100644 index 0000000..83effb9 --- /dev/null +++ b/vitty-backend-api/cli/initialize.go @@ -0,0 +1,28 @@ +package vittyCli + +import ( + "github.com/GDGVIT/vitty-backend/vitty-backend-api/cli/commands" + "github.com/urfave/cli/v2" +) + +func NewCliApp() *cli.App { + // New cli app + cliApp := cli.NewApp() + + // Set the name, usage and version of the app + cliApp.Name = "Vitty" + cliApp.Usage = "Vitty Backend API" + cliApp.Version = "0.0.1" + cliApp.Authors = []*cli.Author{ + { + Name: "Dhruv Shah", + Email: "dhruvshahrds@gmail.com", + }} + cliApp.EnableBashCompletion = true + cliApp.Description = "CLI for Vitty Backend API" + + // Set the commands + commands.AddCommands(cliApp) + + return cliApp +} diff --git a/vitty-backend-api/cmd/management/createSuperuser.go b/vitty-backend-api/cmd/management/createSuperuser.go deleted file mode 100644 index 863b278..0000000 --- a/vitty-backend-api/cmd/management/createSuperuser.go +++ /dev/null @@ -1,30 +0,0 @@ -package management - -import ( - "errors" - "fmt" - - "github.com/GDGVIT/vitty-backend/vitty-backend-api/internal/database" - "github.com/GDGVIT/vitty-backend/vitty-backend-api/internal/utils" - "github.com/urfave/cli/v2" -) - -func createSuperuser(c *cli.Context) error { - fmt.Println("Enter username: ") - var username string - fmt.Scanln(&username) - - if !utils.CheckUserExists(username) { - return errors.New("user does not exist") - } - user := utils.GetUserByUsername(username) - user.Role = "superuser" - return database.DB.Save(&user).Error -} - -var CreateSuperuserCommand = cli.Command{ - Name: "createsuperuser", - Aliases: []string{"csu"}, - Usage: "Create a superuser", - Action: createSuperuser, -} diff --git a/vitty-backend-api/cmd/management/deleteUser.go b/vitty-backend-api/cmd/management/deleteUser.go deleted file mode 100644 index 88cf3d6..0000000 --- a/vitty-backend-api/cmd/management/deleteUser.go +++ /dev/null @@ -1,51 +0,0 @@ -package management - -import ( - "fmt" - - "github.com/GDGVIT/vitty-backend/vitty-backend-api/internal/database" - "github.com/GDGVIT/vitty-backend/vitty-backend-api/internal/models" - "github.com/urfave/cli/v2" -) - -func deleteUser(c *cli.Context) error { - key := c.String("key") - value := c.String("value") - - if key == "" || value == "" { - fmt.Println("Enter key: ") - fmt.Scanln(&key) - fmt.Println("Enter value: ") - fmt.Scanln(&value) - } - - var users models.User - filter_query := fmt.Sprintf("%s = ?", key) - err := database.DB.Where(filter_query, value).Delete(&users).Error - if err != nil { - fmt.Printf("Error: %s\n", err) - return err - } - return nil -} - -var DeleteUserCommand = cli.Command{ - Name: "deleteuser", - Aliases: []string{"du"}, - Usage: "Delete user", - Action: deleteUser, - Flags: []cli.Flag{ - &cli.StringFlag{ - Name: "key", - Aliases: []string{"k"}, - Usage: "Key of user to delete", - Required: false, - }, - &cli.StringFlag{ - Name: "value", - Aliases: []string{"v"}, - Usage: "Value of key", - Required: false, - }, - }, -} diff --git a/vitty-backend-api/cmd/management/getUsers.go b/vitty-backend-api/cmd/management/getUsers.go deleted file mode 100644 index 0d04e25..0000000 --- a/vitty-backend-api/cmd/management/getUsers.go +++ /dev/null @@ -1,55 +0,0 @@ -package management - -import ( - "fmt" - - "github.com/GDGVIT/vitty-backend/vitty-backend-api/internal/database" - "github.com/GDGVIT/vitty-backend/vitty-backend-api/internal/models" - "github.com/urfave/cli/v2" -) - -func getUsers(c *cli.Context) error { - // Take arguements as name of filter and value of filter - // If no arguements are given, return all users - // If arguements are given, return users that match the filter - - filter := c.String("filter") - value := c.String("value") - - var users []models.User - filter_query := fmt.Sprintf("%s = ?", filter) - - if filter == "" || value == "" { - database.DB.Find(&users) - } else { - database.DB.Where(filter_query, value).Find(&users) - } - - // Display Users in a table - fmt.Println("ID\tUsername\t\tRole\tRegNo\tEmail\tPicture") - for _, user := range users { - fmt.Printf("\t%s\t\t%s\t\t%s\t\t%s\t\t%s\n", user.Username, user.Role, user.RegNo, user.Email, user.Picture) - } - return nil -} - -var GetUsersCommand = cli.Command{ - Name: "getusers", - Aliases: []string{"gu"}, - Usage: "Get users", - Action: getUsers, - Flags: []cli.Flag{ - &cli.StringFlag{ - Name: "filter", - Aliases: []string{"f"}, - Usage: "Filter users by", - Required: false, - }, - &cli.StringFlag{ - Name: "value", - Aliases: []string{"v"}, - Usage: "Value of filter", - Required: false, - }, - }, -} diff --git a/vitty-backend-api/cmd/cliApp.go b/vitty-backend-api/cmd/root.go similarity index 53% rename from vitty-backend-api/cmd/cliApp.go rename to vitty-backend-api/cmd/root.go index c18b32a..d1410b4 100644 --- a/vitty-backend-api/cmd/cliApp.go +++ b/vitty-backend-api/cmd/root.go @@ -5,22 +5,20 @@ import ( "os" "github.com/GDGVIT/vitty-backend/vitty-backend-api/api" - "github.com/GDGVIT/vitty-backend/vitty-backend-api/cmd/management" + vittyCli "github.com/GDGVIT/vitty-backend/vitty-backend-api/cli" "github.com/GDGVIT/vitty-backend/vitty-backend-api/internal/auth" "github.com/GDGVIT/vitty-backend/vitty-backend-api/internal/database" "github.com/GDGVIT/vitty-backend/vitty-backend-api/internal/models" "github.com/gofiber/fiber/v2" - "github.com/gofiber/fiber/v2/middleware/cors" - "github.com/gofiber/fiber/v2/middleware/logger" "github.com/urfave/cli/v2" ) // VITTY CLI App -type VittyCliApp struct { +type VittyApp struct { env Env - CliApp *cli.App - fiberApp *fiber.App + cliApp *cli.App + webApp *fiber.App } // Environment variables @@ -41,15 +39,15 @@ type Env struct { google_redirect_uri string } -// Method to create a new VittyCliApp -func NewVittyCliApp() *VittyCliApp { - var vittyCliApp VittyCliApp - vittyCliApp.init() - return &vittyCliApp +// Method to create a new VittyApp +func NewVittyCliApp() *VittyApp { + var vittyApp VittyApp + vittyApp.init() + return &vittyApp } // Method to set environment variables -func (v *VittyCliApp) setEnv() { +func (v *VittyApp) setEnv() { v.env.fiberPort = os.Getenv("FIBER_PORT") v.env.debug = os.Getenv("DEBUG") v.env.postgresUrl = os.Getenv("POSTGRES_URL") @@ -59,62 +57,52 @@ func (v *VittyCliApp) setEnv() { v.env.google_redirect_uri = os.Getenv("GOOGLE_REDIRECT_URI") } -// Method to initialize the VittyCliApp -func (v *VittyCliApp) init() { +// Method to initialize CLI app +func (v *VittyApp) initCliApp() { + v.cliApp = vittyCli.NewCliApp() + + // Adding Run command + v.cliApp.Commands = append(v.cliApp.Commands, &cli.Command{ + Name: "run", + Aliases: []string{"r"}, + Usage: "Run the server", + Action: func(c *cli.Context) error { + v.webApp.Listen(v.env.fiberPort) + return nil + }, + }) +} + +// Method to initialize Web app +func (v *VittyApp) initWebApp() { + v.webApp = api.NewWebApi() +} + +// Method to initialize the VittyApp +func (v *VittyApp) init() { + // Set environment variables v.setEnv() + // Connect to database database.Connect(v.env.debug, v.env.postgresUrl) + + // Initialize models models.InitializeModels() + + // Initialize auth auth.InitializeAuth(v.env.jwtSecret) auth.InitializeGoogleOauth(v.env.google_client_id, v.env.google_client_secret, v.env.google_redirect_uri) auth.InitializeFirebaseApp() - v.CliApp = cli.NewApp() - - // Set the name, usage and version of the app - v.CliApp.Name = "Vitty" - v.CliApp.Usage = "Vitty Backend API" - v.CliApp.Version = "0.0.1" - v.CliApp.Authors = []*cli.Author{ - { - Name: "Dhruv Shah", - Email: "dhruvshahrds@gmail.com", - }} - v.CliApp.EnableBashCompletion = true - - v.fiberApp = fiber.New() - v.fiberApp.Use(logger.New()) - v.fiberApp.Use(cors.New( - cors.Config{ - AllowOrigins: "*", - AllowHeaders: "*", - AllowCredentials: true, - AllowMethods: "GET,POST,DELETE,PATCH,PUT,OPTIONS", - }, - )) - - api.InitializeApi(v.fiberApp) - - runCommand := cli.Command{ - Name: "run", - Aliases: []string{"r"}, - Usage: "Run the server", - Action: func(c *cli.Context) error { - v.fiberApp.Listen(v.env.fiberPort) - return nil - }, - } + // Initialize Web app + v.initWebApp() - v.CliApp.Commands = []*cli.Command{ - &runCommand, - &management.CreateSuperuserCommand, - &management.GetUsersCommand, - &management.DeleteUserCommand, - } + // Initialize CLI app + v.initCliApp() } -func (v *VittyCliApp) Run() { - err := v.CliApp.Run(os.Args) +func (v *VittyApp) Run() { + err := v.cliApp.Run(os.Args) if err != nil { log.Fatal(err) }