Skip to content

Commit

Permalink
chore: extract prepareServices and parseConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
martinjirku committed Apr 12, 2024
1 parent 781c3f3 commit 76b649f
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ func main() {
if err != nil {
log.Error("Error loading .env file", slog.Any("error", err))
}
calendarService := services.NewCalendarService(os.Getenv(GOOGLE_API_KEY), os.Getenv(GOOGLE_CALENDAR_ID))
setupWebserver(log, calendarService)
setupWebserver(log)
}

type configuration struct {
Expand All @@ -51,21 +50,10 @@ type configuration struct {
panoramaURL string
}

func setupWebserver(log *slog.Logger, calendarService *services.CalendarService) {
config := configuration{}
func setupWebserver(log *slog.Logger) {
config := parseConfig(log)
router := mux.NewRouter()

flag.StringVar(&config.publicPath, "public", "", "Usage description of the flag")
flag.StringVar(&config.host, "host", "0.0.0.0", "specify the app host")
flag.StringVar(&config.panoramaURL, "panorama", "http://mcmamina.panfoto.sk/", "specify the panorama URL for reverse proxy")
flag.IntVar(&config.port, "port", 8080, "specfiy the port application will listen")
flag.Parse()

if !validatePort(config.port) {
log.Error("invalid port")
panic("invalid port")
}

var workingFolder fs.FS
log.Info("reading public folder")
if config.publicPath == "" {
Expand All @@ -81,7 +69,7 @@ func setupWebserver(log *slog.Logger, calendarService *services.CalendarService)
workingFolder = os.DirFS(config.publicPath)
}

cssService, sponsorService, recaptchaService := prepareServices(log, workingFolder)
cssService, sponsorService, recaptchaService, calendarService := prepareServices(log, workingFolder)
prepareMiddleware(router, log)

router.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -136,11 +124,27 @@ func setupWebserver(log *slog.Logger, calendarService *services.CalendarService)
os.Exit(0)
}

func prepareServices(log *slog.Logger, fs fs.FS) (*services.CSS, *services.SponsorService, *services.RecaptchaService) {
func parseConfig(log *slog.Logger) configuration {
config := configuration{}
flag.StringVar(&config.publicPath, "public", "", "Usage description of the flag")
flag.StringVar(&config.host, "host", "0.0.0.0", "specify the app host")
flag.StringVar(&config.panoramaURL, "panorama", "http://mcmamina.panfoto.sk/", "specify the panorama URL for reverse proxy")
flag.IntVar(&config.port, "port", 8080, "specfiy the port application will listen")
flag.Parse()

if !validatePort(config.port) {
log.Error("invalid port")
panic("invalid port")
}
return config
}

func prepareServices(log *slog.Logger, fs fs.FS) (*services.CSS, *services.SponsorService, *services.RecaptchaService, *services.CalendarService) {
cssService := services.NewCSS(fs, serviceLog(log, "css"))
sponsorService := services.NewSponsorService()
recaptchaService := services.NewRecaptchaService(os.Getenv(GOOGLE_API_KEY), os.Getenv(GOOGLE_CAPTCHA_SITE))
return cssService, sponsorService, recaptchaService
calendarService := services.NewCalendarService(os.Getenv(GOOGLE_API_KEY), os.Getenv(GOOGLE_CALENDAR_ID))
return cssService, sponsorService, recaptchaService, calendarService
}

func prepareMiddleware(router *mux.Router, log *slog.Logger) {
Expand Down

0 comments on commit 76b649f

Please sign in to comment.