Added Flags
This commit is contained in:
@@ -2,10 +2,8 @@
|
|||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
const port = 8080
|
func getSecret() (string, error) {
|
||||||
|
return "test", nil
|
||||||
func getSecret() string {
|
|
||||||
return "test"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDbPath() string {
|
func getDbPath() string {
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ func (s *Server) Auth(next http.Handler) http.Handler {
|
|||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if cookie != nil && cookie.Value == s.secret {
|
if s.secret == "" || (cookie != nil && 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)
|
||||||
|
|||||||
42
main.go
42
main.go
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@@ -15,20 +16,39 @@ import (
|
|||||||
"github.com/pablu23/mangaGetter/internal/server"
|
"github.com/pablu23/mangaGetter/internal/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
secretFlag = flag.String("secret", "", "Secret to use for Auth")
|
||||||
|
authFlag = flag.Bool("auth", false, "Use Auth, does not need to be set if secret or secret-path is set")
|
||||||
|
secretFilePathFlag = flag.String("secret-path", "", "Path to file with ONLY secret in it")
|
||||||
|
portFlag = flag.Int("port", 80, "The port on which to host")
|
||||||
|
serverFlag = flag.Bool("server", false, "If false dont open Browser with Address")
|
||||||
|
databaseFlag = flag.String("database", "", "Path to sqlite.db file")
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
openBrowser := true
|
var secret string = ""
|
||||||
var filePath string
|
var filePath string
|
||||||
var secret string
|
|
||||||
if len(os.Args) >= 2 {
|
flag.Parse()
|
||||||
openBrowser = false
|
if secretFlag != nil && *secretFlag != "" {
|
||||||
filePath = os.Args[2]
|
secret = *secretFlag
|
||||||
buf, err := os.ReadFile(os.Args[3])
|
} else if secretFilePathFlag != nil && *secretFilePathFlag != "" {
|
||||||
|
buf, err := os.ReadFile(*secretFilePathFlag)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
secret = string(buf)
|
secret = string(buf)
|
||||||
|
} else if *authFlag {
|
||||||
|
cacheSecret, err := getSecret()
|
||||||
|
secret = cacheSecret
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Secret file could not be found or read because of %s, not activating Auth\n", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if databaseFlag != nil && *databaseFlag != "" {
|
||||||
|
filePath = *databaseFlag
|
||||||
} else {
|
} else {
|
||||||
secret = getSecret()
|
|
||||||
filePath = getDbPath()
|
filePath = getDbPath()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,7 +59,7 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
secret = strings.TrimSpace(secret)
|
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)
|
||||||
|
|
||||||
@@ -52,17 +72,17 @@ func main() {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if openBrowser {
|
if serverFlag != nil && !*serverFlag {
|
||||||
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", *portFlag))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
err = s.Start(port)
|
err = s.Start(*portFlag)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|||||||
10
release.go
10
release.go
@@ -7,21 +7,19 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
const port = 80
|
func getSecret() (string, error) {
|
||||||
|
|
||||||
func getSecret() string {
|
|
||||||
dir, err := os.UserCacheDir()
|
dir, err := os.UserCacheDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
dirPath := filepath.Join(dir, "MangaGetter")
|
dirPath := filepath.Join(dir, "MangaGetter")
|
||||||
filePath := filepath.Join(dirPath, "secret.secret")
|
filePath := filepath.Join(dirPath, "secret.secret")
|
||||||
buf, err := os.ReadFile(filePath)
|
buf, err := os.ReadFile(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return "", err
|
||||||
}
|
}
|
||||||
return string(buf)
|
return string(buf), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDbPath() string {
|
func getDbPath() string {
|
||||||
|
|||||||
Reference in New Issue
Block a user