add golang backend for login an d webserver

This commit is contained in:
Adrian Zürcher
2025-05-28 21:48:30 +02:00
parent c8b27813ae
commit 8cfb720c42
15 changed files with 761 additions and 0 deletions

14
backend/utils/hash.go Normal file
View File

@@ -0,0 +1,14 @@
package utils
import "golang.org/x/crypto/bcrypt"
// Hash password
func HashPassword(password string) (string, error) {
b, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
return string(b), err
}
// Check password
func CheckPassword(password, hash string) bool {
return bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) == nil
}

14
backend/utils/secret.go Normal file
View File

@@ -0,0 +1,14 @@
package utils
import (
"crypto/rand"
)
func GenerateJWTSecret(length int) ([]byte, error) {
bytes := make([]byte, length)
_, err := rand.Read(bytes)
if err != nil {
return nil, err
}
return bytes, nil
}

46
backend/utils/utils.go Normal file
View File

@@ -0,0 +1,46 @@
package utils
import (
"fmt"
"os/exec"
"runtime"
"github.com/tecamino/tecamino-logger/logging"
)
func OpenBrowser(url string, logger *logging.Logger) error {
var commands [][]string
switch runtime.GOOS {
case "windows":
// Try with Chrome in kiosk mode
commands = [][]string{
{`C:\Program Files\Google\Chrome\Application\chrome.exe`, "--kiosk", url},
{"rundll32", "url.dll,FileProtocolHandler", url}, // fallback
}
case "darwin":
// macOS: open with Chrome in kiosk
commands = [][]string{
{"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", "--kiosk", url},
{"open", url}, // fallback
}
default: // Linux
commands = [][]string{
{"chromium-browser", "--kiosk", url},
{"google-chrome", "--kiosk", url},
{"firefox", "--kiosk", url},
{"xdg-open", url}, // fallback
}
}
for _, cmd := range commands {
execCmd := exec.Command(cmd[0], cmd[1:]...)
if err := execCmd.Start(); err == nil {
return nil
} else {
logger.Error("utils.OpenBrowser", err)
}
}
return fmt.Errorf("could not open browser")
}