remote-i3wm-go/main.go

76 lines
1.7 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
actions Actions
)
func main() {
e := echo.New()
e.HideBanner = true
if len(os.Args) != 2 {
fmt.Errorf("Configuration required!")
os.Exit(1)
}
value, err := createConfigFromFile(os.Args[1])
if err != nil {
fmt.Printf("Configuration error:")
fmt.Printf("%+v\n", err)
os.Exit(1)
}
config = value
e.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
if config.Server.Auth.Username == "" && config.Server.Auth.Password == "" {
return true, nil
}
isValidUsername := subtle.ConstantTimeCompare([]byte(username), []byte(config.Server.Auth.Username)) == 1
isValidPassword := subtle.ConstantTimeCompare([]byte(password), []byte(config.Server.Auth.Password)) == 1
if isValidUsername && isValidPassword {
return true, nil
}
return false, nil
}))
assetHandler := http.FileServer(rice.MustFindBox("static").HTTPBox())
actions = createActions()
e.GET("/static/*", echo.WrapHandler(http.StripPrefix("/static/", assetHandler)))
e.GET("/manifest.webmanifest", manifestController)
e.GET("/", homeController)
e.GET("/ws", wsController)
if config.Server.Tls.Enable == false {
e.Logger.Fatal(e.Start(config.Server.Listen))
} else {
e.Logger.Fatal(e.StartTLS(
config.Server.Listen,
config.Server.Tls.CertFile,
config.Server.Tls.CertKeyFile,
))
}
}