2023-05-22 11:08:08 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-11-08 13:01:27 +00:00
|
|
|
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621"
|
2023-07-17 10:57:23 +00:00
|
|
|
"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/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"
|
2023-06-20 08:38:36 +00:00
|
|
|
"strings"
|
2023-07-26 13:27:18 +00:00
|
|
|
"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
|
2023-07-26 13:27:18 +00:00
|
|
|
appConfig, err := config.LoadConfig()
|
2023-05-22 11:08:08 +00:00
|
|
|
if err != nil {
|
2023-07-26 13:27:18 +00:00
|
|
|
log.Fatal(err)
|
2023-05-22 11:08:08 +00:00
|
|
|
}
|
2023-07-26 13:27:18 +00:00
|
|
|
log.Debug("main: config loaded")
|
|
|
|
|
|
|
|
loggingSetup(appConfig)
|
|
|
|
|
|
|
|
var graphConnection logic.GraphConnection
|
|
|
|
ctx := context.Background()
|
2023-05-22 11:08:08 +00:00
|
|
|
|
2023-07-26 13:27:18 +00:00
|
|
|
switch strings.ToLower(appConfig.DBType) {
|
2023-06-20 08:38:36 +00:00
|
|
|
case "neo4j":
|
2023-07-26 13:27:18 +00:00
|
|
|
graphConnection = neo4j.NewNeo4JConnection(appConfig.Neo4jDebug)
|
|
|
|
err = graphConnection.Connect(ctx, appConfig.DBEndpoint, appConfig.DBUsername, appConfig.DBPassword)
|
2023-05-22 19:01:59 +00:00
|
|
|
if err != nil {
|
2023-07-26 13:27:18 +00:00
|
|
|
log.Panicf("main: %s", err)
|
2023-05-22 19:01:59 +00:00
|
|
|
}
|
2023-06-20 08:38:36 +00:00
|
|
|
default:
|
2023-07-26 13:27:18 +00:00
|
|
|
log.Panic("main: no database was selected!")
|
2023-06-20 08:38:36 +00:00
|
|
|
}
|
2023-07-26 13:27:18 +00:00
|
|
|
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
|
2023-11-08 13:01:27 +00:00
|
|
|
client := e621.NewClient(appConfig.E621Username, appConfig.E621APIKey)
|
2023-07-17 10:57:23 +00:00
|
|
|
// Register the ScapeUserFavourites with the "/user" route
|
2023-11-08 13:01:27 +00:00
|
|
|
http.HandleFunc("/user", api.ScapeUserFavourites(ctx, graphConnection, &client))
|
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)
|
2023-05-22 20:10:05 +00:00
|
|
|
if err != nil {
|
2023-07-26 13:27:18 +00:00
|
|
|
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")
|
2023-05-22 20:10:05 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 13:27:18 +00:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"log_level": log.GetLevel(),
|
|
|
|
"formatter": appConfig.LogFormat,
|
|
|
|
}).Info("main: setting logging info")
|
2023-05-22 20:10:05 +00:00
|
|
|
}
|