add scraping the torrent topics rss files

This commit is contained in:
2024-09-09 08:54:29 +03:00
parent 20c4f25c55
commit e587e645fc
26 changed files with 714 additions and 22 deletions

31
internal/bootstrap.go Normal file
View File

@@ -0,0 +1,31 @@
package scraper
import (
"fmt"
"github.com/spf13/viper"
"git.amok.space/yevhen/resource-scraper/internal/config"
"git.amok.space/yevhen/resource-scraper/internal/db"
"git.amok.space/yevhen/resource-scraper/pkg/handler"
"git.amok.space/yevhen/resource-scraper/pkg/repository"
"git.amok.space/yevhen/resource-scraper/pkg/service"
"git.amok.space/yevhen/resource-scraper/types"
)
func Bootstrap() {
config.New()
dbase := db.New()
repos := repository.New(dbase)
services := service.New(repos)
handlers := handler.New(services)
switch viper.GetString("role") {
case types.RoleConsole:
fmt.Printf("init console console: %s\n", handlers.InitConsole())
break
}
///http.Run()
}

34
internal/config/config.go Normal file
View File

@@ -0,0 +1,34 @@
package config
import (
"fmt"
"log"
"path/filepath"
"github.com/spf13/viper"
)
func New() {
configFilePath := viper.GetString("config-file")
configDir := "./" + filepath.Dir(configFilePath)
viper.SetConfigName(filepath.Base(configFilePath))
viper.AddConfigPath(configDir)
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("fatal error config file: %w", err))
}
viper.SetDefault("ConfigDir", configDir)
scope := viper.GetString("scope")
if scope != "" {
viper.SetConfigName(scope)
viper.AddConfigPath(configDir)
err := viper.MergeInConfig()
if err != nil {
log.Fatalf("fatal error config file: %v", err)
return
}
}
}

24
internal/db/connector.go Normal file
View File

@@ -0,0 +1,24 @@
package db
import (
"github.com/jmoiron/sqlx"
"github.com/spf13/viper"
)
func New() *sqlx.DB {
drv := viper.GetString("db.driver")
dsn := viper.GetString("db.data_source_name")
db, err := sqlx.Connect(drv, dsn)
if err != nil {
panic("Failed to connect to the database: " + err.Error())
}
// Verify the connection to the database is still alive
err = db.Ping()
if err != nil {
panic("Failed to ping the database: " + err.Error())
}
return db
}

55
internal/flag.go Normal file
View File

@@ -0,0 +1,55 @@
package scraper
import (
"flag"
"fmt"
"log"
"runtime"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
const usage = `Music Database (MDB) server/cli craftware'
Usage:
mdb [command] -c [config-file-path]
Commands:
-help, -h Print this message
-version -v Print version.
-config -c Set configuration file without extension. It is %s by default.
-section -s Select section e.g.: rutracker, bandcamp etc.
The Go runtime version: %s
Report bugs to https://git.amok.space/yevhen/resource-scraper/issues`
const (
maxPasswordLength = 20
version = "0.1"
defaultConfigPath = "./config/default"
)
func ParseFlags() {
flag.Bool("h", false, "")
flag.Bool("help", false, "")
flag.Bool("v", false, "")
flag.Bool("version", false, "")
flag.Bool("debug", false, "")
flag.String("config-file", defaultConfigPath, "config file location used for the program")
flag.String("scope", "", "")
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
err := viper.BindPFlags(pflag.CommandLine)
if err != nil {
log.Fatalf("[ERR] Failed to bind flags to config: %s", err)
}
if viper.GetBool("h") || viper.GetBool("help") {
msg := fmt.Sprintf(usage, defaultConfigPath, runtime.Version())
fmt.Println(msg)
} else if viper.GetBool("v") || viper.GetBool("version") {
fmt.Println("MDB version", version)
}
}

54
internal/http/http.go Normal file
View File

@@ -0,0 +1,54 @@
package http
import (
"log"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/go-chi/cors"
"git.amok.space/yevhen/resource-scraper/helper/web"
)
/*type Server struct {
router *chi.Mux
}*/
/*type Routes struct {
Api *chi.Mux
}*/
/*type Handler func(w http.ResponseWriter, r *http.Request) error
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err := h(w, r); err != nil {
w.WriteHeader(http.StatusServiceUnavailable)
_, err := w.Write([]byte("bad"))
if err != nil {
log.Fatalf("Error starting HTTP server: %v", err)
}
}
}*/
func Run() {
port := "19576"
r := chi.NewRouter()
r.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{"https://*", "http://*"},
AllowedMethods: []string{"GET", "POST"},
AllowedHeaders: []string{"*"},
ExposedHeaders: []string{"Link"},
AllowCredentials: false,
MaxAge: 300,
}))
r.Get("/", web.FallbackHandler)
log.Printf("Server starting on port %v", port)
err := http.ListenAndServe("localhost:"+port, r)
if err != nil {
log.Fatalf("Error starting HTTP server: %v", err)
}
}