code refactoring

This commit is contained in:
2024-04-10 12:02:04 +02:00
parent 566f0f2ac2
commit 2f02293a52
6 changed files with 16 additions and 57 deletions

View File

@@ -17,9 +17,9 @@ type LoginRequest struct {
}
func Login(w http.ResponseWriter, r *http.Request) {
req := new(LoginRequest)
err := json.NewDecoder(r.Body).Decode(req)
if err != nil {
var req LoginRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

View File

@@ -3,7 +3,6 @@ package internal
import (
"encoding/gob"
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"
@@ -35,33 +34,6 @@ func (m *MemoryDB) Set(process *Process) string {
return id
}
// Update a process info/metadata, given the process id
//
// Deprecated: will be removed anytime soon.
func (m *MemoryDB) UpdateInfo(id string, info DownloadInfo) error {
entry, ok := m.table.Load(id)
if ok {
entry.(*Process).Info = info
m.table.Store(id, entry)
return nil
}
return fmt.Errorf("can't update row with id %s", id)
}
// Update a process progress data, given the process id
// Used for updating completition percentage or ETA.
//
// Deprecated: will be removed anytime soon.
func (m *MemoryDB) UpdateProgress(id string, progress DownloadProgress) error {
entry, ok := m.table.Load(id)
if ok {
entry.(*Process).Progress = progress
m.table.Store(id, entry)
return nil
}
return fmt.Errorf("can't update row with id %s", id)
}
// Removes a process progress, given the process id
func (m *MemoryDB) Delete(id string) {
m.table.Delete(id)
@@ -92,7 +64,7 @@ func (m *MemoryDB) All() *[]ProcessResponse {
return &running
}
// WIP: Persist the database in a single file named "session.dat"
// Persist the database in a single file named "session.dat"
func (m *MemoryDB) Persist() error {
running := m.All()
@@ -115,17 +87,16 @@ func (m *MemoryDB) Persist() error {
return nil
}
// WIP: Restore a persisted state
// Restore a persisted state
func (m *MemoryDB) Restore(logger *slog.Logger) {
fd, err := os.Open("session.dat")
if err != nil {
return
}
session := Session{}
var session Session
err = gob.NewDecoder(fd).Decode(&session)
if err != nil {
if err := gob.NewDecoder(fd).Decode(&session); err != nil {
return
}

View File

@@ -29,17 +29,15 @@ func PlaylistDetect(req DownloadRequest, mq *MessageQueue, db *MemoryDB, logger
return err
}
m := metadata{}
var m metadata
err = cmd.Start()
if err != nil {
if err := cmd.Start(); err != nil {
return err
}
logger.Info("decoding metadata", slog.String("url", req.URL))
err = json.NewDecoder(stdout).Decode(&m)
if err != nil {
if err := json.NewDecoder(stdout).Decode(&m); err != nil {
return err
}

View File

@@ -9,7 +9,7 @@ import (
var (
logsChan = make(chan rxgo.Item, 100)
logsObservable = rxgo.
FromChannel(logsChan, rxgo.WithBackPressureStrategy(rxgo.Drop)).
FromEventSource(logsChan, rxgo.WithBackPressureStrategy(rxgo.Drop)).
BufferWithTime(rxgo.WithDuration(time.Millisecond * 500))
)
@@ -20,9 +20,7 @@ func NewObservableLogger() *ObservableLogger {
}
func (o *ObservableLogger) Write(p []byte) (n int, err error) {
go func() {
logsChan <- rxgo.Of(string(p))
}()
logsChan <- rxgo.Of(string(p))
n = len(p)
err = nil

View File

@@ -11,20 +11,18 @@ import (
)
func validateToken(tokenValue string) error {
if tokenValue == "" {
return errors.New("invalid token")
}
token, _ := jwt.Parse(tokenValue, func(t *jwt.Token) (interface{}, error) {
token, err := jwt.Parse(tokenValue, func(t *jwt.Token) (interface{}, error) {
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", t.Header["alg"])
}
return []byte(os.Getenv("JWT_SECRET")), nil
})
if err != nil {
return err
}
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
expiresAt, err := time.Parse(time.RFC3339, claims["expiresAt"].(string))
if err != nil {
return err
}