Skip to content

Commit

Permalink
add comment
Browse files Browse the repository at this point in the history
  • Loading branch information
sumitroajiprabowo committed Oct 29, 2023
1 parent c6f108f commit 235f307
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
12 changes: 10 additions & 2 deletions app/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
)

func loadRoutes(onuHandler *handler.OnuHandler) http.Handler {

// Initialize logger
l := log.Output(zerolog.ConsoleWriter{
Out: os.Stdout,
Expand All @@ -31,20 +32,27 @@ func loadRoutes(onuHandler *handler.OnuHandler) http.Handler {
// Create a group for /api/v1/
apiV1Group := chi.NewRouter()

// Define routes for /api/v1/
apiV1Group.Route("/board", func(r chi.Router) {
r.Get("/{board_id}/pon/{pon_id}", onuHandler.GetByBoardIDAndPonID)
r.Get("/{board_id}/pon/{pon_id}/onu/{onu_id}", onuHandler.GetByBoardIDPonIDAndOnuID)
r.Get("/{board_id}/pon/{pon_id}/onu_id/empty", onuHandler.GetEmptyOnuID)
r.Get("/{board_id}/pon/{pon_id}/onu_id/update", onuHandler.UpdateEmptyOnuID)
})

// Define routes for /api/v1/paginate
apiV1Group.Route("/paginate", func(r chi.Router) {
r.Get("/board/{board_id}/pon/{pon_id}", onuHandler.GetByBoardIDAndPonIDWithPaginate)
})

// Mount /api/v1/ to root router
router.Mount("/api/v1", apiV1Group)

return router
}

// rootHandler is a simple handler for root endpoint
func rootHandler(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("Hello, this is the root endpoint!"))
w.WriteHeader(http.StatusOK) // Set HTTP status code to 200
_, _ = w.Write([]byte("Hello, this is the root endpoint!")) // Write response body
}
18 changes: 10 additions & 8 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ import (
)

func main() {
server := app.New()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// Initialize application
server := app.New() // Create a new instance of application
ctx, cancel := context.WithCancel(context.Background()) // Create a new context with cancel function
defer cancel() // Cancel context when main function is finished

// Start application server in a goroutine
go func() {
err := server.Start(ctx)
err := server.Start(ctx) // Start application server
if err != nil {
log.Fatal().Err(err).Msg("Failed to start server")
cancel()
log.Fatal().Err(err).Msg("Failed to start server") // Log error message
cancel() // Cancel context if error occurred
}
}()

Expand All @@ -25,8 +28,7 @@ func main() {

// Wait for a signal to gracefully stop the application.
select {
case <-ctx.Done():
// Application was gracefully stopped or an error occurred
case <-ctx.Done(): // Application was gracefully stopped or an error occurred
log.Error().Err(ctx.Err()).Msg("Application was gracefully stopped or an error occurred")
}
}
14 changes: 12 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,19 +229,29 @@ type Board2Pon8 struct {

// LoadConfig file from given path using viper
func LoadConfig(filename string) (*Config, error) {

// Initialize viper
v := viper.New()

// Set config file name
v.SetConfigName(filename)

// Set config path in current directory
v.AddConfigPath(".")

// Allow environment variables to override config
v.AutomaticEnv()

// Read config file
if err := v.ReadInConfig(); err != nil {
var configFileNotFoundError viper.ConfigFileNotFoundError
var configFileNotFoundError viper.ConfigFileNotFoundError // Initialize config file not found error
if errors.As(err, &configFileNotFoundError) {
return nil, errors.New("config file not found")
}
return nil, err
}

var cfg Config
var cfg Config // Initialize config variable

// Unmarshal config
if err := v.Unmarshal(&cfg); err != nil {
Expand Down

0 comments on commit 235f307

Please sign in to comment.