From b51055e6ded2b3f297bf9a82b417ee8996638644 Mon Sep 17 00:00:00 2001 From: Fijxu Date: Fri, 27 Dec 2024 14:04:18 -0300 Subject: [PATCH] Init --- cmd/vanity-tester-backend/config.go | 49 ++++++++++++++++++++++++++ cmd/vanity-tester-backend/database.go | 29 +++++++++++++++ cmd/vanity-tester-backend/endpoints.go | 9 +++++ cmd/vanity-tester-backend/exporters.go | 1 + cmd/vanity-tester-backend/main.go | 43 ++++++++++++++++++++++ go.mod | 14 ++++++++ go.sum | 10 ++++++ 7 files changed, 155 insertions(+) create mode 100644 cmd/vanity-tester-backend/config.go create mode 100644 cmd/vanity-tester-backend/database.go create mode 100644 cmd/vanity-tester-backend/endpoints.go create mode 100644 cmd/vanity-tester-backend/exporters.go create mode 100644 cmd/vanity-tester-backend/main.go create mode 100644 go.mod create mode 100644 go.sum diff --git a/cmd/vanity-tester-backend/config.go b/cmd/vanity-tester-backend/config.go new file mode 100644 index 0000000..650e902 --- /dev/null +++ b/cmd/vanity-tester-backend/config.go @@ -0,0 +1,49 @@ +package main + +import ( + "encoding/json" + "io/ioutil" + "log" +) + +type HTTPServer struct { + Port string `json:"port"` + Addr string `json:"addr"` +} + +type Redis struct { + Addr string `json:"addr"` + DB int `json:"db"` +} + +type Config struct { + HTTPServer HTTPServer `json:"http_server"` + Redis Redis `json:"redis"` +} + +func loadConfig(configPath string) Config { + + file, err := ioutil.ReadFile(configPath) + if err != nil { + log.Fatal("Error reading config file: ", err) + } + + // Default config in case a value is missing in the config file + var config = Config{ + HTTPServer: HTTPServer{ + Port: "127.0.0.1", + Addr: "8080", + }, + Redis: Redis{ + Addr: "127.0.0.1:6379", + DB: 0, + }, + } + + err = json.Unmarshal(file, &config) + if err != nil { + log.Fatal("Error loading config file: ", err) + } + + return config +} diff --git a/cmd/vanity-tester-backend/database.go b/cmd/vanity-tester-backend/database.go new file mode 100644 index 0000000..c83367b --- /dev/null +++ b/cmd/vanity-tester-backend/database.go @@ -0,0 +1,29 @@ +package main + +import ( + "database/sql" + "log" +) + +func createDb(db *sql.DB) { + var exists bool + // rows := db.QueryRow("SELECT EXISTS (SELECT 1 FROM sqlite_schema WHERE type='table' AND name='seventv_ids') AND EXISTS (SELECT 1 FROM sqlite_schema WHERE type='table' AND name='seventv_ids');") + err := db.QueryRow(`SELECT EXISTS (SELECT 1 FROM sqlite_schema WHERE type='table' AND name='seventv_ids') AND EXISTS (SELECT 1 FROM sqlite_schema WHERE type='table' AND name='seventv_ids');`).Scan(&exists) + if err != nil { + log.Fatal("Query failed: ", err) + } + + if !exists { + queries := []string{ + `CREATE TABLE IF NOT EXISTS seventv_ids (username TEXT UNIQUE, twitch_id TEXT UNIQUE, seventv_id TEXT UNIQUE);`, + `CREATE TABLE IF NOT EXISTS chatty_ids (username TEXT UNIQUE, twitch_id TEXT UNIQUE);`, + } + + for _, query := range queries { + _, err := db.Exec(query) + if err != nil { + log.Fatalf("Failed to create table: %s\nError: %v", query, err) + } + } + } +} diff --git a/cmd/vanity-tester-backend/endpoints.go b/cmd/vanity-tester-backend/endpoints.go new file mode 100644 index 0000000..10f8530 --- /dev/null +++ b/cmd/vanity-tester-backend/endpoints.go @@ -0,0 +1,9 @@ +package main + +import ( + "net/http" +) + +func root(w http.ResponseWriter, req *http.Request) { + return +} diff --git a/cmd/vanity-tester-backend/exporters.go b/cmd/vanity-tester-backend/exporters.go new file mode 100644 index 0000000..06ab7d0 --- /dev/null +++ b/cmd/vanity-tester-backend/exporters.go @@ -0,0 +1 @@ +package main diff --git a/cmd/vanity-tester-backend/main.go b/cmd/vanity-tester-backend/main.go new file mode 100644 index 0000000..ff5ae07 --- /dev/null +++ b/cmd/vanity-tester-backend/main.go @@ -0,0 +1,43 @@ +package main + +import ( + "context" + "database/sql" + "log" + "net/http" + + _ "github.com/mattn/go-sqlite3" +) + +var client *http.Client +var rctx = context.Background() +var config = loadConfig("./config.json") + +func main() { + // redisDb := redis.NewClient(&redis.Options{ + // Addr: config.Redis.Addr, + // DB: config.Redis.DB, + // }) + + db, err := sql.Open("sqlite3", "./database.db") + if err != nil { + log.Fatal(err) + } + defer db.Close() + + createDb(db) + + mux := http.NewServeMux() + + mux.HandleFunc("/", root) + // mux.HandleFunc("/health", health()) + // mux.HandleFunc("/stats", stats()) + + srv := &http.Server{ + Handler: mux, + Addr: config.HTTPServer.Addr + ":" + config.HTTPServer.Port, + } + + srv.ListenAndServe() + log.Printf("Server running at %s:%s", config.HTTPServer.Addr, config.HTTPServer.Port) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..00c411b --- /dev/null +++ b/go.mod @@ -0,0 +1,14 @@ +module git.nadeko.net/fijxu/vanity-tester-backend-go + +go 1.23.3 + +require ( + github.com/go-redis/redis v6.15.9+incompatible + github.com/mattn/go-sqlite3 v1.14.24 +) + +require ( + github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/redis/go-redis/v9 v9.7.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..8a50db7 --- /dev/null +++ b/go.sum @@ -0,0 +1,10 @@ +github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= +github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg= +github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA= +github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM= +github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= +github.com/redis/go-redis/v9 v9.7.0 h1:HhLSs+B6O021gwzl+locl0zEDnyNkxMtf/Z3NNBMa9E= +github.com/redis/go-redis/v9 v9.7.0/go.mod h1:f6zhXITC7JUJIlPEiBOTXxJgPLdZcA93GewI7inzyWw=