Skip to content

Commit

Permalink
feat: add command to print out all routes
Browse files Browse the repository at this point in the history
print-routes fetches all routes from our fiber sever dynamically so no
maintenance needed.
  • Loading branch information
HilkopterBob committed Oct 12, 2024
1 parent ffce455 commit 7692633
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var startCmd = &cobra.Command{
Use: "start",
Short: "Start the server",
Run: func(cmd *cobra.Command, args []string) {
initServer()
initServer(false)
},
}

Expand Down Expand Up @@ -72,6 +72,14 @@ var setupCmd = &cobra.Command{
},
}

var printRoutesCmd = &cobra.Command{
Use: "print-routes",
Short: "Prints out all registered routes",
Run: func(cmd *cobra.Command, args []string) {
initServer(true)
},
}

// Generate command
var generateCmd = &cobra.Command{
Use: "generate [certs|config|admin]",
Expand Down Expand Up @@ -212,6 +220,7 @@ func init() {
rootCmd.AddCommand(restartCmd)
rootCmd.AddCommand(stopCmd)
rootCmd.AddCommand(setupCmd)
rootCmd.AddCommand(printRoutesCmd)

// Declare the Logger into global logger.Logger
// Init here so commands can be logget to!
Expand Down Expand Up @@ -289,19 +298,19 @@ func initConfig() {

// Initializes everything that is needed for the Server
// to run
func initServer() {
func initServer(printRoutes bool) {
initConfig()
err := db.InitDB()
if err != nil {
logger.Logger.Panicf("Got error from db.InitDB: %s", err)
}

// after init run Server
startServer()
startServer(printRoutes)
}

// startServer starts the Fiber server with appropriate configuration
func startServer() {
func startServer(printRoutes bool) {
pid := os.Getpid()
err := os.WriteFile("packagelock.pid", []byte(strconv.Itoa(pid)), 0644)
if err != nil {
Expand All @@ -316,10 +325,22 @@ func startServer() {
signal.Notify(quitChan, os.Interrupt, syscall.SIGTERM)

// Start the server in a goroutine
go func() {
go func(printRoutes bool) {
for {
Router := server.AddRoutes(config.Config)

if printRoutes == true {

Check failure on line 332 in main.go

View workflow job for this annotation

GitHub Actions / lint

S1002: should omit comparison to bool constant, can be simplified to `printRoutes` (gosimple)
routes := Router.Router.Stack() // Get all registered routes

for _, route := range routes {
for _, r := range route {
fmt.Printf("%s %s\n", r.Method, r.Path)
}
}
logger.Logger.Info("Printed all routes. Stopping the server...")
stopServer()
}

// Setup server address from config
serverAddr := config.Config.GetString("network.fqdn") + ":" + config.Config.GetString("network.port")

Expand Down Expand Up @@ -364,7 +385,7 @@ func startServer() {
logger.Logger.Info("Server stopped.")
}

startServer()
startServer(printRoutes)

case <-quitChan:

Expand All @@ -383,7 +404,7 @@ func startServer() {
return
}
}
}()
}(printRoutes)

// Watch for config changes
config.Config.OnConfigChange(func(e fsnotify.Event) {
Expand All @@ -405,7 +426,7 @@ func restartServer() {
fmt.Println("Restarting the Server...")
logger.Logger.Info("Restarting the Server...")
time.Sleep(5 * time.Second)
startServer()
startServer(false)
}

func stopServer() {
Expand Down

0 comments on commit 7692633

Please sign in to comment.