diff --git a/server/api/api.go b/server/api/api.go index f2a935b..c53d451 100644 --- a/server/api/api.go +++ b/server/api/api.go @@ -3,10 +3,14 @@ package api import ( "net/http" + "git.dubyatp.xyz/dubyatp/scannerbot/server/db" "github.com/go-chi/chi/v5" ) func Start() { + db.InitScyllaDB() + defer db.CloseScyllaDB() + r := chi.NewRouter() r.Get("/", func(w http.ResponseWriter, r *http.Request) { diff --git a/server/db/scylla.go b/server/db/scylla.go new file mode 100644 index 0000000..c07bb78 --- /dev/null +++ b/server/db/scylla.go @@ -0,0 +1,31 @@ +package db + +import ( + "log/slog" + "os" + + "github.com/gocql/gocql" +) + +var Session *gocql.Session + +func InitScyllaDB() { + cluster := gocql.NewCluster(os.Getenv("SCYLLA_CLUSTER")) + cluster.Keyspace = os.Getenv("SCYLLA_KEYSPACE") + + cluster.Consistency = gocql.Quorum + + session, err := cluster.CreateSession() + if err != nil { + slog.Error("Failed to connect to ScyllaDB", "error", err) + os.Exit(1) + } + Session = session + slog.Info("Connected to ScyllaDB") +} + +func CloseScyllaDB() { + if Session != nil { + Session.Close() + } +} diff --git a/server/go.mod b/server/go.mod index bf0718b..1175eff 100644 --- a/server/go.mod +++ b/server/go.mod @@ -4,5 +4,10 @@ go 1.26.3 require ( github.com/go-chi/chi/v5 v5.2.5 // indirect + github.com/gocql/gocql v1.7.0 // indirect + github.com/golang/snappy v0.0.3 // indirect github.com/google/uuid v1.6.0 // indirect + github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed // indirect + github.com/joho/godotenv v1.5.1 // indirect + gopkg.in/inf.v0 v0.9.1 // indirect ) diff --git a/server/go.sum b/server/go.sum index cf4e57e..c6b5ddf 100644 --- a/server/go.sum +++ b/server/go.sum @@ -1,4 +1,23 @@ +github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932/go.mod h1:NOuUCSz6Q9T7+igc/hlvDOUdtWKryOrtFyIVABv/p7k= +github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-chi/chi/v5 v5.2.5 h1:Eg4myHZBjyvJmAFjFvWgrqDTXFyOzjj7YIm3L3mu6Ug= github.com/go-chi/chi/v5 v5.2.5/go.mod h1:X7Gx4mteadT3eDOMTsXzmI4/rwUpOwBHLpAfupzFJP0= +github.com/gocql/gocql v1.7.0 h1:O+7U7/1gSN7QTEAaMEsJc1Oq2QHXvCWoF3DFK9HDHus= +github.com/gocql/gocql v1.7.0/go.mod h1:vnlvXyFZeLBF0Wy+RS8hrOdbn0UWsWtdg07XJnFxZ+4= +github.com/golang/snappy v0.0.3 h1:fHPg5GQYlCeLIPB9BZqMVR5nR9A+IM5zcgeTdjMYmLA= +github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed h1:5upAirOpQc1Q53c0bnx2ufif5kANL7bfZWcc6VJWJd8= +github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed/go.mod h1:tMWxXQ9wFIaZeTI9F+hmhFiGpFmhOHzyShyFUhRm0H4= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc= +gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= diff --git a/server/main.go b/server/main.go index 557e6f4..ef745c8 100644 --- a/server/main.go +++ b/server/main.go @@ -2,12 +2,38 @@ package main import ( "log/slog" + "os" "git.dubyatp.xyz/dubyatp/scannerbot/server/api" + "github.com/joho/godotenv" ) +var REQUIRED_ENVS = [...]string{ + "SCYLLA_CLUSTER", "SCYLLA_KEYSPACE", +} + +func checkEnvVars(keys []string) (bool, []string) { + var missing []string + for _, key := range keys { + if _, ok := os.LookupEnv(key); !ok { + missing = append(missing, key) + } + } + return len(missing) == 0, missing +} + func main() { - //println("hello world") + + err := godotenv.Load() + if err != nil { + slog.Debug("No .env file loaded, will try OS environment variables") + } + + exists, missingVars := checkEnvVars(REQUIRED_ENVS[:]) + if !exists { + slog.Error("Missing environment variables", "missing", missingVars) + os.Exit(1) + } slog.Info("Starting the API server...") api.Start()