Init
This commit is contained in:
commit
b51055e6de
7 changed files with 155 additions and 0 deletions
49
cmd/vanity-tester-backend/config.go
Normal file
49
cmd/vanity-tester-backend/config.go
Normal file
|
@ -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
|
||||
}
|
29
cmd/vanity-tester-backend/database.go
Normal file
29
cmd/vanity-tester-backend/database.go
Normal file
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
9
cmd/vanity-tester-backend/endpoints.go
Normal file
9
cmd/vanity-tester-backend/endpoints.go
Normal file
|
@ -0,0 +1,9 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func root(w http.ResponseWriter, req *http.Request) {
|
||||
return
|
||||
}
|
1
cmd/vanity-tester-backend/exporters.go
Normal file
1
cmd/vanity-tester-backend/exporters.go
Normal file
|
@ -0,0 +1 @@
|
|||
package main
|
43
cmd/vanity-tester-backend/main.go
Normal file
43
cmd/vanity-tester-backend/main.go
Normal file
|
@ -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)
|
||||
}
|
14
go.mod
Normal file
14
go.mod
Normal file
|
@ -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
|
||||
)
|
10
go.sum
Normal file
10
go.sum
Normal file
|
@ -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=
|
Loading…
Reference in a new issue