remote-i3wm-go/main.go

76 lines
1.6 KiB
Go
Raw Normal View History

2023-08-24 18:41:12 +02:00
package main
import (
"crypto/subtle"
2023-08-25 09:03:49 +02:00
"embed"
2023-08-25 11:19:54 +02:00
"fmt"
2023-08-25 09:03:49 +02:00
rice "github.com/GeertJohan/go.rice"
2023-08-24 18:41:12 +02:00
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
2023-08-25 09:03:49 +02:00
"html/template"
2023-08-24 18:41:12 +02:00
"net/http"
2023-08-25 09:03:49 +02:00
"os"
2023-08-24 18:41:12 +02:00
)
2023-08-25 09:03:49 +02:00
var (
templates map[string]*template.Template
//go:embed static
staticFiles embed.FS
//go:embed views/layout views/page
2023-08-25 11:19:54 +02:00
views embed.FS
config Config
2023-08-25 09:03:49 +02:00
)
2023-08-24 18:41:12 +02:00
2023-08-25 09:03:49 +02:00
func main() {
e := echo.New()
e.HideBanner = true
2023-08-24 20:33:21 +02:00
2023-08-25 09:03:49 +02:00
RI3_USERNAME := os.Getenv("RI3_USERNAME")
RI3_PASSWORD := os.Getenv("RI3_PASSWORD")
RI3_BIND := os.Getenv("RI3_BIND")
2023-08-24 21:35:11 +02:00
2023-08-25 09:03:49 +02:00
if RI3_BIND == "" {
RI3_BIND = "0.0.0.0:4000"
2023-08-24 18:41:12 +02:00
}
2023-08-25 11:19:54 +02:00
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)
2023-08-25 09:03:49 +02:00
assetHandler := http.FileServer(rice.MustFindBox("static").HTTPBox())
2023-08-24 22:11:40 +02:00
2023-08-25 09:03:49 +02:00
e.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
if RI3_USERNAME == "" && RI3_PASSWORD == "" {
return true, nil
2023-08-24 21:35:11 +02:00
}
2023-08-25 09:03:49 +02:00
isValidUsername := subtle.ConstantTimeCompare([]byte(username), []byte(RI3_USERNAME)) == 1
isValidPassword := subtle.ConstantTimeCompare([]byte(password), []byte(RI3_PASSWORD)) == 1
2023-08-24 18:41:12 +02:00
2023-08-25 09:03:49 +02:00
if isValidUsername && isValidPassword {
2023-08-24 18:41:12 +02:00
return true, nil
}
return false, nil
}))
2023-08-25 09:03:49 +02:00
e.GET("/", echo.WrapHandler(assetHandler))
e.GET("/static/*", echo.WrapHandler(http.StripPrefix("/static/", assetHandler)))
e.GET("/", homeController)
e.GET("/ws", wsController)
2023-08-24 18:41:12 +02:00
2023-08-25 09:03:49 +02:00
e.Logger.Fatal(e.Start(RI3_BIND))
2023-08-24 18:41:12 +02:00
}