Fixed middleware

This commit is contained in:
Pablu23
2024-05-22 22:16:50 +02:00
parent 392114b240
commit f905d482a5
4 changed files with 32 additions and 20 deletions

View File

@@ -1,6 +1,5 @@
run: develop
bin/develop
develop:
go build -tags Develop -o bin/develop
release:

View File

@@ -6,16 +6,12 @@ import (
func (s *Server) Auth(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("auth")
if err != nil {
cookie, _ := r.Cookie("auth")
if r.URL.Path == "/login" || r.URL.Path == "/login/" {
next.ServeHTTP(w, r)
return
}
http.Redirect(w, r, "/login", http.StatusFound)
return
}
if cookie.Value == s.secret {
if cookie != nil && cookie.Value == s.secret {
next.ServeHTTP(w, r)
} else {
http.Redirect(w, r, "/login", http.StatusFound)

21
main.go
View File

@@ -7,6 +7,7 @@ import (
"os/exec"
"os/signal"
"runtime"
"strings"
"time"
"github.com/pablu23/mangaGetter/internal/database"
@@ -15,7 +16,21 @@ import (
)
func main() {
filePath := getDbPath()
openBrowser := true
var filePath string
var secret string
if len(os.Args) >= 2 {
openBrowser = false
filePath = os.Args[2]
buf, err := os.ReadFile(os.Args[3])
if err != nil {
panic(err)
}
secret = string(buf)
} else {
secret = getSecret()
filePath = getDbPath()
}
db := database.NewDatabase(filePath, true)
err := db.Open()
@@ -24,7 +39,7 @@ func main() {
return
}
secret := getSecret()
secret = strings.TrimSpace(secret)
mux := http.NewServeMux()
s := server.New(&provider.Bato{}, &db, mux, secret)
@@ -37,6 +52,7 @@ func main() {
}
}()
if openBrowser {
go func() {
time.Sleep(300 * time.Millisecond)
err := open(fmt.Sprintf("http://localhost:%d", port))
@@ -44,6 +60,7 @@ func main() {
fmt.Println(err)
}
}()
}
err = s.Start(port)
if err != nil {

View File

@@ -7,7 +7,7 @@ import (
"path/filepath"
)
const port = 8000
const port = 80
func getSecret() string {
dir, err := os.UserCacheDir()