add more shit
This commit is contained in:
parent
b51055e6de
commit
c3d055b2f1
8 changed files with 153 additions and 16 deletions
|
@ -2,8 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"os"
|
||||||
"log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type HTTPServer struct {
|
type HTTPServer struct {
|
||||||
|
@ -16,33 +15,43 @@ type Redis struct {
|
||||||
DB int `json:"db"`
|
DB int `json:"db"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Gql struct {
|
||||||
|
ClientID string `json:"clientId"`
|
||||||
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
HTTPServer HTTPServer `json:"http_server"`
|
HTTPServer HTTPServer `json:"http_server"`
|
||||||
Redis Redis `json:"redis"`
|
Redis Redis `json:"redis"`
|
||||||
|
Gql Gql `json:"gql"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadConfig(configPath string) Config {
|
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
|
// Default config in case a value is missing in the config file
|
||||||
var config = Config{
|
var config = Config{
|
||||||
HTTPServer: HTTPServer{
|
HTTPServer: HTTPServer{
|
||||||
Port: "127.0.0.1",
|
Addr: "127.0.0.1",
|
||||||
Addr: "8080",
|
Port: "8080",
|
||||||
},
|
},
|
||||||
Redis: Redis{
|
Redis: Redis{
|
||||||
Addr: "127.0.0.1:6379",
|
Addr: "127.0.0.1:6379",
|
||||||
DB: 0,
|
DB: 0,
|
||||||
},
|
},
|
||||||
|
Gql: Gql{
|
||||||
|
ClientID: "ue6666qo983tsx6so1t0vnawi233wa",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
file, err := os.ReadFile(configPath)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error().Msg("Failed to read config file. The default config will be used")
|
||||||
|
return config
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(file, &config)
|
err = json.Unmarshal(file, &config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error loading config file: ", err)
|
logger.Error().Msg("Failed to parse config file. The default config will be used")
|
||||||
|
return config
|
||||||
}
|
}
|
||||||
|
|
||||||
return config
|
return config
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func root(w http.ResponseWriter, req *http.Request) {
|
func root(w http.ResponseWriter, req *http.Request) {
|
||||||
return
|
io.WriteString(w, "WIP Vanity Tester Backend")
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
package main
|
|
35
cmd/vanity-tester-backend/gql.go
Normal file
35
cmd/vanity-tester-backend/gql.go
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type GQL struct{}
|
||||||
|
|
||||||
|
func getUserID(username string) {
|
||||||
|
headers := map[string]string{
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Client-Id": config.Gql.ClientID,
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Send the data!
|
||||||
|
request, err := http.Post("https://gql.twitch.tv/gql", "application/json", data)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error().Msg(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
for key, value := range headers {
|
||||||
|
request.Header.Set(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := client.Do(request.Request)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error().Msg(err.Error())
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
|
||||||
|
body, err := io.ReadAll(res.Body)
|
||||||
|
|
||||||
|
return string(body)
|
||||||
|
}
|
|
@ -3,15 +3,18 @@ package main
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
"github.com/rs/zerolog"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
var client *http.Client
|
var client *http.Client
|
||||||
var rctx = context.Background()
|
var rctx = context.Background()
|
||||||
var config = loadConfig("./config.json")
|
var config = loadConfig("./config.json")
|
||||||
|
var logger = log.Logger.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// redisDb := redis.NewClient(&redis.Options{
|
// redisDb := redis.NewClient(&redis.Options{
|
||||||
|
@ -21,7 +24,7 @@ func main() {
|
||||||
|
|
||||||
db, err := sql.Open("sqlite3", "./database.db")
|
db, err := sql.Open("sqlite3", "./database.db")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
logger.Fatal().Msg(err.Error())
|
||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
|
@ -38,6 +41,14 @@ func main() {
|
||||||
Addr: config.HTTPServer.Addr + ":" + config.HTTPServer.Port,
|
Addr: config.HTTPServer.Addr + ":" + config.HTTPServer.Port,
|
||||||
}
|
}
|
||||||
|
|
||||||
srv.ListenAndServe()
|
logger.Info().Msgf("Starting server at http://%s:%s", config.HTTPServer.Addr, config.HTTPServer.Port)
|
||||||
log.Printf("Server running at %s:%s", config.HTTPServer.Addr, config.HTTPServer.Port)
|
|
||||||
|
go func() {
|
||||||
|
if err := srv.ListenAndServe(); err != nil {
|
||||||
|
logger.Fatal().Msgf("Failed to start server: %s", err.Error())
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
logger.Info().Msgf("Server running at http://%s:%s", config.HTTPServer.Addr, config.HTTPServer.Port)
|
||||||
|
select {}
|
||||||
}
|
}
|
||||||
|
|
61
cmd/vanity-tester-backend/providers.go
Normal file
61
cmd/vanity-tester-backend/providers.go
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SevenTv struct{}
|
||||||
|
type Bttv struct{}
|
||||||
|
type FrankerFz struct{}
|
||||||
|
type Chatterino struct{}
|
||||||
|
type Chatty struct{}
|
||||||
|
type DankChat struct{}
|
||||||
|
type Homies struct{}
|
||||||
|
type PurpleTV struct{}
|
||||||
|
|
||||||
|
func doGetRequest(url string) string {
|
||||||
|
request, err := http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error().Msg(err.Error())
|
||||||
|
}
|
||||||
|
res, err := client.Do(request.Request)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error().Msg(err.Error())
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
|
||||||
|
body, err := io.ReadAll(res.Body)
|
||||||
|
|
||||||
|
return string(body)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SevenTv) getUserID(userId string) string {
|
||||||
|
req := doGetRequest("https://7tv.io/v3/users/twitch/" + userId)
|
||||||
|
return req
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Bttv) getBadges() string {
|
||||||
|
req := doGetRequest("https://api.betterttv.net/3/cached/badges/twitch")
|
||||||
|
return req
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *FrankerFz) getBadges() string {
|
||||||
|
req := doGetRequest("https://api.frankerfacez.com/v1/badges/ids")
|
||||||
|
return req
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Chatty) getBadges() string {
|
||||||
|
req := doGetRequest("https://api.betterttv.net/3/cached/badges/twitch")
|
||||||
|
return req
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *DankChat) getBadges() string {
|
||||||
|
req := doGetRequest("https://flxrs.com/api/badges")
|
||||||
|
return req
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Chatterino) getBadges() string {
|
||||||
|
req := doGetRequest("https://api.chatterino.com/badges")
|
||||||
|
return req
|
||||||
|
}
|
4
go.mod
4
go.mod
|
@ -10,5 +10,9 @@ require (
|
||||||
require (
|
require (
|
||||||
github.com/cespare/xxhash/v2 v2.2.0 // indirect
|
github.com/cespare/xxhash/v2 v2.2.0 // indirect
|
||||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||||
|
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||||
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
github.com/redis/go-redis/v9 v9.7.0 // indirect
|
github.com/redis/go-redis/v9 v9.7.0 // indirect
|
||||||
|
github.com/rs/zerolog v1.33.0 // indirect
|
||||||
|
golang.org/x/sys v0.29.0 // indirect
|
||||||
)
|
)
|
||||||
|
|
17
go.sum
17
go.sum
|
@ -1,10 +1,27 @@
|
||||||
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
|
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/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||||
|
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
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/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 h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg=
|
||||||
github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
|
github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
|
||||||
|
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||||
|
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||||
|
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||||
|
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||||
|
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
|
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||||
|
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
|
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/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||||
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
github.com/redis/go-redis/v9 v9.7.0 h1:HhLSs+B6O021gwzl+locl0zEDnyNkxMtf/Z3NNBMa9E=
|
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=
|
github.com/redis/go-redis/v9 v9.7.0/go.mod h1:f6zhXITC7JUJIlPEiBOTXxJgPLdZcA93GewI7inzyWw=
|
||||||
|
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
|
||||||
|
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
|
||||||
|
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
|
||||||
|
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
|
||||||
|
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
|
Loading…
Reference in a new issue