diff --git a/cmd/server/config.go b/cmd/server/config.go deleted file mode 100644 index e35f60d7..00000000 --- a/cmd/server/config.go +++ /dev/null @@ -1,20 +0,0 @@ -package main - -import "github.com/spf13/viper" - -func loadConfig() error { - viper.SetConfigName("config") // name of config file (without extension) - viper.SetConfigType("env") // REQUIRED if the config file does not have the extension in the name - viper.AddConfigPath(".") // optionally look for config in the working directory - - viper.SetDefault("PORT", "8080") // Set Default variables - - viper.AutomaticEnv() - - err := viper.ReadInConfig() // Find and read the config file - if err != nil { // Handle errors reading the config file - return err - } - - return nil -} diff --git a/cmd/server/main.go b/cmd/server/main.go index 2f468f2a..dd315933 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -1,9 +1,95 @@ package main -import "log" +import ( + "context" + "log" + "net" + "net/http" + "os" + "os/signal" + "syscall" + "time" + + "github.com/go-chi/chi/v5" + "github.com/rs/cors" + "github.com/spf13/viper" + "golang.org/x/sync/errgroup" + + "github.com/rog-golang-buddies/rapidmidiex/www" +) func main() { if err := run(); err != nil { log.Fatalln(err) } } + +func run() error { + // if err := loadConfig(); err != nil { + // return err + // } + + port := getEnv("PORT", "8888") + + sCtx, cancel := signal.NotifyContext(context.Background(), syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) + defer cancel() + + c := cors.Options{ + AllowedOrigins: []string{"http://localhost:" + port}, + AllowCredentials: true, + AllowedMethods: []string{http.MethodGet}, + AllowedHeaders: []string{"Origin", "Content-Type", "Accept", "Authorization"}, + } + + srv := http.Server{ + Addr: ":" + port, + Handler: cors.New(c).Handler(www.NewService(chi.NewMux())), + ReadTimeout: 10 * time.Second, // max time to read request from the client + WriteTimeout: 10 * time.Second, // max time to write response to the client + IdleTimeout: 120 * time.Second, // max time for connections using TCP Keep-Alive + BaseContext: func(_ net.Listener) context.Context { return sCtx }, + } + + g, gCtx := errgroup.WithContext(sCtx) + + g.Go(func() error { + // Run the server + log.Printf("App server starting on %s", srv.Addr) + return srv.ListenAndServe() + }) + + g.Go(func() error { + <-gCtx.Done() + return srv.Shutdown(context.Background()) + }) + + if err := g.Wait(); err != nil { + log.Printf("exit reason: %s \n", err) + } + + return nil +} + +func getEnv(key, fallback string) string { + if value, ok := os.LookupEnv(key); ok { + return value + } + return fallback +} + +func loadConfig() error { + viper.SetConfigName("config") // name of config file (without extension) + viper.SetConfigType("env") // REQUIRED if the config file does not have the extension in the name + viper.AddConfigPath(".") // optionally look for config in the working directory + + viper.SetDefault("PORT", "8080") // Set Default variables + + viper.AutomaticEnv() + + err := viper.ReadInConfig() // Find and read the config file + if err != nil { // Handle errors reading the config file + return err + } + + return nil +} diff --git a/cmd/server/run.go b/cmd/server/run.go deleted file mode 100644 index 5be3bc17..00000000 --- a/cmd/server/run.go +++ /dev/null @@ -1,71 +0,0 @@ -package main - -import ( - "context" - "log" - "net" - "net/http" - "os" - "os/signal" - "syscall" - "time" - - "github.com/go-chi/chi/v5" - "github.com/rs/cors" - "golang.org/x/sync/errgroup" - - "github.com/rog-golang-buddies/rapidmidiex/www" -) - -func run() error { - // if err := loadConfig(); err != nil { - // return err - // } - - port := getEnv("PORT", "8888") - - sCtx, cancel := signal.NotifyContext(context.Background(), syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) - defer cancel() - - c := cors.Options{ - AllowedOrigins: []string{"http://localhost:" + port}, - AllowCredentials: true, - AllowedMethods: []string{http.MethodGet}, - AllowedHeaders: []string{"Origin", "Content-Type", "Accept", "Authorization"}, - } - - srv := http.Server{ - Addr: ":" + port, - Handler: cors.New(c).Handler(www.NewService(chi.NewMux())), - ReadTimeout: 10 * time.Second, // max time to read request from the client - WriteTimeout: 10 * time.Second, // max time to write response to the client - IdleTimeout: 120 * time.Second, // max time for connections using TCP Keep-Alive - BaseContext: func(_ net.Listener) context.Context { return sCtx }, - } - - g, gCtx := errgroup.WithContext(sCtx) - - g.Go(func() error { - // Run the server - log.Printf("App server starting on %s", srv.Addr) - return srv.ListenAndServe() - }) - - g.Go(func() error { - <-gCtx.Done() - return srv.Shutdown(context.Background()) - }) - - if err := g.Wait(); err != nil { - log.Printf("exit reason: %s \n", err) - } - - return nil -} - -func getEnv(key, fallback string) string { - if value, ok := os.LookupEnv(key); ok { - return value - } - return fallback -}