remote-i3wm-go/main.go

76 lines
1.7 KiB
Go
Raw Permalink 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
views embed.FS
config Config
actions Actions
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 11:19:54 +02:00
if len(os.Args) != 2 {
2023-08-26 11:41:43 +02:00
fmt.Errorf("Configuration required!")
2023-08-25 11:19:54 +02:00
os.Exit(1)
}
value, err := createConfigFromFile(os.Args[1])
if err != nil {
2023-08-26 11:41:43 +02:00
fmt.Printf("Configuration error:")
2023-08-25 11:19:54 +02:00
fmt.Printf("%+v\n", err)
os.Exit(1)
}
config = value
2023-08-25 09:03:49 +02:00
e.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
2023-08-26 11:41:43 +02:00
if config.Server.Auth.Username == "" && config.Server.Auth.Password == "" {
2023-08-25 09:03:49 +02:00
return true, nil
2023-08-24 21:35:11 +02:00
}
2023-08-26 11:41:43 +02:00
isValidUsername := subtle.ConstantTimeCompare([]byte(username), []byte(config.Server.Auth.Username)) == 1
isValidPassword := subtle.ConstantTimeCompare([]byte(password), []byte(config.Server.Auth.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
}
2023-08-26 11:41:43 +02:00
2023-08-24 18:41:12 +02:00
return false, nil
}))
2023-08-26 11:41:43 +02:00
assetHandler := http.FileServer(rice.MustFindBox("static").HTTPBox())
actions = createActions()
2023-08-26 11:41:43 +02:00
2023-08-25 09:03:49 +02:00
e.GET("/static/*", echo.WrapHandler(http.StripPrefix("/static/", assetHandler)))
2023-12-06 17:54:32 +01:00
e.GET("/manifest.webmanifest", manifestController)
2023-08-25 09:03:49 +02:00
e.GET("/", homeController)
e.GET("/ws", wsController)
2023-08-24 18:41:12 +02:00
2023-11-17 18:50:03 +01:00
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,
))
}
2023-08-24 18:41:12 +02:00
}