2024-12-27 14:04:18 -03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"net/http"
|
2025-01-08 20:16:19 -03:00
|
|
|
"net/url"
|
2025-01-07 23:42:16 -03:00
|
|
|
"os"
|
2024-12-27 14:04:18 -03:00
|
|
|
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2025-01-07 23:42:16 -03:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
2024-12-27 14:04:18 -03:00
|
|
|
)
|
|
|
|
|
|
|
|
var rctx = context.Background()
|
|
|
|
var config = loadConfig("./config.json")
|
2025-01-07 23:42:16 -03:00
|
|
|
var logger = log.Logger.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
2024-12-27 14:04:18 -03:00
|
|
|
|
2025-01-08 20:16:19 -03:00
|
|
|
var client = &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
Proxy: func(r *http.Request) (*url.URL, error) {
|
|
|
|
if os.Getenv("PROXY") != "" {
|
|
|
|
return url.Parse(os.Getenv("PROXY"))
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just a small handler to add headers on all responses
|
|
|
|
func middleware(next http.HandlerFunc) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
next(w, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-27 14:04:18 -03:00
|
|
|
func main() {
|
2025-01-08 20:16:19 -03:00
|
|
|
// s := &SevenTv{}
|
|
|
|
// xd, _ := s.getPaints()
|
|
|
|
// logger.Trace().Msg(xd)
|
2024-12-27 14:04:18 -03:00
|
|
|
// redisDb := redis.NewClient(&redis.Options{
|
|
|
|
// Addr: config.Redis.Addr,
|
|
|
|
// DB: config.Redis.DB,
|
|
|
|
// })
|
|
|
|
|
2025-01-08 20:16:19 -03:00
|
|
|
db, err := sql.Open("sqlite3", config.DbPath)
|
2024-12-27 14:04:18 -03:00
|
|
|
if err != nil {
|
2025-01-07 23:42:16 -03:00
|
|
|
logger.Fatal().Msg(err.Error())
|
2024-12-27 14:04:18 -03:00
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
2025-01-08 20:16:19 -03:00
|
|
|
err = createDb(db)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal().Msgf("Failed to create DB: '%s'", err.Error())
|
|
|
|
}
|
2024-12-27 14:04:18 -03:00
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
|
|
|
mux.HandleFunc("/", root)
|
2025-01-08 20:16:19 -03:00
|
|
|
mux.HandleFunc("/badges", middleware(getAllBadges))
|
|
|
|
// mux.HandleFunc("/7tv/paints", middleware(sevenTvPaints))
|
|
|
|
// mux.HandleFunc("/7tv/badges", middleware(sevenTvBadges))
|
|
|
|
// // CONSIDERATION: github.com/gorilla/mux may be an option to make use of something like this:
|
|
|
|
// // mux.HandleFunc("/7tv/cosmetics/{user}", middleware(sevenTvUserCosmetics))
|
|
|
|
// // For now I will just trim the path
|
|
|
|
// mux.HandleFunc("/7tv/cosmetics/", middleware(sevenTvUserCosmetics))
|
2024-12-27 14:04:18 -03:00
|
|
|
|
|
|
|
srv := &http.Server{
|
|
|
|
Addr: config.HTTPServer.Addr + ":" + config.HTTPServer.Port,
|
2025-01-08 20:16:19 -03:00
|
|
|
Handler: mux,
|
2024-12-27 14:04:18 -03:00
|
|
|
}
|
|
|
|
|
2025-01-07 23:42:16 -03:00
|
|
|
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 {}
|
2024-12-27 14:04:18 -03:00
|
|
|
}
|