func main()

in main.go [21:148]


func main() {
	// load config file
	cfg, err := LoadConfig()
	if err != nil {
		log.Panic().Err(err).Send()
	}

	s := &Server{config: cfg}

	// configure logging
	configureLogging(cfg.LogLevel)

	// load dashboard configs
	dashboards, err := loadDashboards(cfg.ConfigFile, cfg)
	if err != nil {
		log.Fatal().Err(err).Send()
	}

	// parse index template
	tmpl, err := template.ParseFiles("index.gohtml")
	if err != nil {
		log.Fatal().Err(err).Send()
	}

	// create chain with http loggin
	public := newLoggingChain()
	private := public

	r := mux.NewRouter()
	r.StrictSlash(true)

	bd := cfg.BaseDomain
	bdr := r.Host(bd).Subrouter()

	// configure authentication if enabled
	if cfg.OAuthEnabled {
		cookieStore := sessions.NewCookieStore([]byte(cfg.SessionSecret))
		cookieStore.Options.HttpOnly = true
		parts := strings.Split(cfg.BaseDomain, ":")
		cookieStore.Options.Domain = parts[0]
		gothic.Store = cookieStore
		s.sessionStore = cookieStore

		pkceProvider := pkce.New(
			cfg.OAuthClientID,
			cfg.OAuthRedirectURI,
			cfg.OAuthDomain,
		)

		auth0Provider := auth0.New(
			cfg.OAuthClientID,
			cfg.OAuthClientSecret,
			cfg.OAuthRedirectURI,
			cfg.OAuthDomain,
			"openid",
			"profile",
			"email",
		)

		goth.UseProviders(
			pkceProvider,
			auth0Provider,
		)

		providerName := auth0Provider.Name()
		if cfg.OAuthClientSecret == "" {
			providerName = pkceProvider.Name()
		}

		gothic.GetProviderName = func(req *http.Request) (string, error) {
			return providerName, nil
		}

		log.Info().Msgf("enabling authentication with %s provider", providerName)

		bdr.Handle("/auth/login", public.Then(s.authLogin())).Methods("GET")
		bdr.Handle("/auth/callback", public.Then(s.authCallback())).Methods("GET")
		bdr.Handle("/auth/logout", public.Then(s.authLogout())).Methods("GET")

		private = public.Append(s.requireAuth)
	}

	// iterate over the dashboards and mount them
	for _, dashboard := range dashboards {
		log.Info().Msgf("mounting %s at /%s/", dashboard.Name, dashboard.Slug)
		chain := private
		if dashboard.Public {
			chain = public
		}

		sd := dashboard.Slug + "." + cfg.BaseDomain
		bdp := "/" + dashboard.Slug + "/"
		sdp := "/"
		sdr := r.Host(sd).Subrouter()

		bdghr := bdr.Methods("GET", "HEAD").Subrouter()
		sdghr := sdr.Methods("GET", "HEAD").Subrouter()

		var bdh http.Handler
		var sdh http.Handler

		if dashboard.Subdomain {
			bdh = chain.Then(redirectToDomain(sd, stripPrefix(bdp)))
			sdh = chain.Then(dashboard.Handler(sdp))
		} else {
			bdh = chain.Then(dashboard.Handler(bdp))
			sdh = chain.Then(redirectToDomain(bd, addPrefix(bdp)))
		}

		bdghr.Handle(bdp, bdh)
		bdghr.PathPrefix(bdp).Handler(bdh)

		if dashboard.Subdomain {
			sdghr.Handle(bdp, bdh)
			sdghr.PathPrefix(bdp).Handler(bdh)
		}

		sdghr.Handle(sdp, sdh)
		sdghr.PathPrefix(sdp).Handler(sdh)
	}

	// mount the index function to "/"
	bdr.Handle("/", public.Then(s.index(dashboards, tmpl))).Methods("GET")

	if err = http.ListenAndServe(cfg.Listen, r); err != nil {
		log.Fatal().Err(err).Send()
	}
}