kycnot.me/src/main.go

92 lines
2.0 KiB
Go
Raw Normal View History

2023-10-30 17:41:30 -04:00
package main
import (
"flag"
"os"
"github.com/joho/godotenv"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
2023-10-30 17:42:11 -04:00
2023-11-12 16:06:22 -05:00
"pluja.dev/kycnot.me/config"
2023-10-30 17:42:11 -04:00
"pluja.dev/kycnot.me/database"
"pluja.dev/kycnot.me/server"
2023-11-12 16:06:22 -05:00
"pluja.dev/kycnot.me/utils/tos_scraper"
2023-10-30 17:41:30 -04:00
)
2023-11-12 16:06:22 -05:00
var (
// Flags
debug = flag.Bool("debug", false, "sets log level to debug")
dev = flag.Bool("dev", false, "sets dev mode")
nocache = flag.Bool("nocache", false, "disables cache")
scraper = flag.Bool("scrap", false, "enables the scraper")
listen = flag.String("listen", ":4488", "address to listen to")
)
2023-11-13 01:14:52 -05:00
func init() {
// Flags
flag.Parse()
2023-11-12 16:06:22 -05:00
config.Conf = config.Config{
Dev: *dev,
Cache: *nocache,
Debug: *debug,
Scraper: *scraper,
ListenAddr: *listen,
}
2023-10-30 17:41:30 -04:00
// Flags
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if *debug {
2023-11-12 16:06:22 -05:00
log.Debug().Msg("Debug mode enabled")
2023-10-30 17:41:30 -04:00
log.Logger = log.Output(
zerolog.ConsoleWriter{
Out: os.Stdout,
TimeFormat: "02.01.2006 15:04:05",
},
).With().Caller().Logger()
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
if *dev {
log.Logger = log.Output(
zerolog.ConsoleWriter{
Out: os.Stdout,
TimeFormat: "02.01.2006 15:04:05",
},
).With().Caller().Logger()
zerolog.SetGlobalLevel(zerolog.DebugLevel)
2023-11-12 16:06:22 -05:00
log.Debug().Msg("DEV mode enabled")
2023-10-30 17:41:30 -04:00
}
2023-11-12 16:06:22 -05:00
2023-10-30 17:41:30 -04:00
if *nocache {
2023-11-12 16:06:22 -05:00
log.Debug().Msg("Cache disabled")
2023-10-30 17:41:30 -04:00
}
// Load .env file
log.Info().Msg("Loading .env file.")
err := godotenv.Load()
if err != nil {
log.Info().Msg("No .env file found, using environment variables")
}
// Database init
log.Info().Msg("Initializing database.")
2023-10-30 17:42:11 -04:00
database.InitDb()
2023-10-30 17:41:30 -04:00
2023-11-12 16:06:22 -05:00
// AI ToS Scraper init
if config.Conf.Scraper {
log.Info().Msg("Initializing AI ToS scraper.")
tos_scraper.InitTosScraperDaemon()
}
2023-11-13 01:14:52 -05:00
}
2023-11-12 16:06:22 -05:00
2023-11-13 01:14:52 -05:00
func main() {
2023-10-30 17:41:30 -04:00
// Server init
log.Info().Msg("Initializing server.")
2023-11-13 01:14:52 -05:00
defer database.Close()
2023-11-12 16:06:22 -05:00
server := server.NewServer(config.Conf.ListenAddr)
if err := server.Run(); err != nil {
log.Fatal().Err(err)
}
2023-10-30 17:41:30 -04:00
}