remote-i3wm-go/main.go
Simon Vieille a3ee86f81b
add config
2023-08-25 11:19:54 +02:00

76 lines
1.6 KiB
Go

package main
import (
"crypto/subtle"
"embed"
"fmt"
rice "github.com/GeertJohan/go.rice"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"html/template"
"net/http"
"os"
)
var (
templates map[string]*template.Template
//go:embed static
staticFiles embed.FS
//go:embed views/layout views/page
views embed.FS
config Config
)
func main() {
e := echo.New()
e.HideBanner = true
RI3_USERNAME := os.Getenv("RI3_USERNAME")
RI3_PASSWORD := os.Getenv("RI3_PASSWORD")
RI3_BIND := os.Getenv("RI3_BIND")
if RI3_BIND == "" {
RI3_BIND = "0.0.0.0:4000"
}
if len(os.Args) != 2 {
fmt.Errorf("Configuration requried")
os.Exit(1)
}
value, err := createConfigFromFile(os.Args[1])
if err != nil {
fmt.Printf("%+v\n", err)
fmt.Errorf("Configuration error")
os.Exit(1)
}
config = value
fmt.Printf("%+v\n", config)
assetHandler := http.FileServer(rice.MustFindBox("static").HTTPBox())
e.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
if RI3_USERNAME == "" && RI3_PASSWORD == "" {
return true, nil
}
isValidUsername := subtle.ConstantTimeCompare([]byte(username), []byte(RI3_USERNAME)) == 1
isValidPassword := subtle.ConstantTimeCompare([]byte(password), []byte(RI3_PASSWORD)) == 1
if isValidUsername && isValidPassword {
return true, nil
}
return false, nil
}))
e.GET("/", echo.WrapHandler(assetHandler))
e.GET("/static/*", echo.WrapHandler(http.StripPrefix("/static/", assetHandler)))
e.GET("/", homeController)
e.GET("/ws", wsController)
e.Logger.Fatal(e.Start(RI3_BIND))
}