remote-i3wm-go/cmd/main.go

69 lines
1.6 KiB
Go

package main
import (
"crypto/subtle"
"fmt"
"net/http"
"os"
rice "github.com/GeertJohan/go.rice"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"gitnet.fr/deblan/remote-i3wm-go/internal/config"
"gitnet.fr/deblan/remote-i3wm-go/internal/handler"
)
func main() {
e := echo.New()
e.HideBanner = true
if len(os.Args) != 2 {
fmt.Errorf("Configuration required!")
os.Exit(1)
}
value, err := config.CreateConfigFromFile(os.Args[1])
if err != nil {
fmt.Printf("Configuration error:")
fmt.Printf("%+v\n", err)
os.Exit(1)
}
conf := value
e.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
if conf.Server.Auth.Username == "" && conf.Server.Auth.Password == "" {
return true, nil
}
isValidUsername := subtle.ConstantTimeCompare([]byte(username), []byte(conf.Server.Auth.Username)) == 1
isValidPassword := subtle.ConstantTimeCompare([]byte(password), []byte(conf.Server.Auth.Password)) == 1
if isValidUsername && isValidPassword {
return true, nil
}
return false, nil
}))
assetHandler := http.FileServer(rice.MustFindBox("../static").HTTPBox())
e.GET("/static/*", echo.WrapHandler(http.StripPrefix("/static/", assetHandler)))
e.GET("/manifest.webmanifest", handler.ManifestHandler)
e.GET("/ws", handler.WsHandler)
e.GET("/capture", handler.CaptureHandler)
e.GET("/", func(c echo.Context) error {
return handler.HomeHandler(c, conf)
})
if conf.Server.Tls.Enable == false {
e.Logger.Fatal(e.Start(conf.Server.Listen))
} else {
e.Logger.Fatal(e.StartTLS(
conf.Server.Listen,
conf.Server.Tls.CertFile,
conf.Server.Tls.CertKeyFile,
))
}
}