diff --git a/cmd/vanity-tester-backend/config.go b/cmd/vanity-tester-backend/config.go index 650e902..669a615 100644 --- a/cmd/vanity-tester-backend/config.go +++ b/cmd/vanity-tester-backend/config.go @@ -2,8 +2,7 @@ package main import ( "encoding/json" - "io/ioutil" - "log" + "os" ) type HTTPServer struct { @@ -16,33 +15,43 @@ type Redis struct { DB int `json:"db"` } +type Gql struct { + ClientID string `json:"clientId"` +} + type Config struct { HTTPServer HTTPServer `json:"http_server"` Redis Redis `json:"redis"` + Gql Gql `json:"gql"` } 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", + Addr: "127.0.0.1", + Port: "8080", }, Redis: Redis{ Addr: "127.0.0.1:6379", 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) 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 diff --git a/cmd/vanity-tester-backend/endpoints.go b/cmd/vanity-tester-backend/endpoints.go index 10f8530..6b4095a 100644 --- a/cmd/vanity-tester-backend/endpoints.go +++ b/cmd/vanity-tester-backend/endpoints.go @@ -1,9 +1,10 @@ package main import ( + "io" "net/http" ) func root(w http.ResponseWriter, req *http.Request) { - return + io.WriteString(w, "WIP Vanity Tester Backend") } diff --git a/cmd/vanity-tester-backend/exporters.go b/cmd/vanity-tester-backend/exporters.go deleted file mode 100644 index 06ab7d0..0000000 --- a/cmd/vanity-tester-backend/exporters.go +++ /dev/null @@ -1 +0,0 @@ -package main diff --git a/cmd/vanity-tester-backend/gql.go b/cmd/vanity-tester-backend/gql.go new file mode 100644 index 0000000..f6955cf --- /dev/null +++ b/cmd/vanity-tester-backend/gql.go @@ -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) +} diff --git a/cmd/vanity-tester-backend/main.go b/cmd/vanity-tester-backend/main.go index ff5ae07..f75442b 100644 --- a/cmd/vanity-tester-backend/main.go +++ b/cmd/vanity-tester-backend/main.go @@ -3,15 +3,18 @@ package main import ( "context" "database/sql" - "log" "net/http" + "os" _ "github.com/mattn/go-sqlite3" + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" ) var client *http.Client var rctx = context.Background() var config = loadConfig("./config.json") +var logger = log.Logger.Output(zerolog.ConsoleWriter{Out: os.Stderr}) func main() { // redisDb := redis.NewClient(&redis.Options{ @@ -21,7 +24,7 @@ func main() { db, err := sql.Open("sqlite3", "./database.db") if err != nil { - log.Fatal(err) + logger.Fatal().Msg(err.Error()) } defer db.Close() @@ -38,6 +41,14 @@ func main() { Addr: config.HTTPServer.Addr + ":" + config.HTTPServer.Port, } - srv.ListenAndServe() - log.Printf("Server running at %s:%s", config.HTTPServer.Addr, config.HTTPServer.Port) + logger.Info().Msgf("Starting server at http://%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 {} } diff --git a/cmd/vanity-tester-backend/providers.go b/cmd/vanity-tester-backend/providers.go new file mode 100644 index 0000000..5198a9a --- /dev/null +++ b/cmd/vanity-tester-backend/providers.go @@ -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 +} diff --git a/go.mod b/go.mod index 00c411b..725a073 100644 --- a/go.mod +++ b/go.mod @@ -10,5 +10,9 @@ require ( require ( github.com/cespare/xxhash/v2 v2.2.0 // 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/rs/zerolog v1.33.0 // indirect + golang.org/x/sys v0.29.0 // indirect ) diff --git a/go.sum b/go.sum index 8a50db7..563cf57 100644 --- a/go.sum +++ b/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/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/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/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/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/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=