package main import ( "database/sql" "errors" "fmt" "net/http" "os" "os/signal" "strings" "go.jolheiser.com/invitea/database" "go.jolheiser.com/invitea/database/sqlc/migrations" "go.jolheiser.com/invitea/router" "code.gitea.io/sdk/gitea" "github.com/golang-migrate/migrate/v4" "github.com/peterbourgon/ff/v3" "github.com/peterbourgon/ff/v3/ffyaml" "github.com/rs/zerolog" "github.com/rs/zerolog/log" ) func main() { level := zerolog.InfoLevel fs.Func("level", "Logging level (debug, info, error)", func(s string) error { lvl, err := zerolog.ParseLevel(s) if err != nil { return err } level = lvl return nil }) if err := ff.Parse(fs, os.Args[1:], ff.WithEnvVarPrefix("INVITEA"), ff.WithConfigFileFlag("config"), ff.WithAllowMissingConfigFile(true), ff.WithConfigFileParser(ffyaml.Parser), ); err != nil { log.Fatal().Err(err).Msg("") } zerolog.SetGlobalLevel(level) if !*jsonLogFlag { log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) } if err := requiredFlags(); err != nil { log.Fatal().Err(err).Msg("") } dsn := fmt.Sprintf("file:%s", *dbFlag) db, err := sql.Open("sqlite", dsn) if err != nil { log.Fatal().Err(err).Msg("could not open database") } defer db.Close() migrator, err := migrations.New(db) if err != nil { log.Fatal().Err(err).Msg("could not initialize migrations") } if err := migrator.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) { log.Fatal().Err(err).Msg("could not migrate database") } client, err := gitea.NewClient(*giteaURLFlag, gitea.SetToken(*giteaTokenFlag)) if err != nil { log.Fatal().Err(err).Msg("could not create gitea client") } r := router.New(router.Config{ Domain: *domainFlag, SessionSecret: *sessionSecretFlag, Database: database.New(db), GiteaClient: client, GiteaURL: strings.TrimSuffix(*giteaURLFlag, "/"), GiteaClientKey: *giteaClientKeyFlag, GiteaClientSecret: *giteaClientSecretFlag, GiteaToken: *giteaTokenFlag, }) go func() { log.Debug().Msgf("Listening at http://localhost:%d", *portFlag) if err := http.ListenAndServe(fmt.Sprintf(":%d", *portFlag), r); err != nil { log.Err(err).Msg("") } }() ch := make(chan os.Signal, 1) signal.Notify(ch, os.Interrupt, os.Kill) <-ch }