generated from rog-golang-buddies/golang-template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
87 additions
and
92 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.