Skip to content
This repository has been archived by the owner on Aug 14, 2024. It is now read-only.

Commit

Permalink
update log
Browse files Browse the repository at this point in the history
  • Loading branch information
hexahigh committed Feb 27, 2024
1 parent 1f4f804 commit b80ddba
Showing 1 changed file with 9 additions and 22 deletions.
31 changes: 9 additions & 22 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ var (
port = flag.Int("p", 8080, "Port to listen on")
compress = flag.Bool("c", false, "Enable compression")
level = flag.Int("l", 3, "Compression level")
logging = flag.Bool("log", false, "Enable logging")
dbType = flag.String("db", "sqlite", "Database type (sqlite or mysql)")
dbPass = flag.String("db:pass", "", "Database password (Unused for sqlite)")
dbUser = flag.String("db:user", "root", "Database user (Unused for sqlite)")
Expand All @@ -47,47 +46,35 @@ var downloadSpeeds []float64
var db *sql.DB
var logger *log.Logger

func init() {
flag.Parse()
if *logging {
logFile, err := os.OpenFile("log.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}

log.SetOutput(logFile)
}
}

func main() {
logger = log.New(os.Stdout, "", log.LstdFlags)
fmt.Println("Starting")
logger.Println("Starting")

// Initialize the SQLite database
var err error
switch *dbType {
case "mysql":
db, err = sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s", *dbUser, *dbPass, *dbHost, *dbDb))
if err != nil {
log.Fatal(err)
logger.Fatal(err)
}
db.SetConnMaxLifetime(time.Minute * 3)
db.SetMaxOpenConns(10)
db.SetMaxIdleConns(10)
case "sqlite":
db, err = sql.Open("sqlite3", fmt.Sprintf("file:%s?cache=shared&mode=rwc", *dbFile))
if err != nil {
log.Fatal(err)
logger.Fatal(err)
}
default:
log.Fatalf("Invalid database type: %s", *dbType)
logger.Fatalf("Invalid database type: %s", *dbType)
os.Exit(1)
}
onStart()
initDB()

fmt.Println("Started")
fmt.Println("Listening on port", *port)
logger.Println("Started")
logger.Println("Listening on port", *port)

http.HandleFunc("/exists", handleExists)
http.HandleFunc("/store", handleStore)
Expand Down Expand Up @@ -127,10 +114,10 @@ func onStart() {
for _, file := range files {
if filepath.Ext(file.Name()) != ".zst" && *compress {
// File is not compressed and compression is on, warn the user
log.Printf("Warning: File %s is not compressed, but compression is enabled. You may want to compress this file.\n", file.Name())
logger.Printf("Warning: File %s is not compressed, but compression is enabled. You may want to compress this file.\n", file.Name())
} else if filepath.Ext(file.Name()) == ".zst" && !*compress {
// File is compressed and compression is off, warn the user
log.Printf("Warning: File %s is compressed, but compression is disabled. You may want to decompress this file.\n", file.Name())
logger.Printf("Warning: File %s is compressed, but compression is disabled. You may want to decompress this file.\n", file.Name())
}
}
}
Expand Down Expand Up @@ -171,7 +158,7 @@ func initDB() {
uploaded INTEGER NOT NULL
)`)
if err != nil {
log.Fatalf("Failed to create table: %v", err)
logger.Fatalf("Failed to create table: %v", err)
}
}

Expand Down

0 comments on commit b80ddba

Please sign in to comment.