Skip to content

Commit

Permalink
mv run/loadConfig into main.go
Browse files Browse the repository at this point in the history
  • Loading branch information
adoublef committed Sep 16, 2022
1 parent f2161e8 commit 5f4e618
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 92 deletions.
20 changes: 0 additions & 20 deletions cmd/server/config.go

This file was deleted.

88 changes: 87 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -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
}
71 changes: 0 additions & 71 deletions cmd/server/run.go

This file was deleted.

0 comments on commit 5f4e618

Please sign in to comment.