package main import ( "context" "database/sql" "net/http" "net/url" "os" _ "github.com/mattn/go-sqlite3" "github.com/rs/zerolog" "github.com/rs/zerolog/log" ) var rctx = context.Background() var config = loadConfig("./config.json") var logger = log.Logger.Output(zerolog.ConsoleWriter{Out: os.Stderr}) 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) } } func main() { // s := &SevenTv{} // xd, _ := s.getPaints() // logger.Trace().Msg(xd) // redisDb := redis.NewClient(&redis.Options{ // Addr: config.Redis.Addr, // DB: config.Redis.DB, // }) db, err := sql.Open("sqlite3", config.DbPath) if err != nil { logger.Fatal().Msg(err.Error()) } defer db.Close() err = createDb(db) if err != nil { logger.Fatal().Msgf("Failed to create DB: '%s'", err.Error()) } mux := http.NewServeMux() mux.HandleFunc("/", root) mux.HandleFunc("/badges", middleware(getAllBadges)) mux.HandleFunc("/7tv/badges", middleware(getSevenTvBadges)) mux.HandleFunc("/7tv/paints", middleware(getSevenTvPaints)) // 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)) srv := &http.Server{ Addr: config.HTTPServer.Addr + ":" + config.HTTPServer.Port, Handler: mux, } 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 {} }