This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
e621-sdk-go/cmd/scraper/main.go

98 lines
2.5 KiB
Go
Raw Normal View History

2023-05-22 11:08:08 +00:00
package main
import (
"context"
"git.dragse.it/anthrove/e621-to-graph/internal/api"
"git.dragse.it/anthrove/e621-to-graph/internal/config"
"git.dragse.it/anthrove/e621-to-graph/internal/database/neo4j"
"git.dragse.it/anthrove/e621-to-graph/internal/e621"
"git.dragse.it/anthrove/e621-to-graph/pkg/logic"
2023-06-21 11:29:23 +00:00
log "github.com/sirupsen/logrus"
2023-05-24 14:05:27 +00:00
"net/http"
"strings"
"time"
2023-05-22 11:08:08 +00:00
)
func main() {
2023-06-21 11:29:23 +00:00
2023-05-24 14:05:27 +00:00
// Loads Config
appConfig, err := config.LoadConfig()
2023-05-22 11:08:08 +00:00
if err != nil {
log.Fatal(err)
2023-05-22 11:08:08 +00:00
}
log.Debug("main: config loaded")
loggingSetup(appConfig)
var graphConnection logic.GraphConnection
ctx := context.Background()
2023-05-22 11:08:08 +00:00
switch strings.ToLower(appConfig.DBType) {
case "neo4j":
graphConnection = neo4j.NewNeo4JConnection(appConfig.Neo4jDebug)
err = graphConnection.Connect(ctx, appConfig.DBEndpoint, appConfig.DBUsername, appConfig.DBPassword)
if err != nil {
log.Panicf("main: %s", err)
}
default:
log.Panic("main: no database was selected!")
}
log.WithFields(log.Fields{
"database_type": strings.ToLower(appConfig.DBType),
}).Info("main: database connection successful")
2023-05-22 11:08:08 +00:00
2023-05-24 14:05:27 +00:00
// Initialize the e621API
e621Client := e621.NewClient(appConfig.E621APIKey, appConfig.E621Username)
// Register the ScapeUserFavourites with the "/user" route
http.HandleFunc("/user", api.ScapeUserFavourites(ctx, graphConnection, e621Client))
2023-05-22 11:08:08 +00:00
2023-05-24 14:05:27 +00:00
// Start the HTTP server
err = http.ListenAndServe(":8080", nil)
if err != nil {
log.Panicf("main: %s", err)
}
}
func loggingSetup(appConfig *config.Config) {
switch strings.ToUpper(appConfig.LogLevel) {
case "FATAL":
log.SetLevel(log.FatalLevel)
case "ERROR":
log.SetLevel(log.ErrorLevel)
case "WARN":
log.SetLevel(log.WarnLevel)
case "INFO":
log.SetLevel(log.InfoLevel)
case "DEBUG":
log.SetLevel(log.DebugLevel)
case "TRACE":
log.SetLevel(log.TraceLevel)
default:
log.Panic("main: no log level was set")
}
switch strings.ToUpper(appConfig.LogFormat) {
case "PLAIN":
log.SetFormatter(&log.TextFormatter{
ForceColors: true,
ForceQuote: true,
DisableLevelTruncation: true,
PadLevelText: true,
FullTimestamp: true,
TimestampFormat: time.DateTime, // 2006-01-02 15:04:05
})
case "JSON":
log.SetFormatter(&log.JSONFormatter{
TimestampFormat: time.DateTime, // 2006-01-02 15:04:05,
})
default:
log.Panic("main: no formatter was set")
}
log.WithFields(log.Fields{
"log_level": log.GetLevel(),
"formatter": appConfig.LogFormat,
}).Info("main: setting logging info")
}