Compare commits

...

2 Commits

Author SHA1 Message Date
williamp d059dc0032 server: implement scylladb 2026-05-14 00:21:44 +00:00
williamp 4be223d1b7 server: /whoami 2026-05-13 22:51:55 +00:00
6 changed files with 126 additions and 2 deletions
+8
View File
@@ -3,15 +3,23 @@ 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) {
w.Write([]byte("hello world"))
})
r.Route("/whoami", func(r chi.Router) {
r.Get("/", Whoami)
})
http.ListenAndServe(":3000", r)
}
+30
View File
@@ -0,0 +1,30 @@
package api
import (
"log/slog"
"net/http"
"github.com/google/uuid"
)
func Whoami(w http.ResponseWriter, r *http.Request) {
slog.Debug("user: entering Whoami handler")
user, ok := r.Context().Value(userKey{}).(*User)
if !ok || user == nil {
slog.Debug("user: anonymous user")
w.Write([]byte("anonymous"))
return
}
}
type User struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Password string `json:"-"`
}
type userKey struct{}
type UserPayload struct {
*User
}
+31
View File
@@ -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()
}
}
+9 -1
View File
@@ -2,4 +2,12 @@ module git.dubyatp.xyz/dubyatp/scannerbot/server
go 1.26.3
require github.com/go-chi/chi/v5 v5.2.5 // indirect
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
)
+21
View File
@@ -1,2 +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=
+27 -1
View File
@@ -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()