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 run: develop
bin/develop bin/develop
develop: develop:
go build -tags Develop -o bin/develop go build -tags Develop -o bin/develop
release: release:

View File

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

21
main.go
View File

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

View File

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