package main
import (
"html/template"
"log"
"github.com/gofiber/contrib/websocket"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/compress"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/gofiber/template/html/v2"
"git.tukangketik.id/swanadiva/hostkeeper/v2/internal/handler"
)
func main() {
engine := html.New("./views", ".html")
engine.Reload(true)
engine.AddFunc("svg", func(name string) template.HTML {
switch name {
case "server":
return ``
case "terminal":
return ``
case "folder-sync":
return ``
case "code2":
return ``
case "key-round":
return ``
case "settings2":
return ``
default:
return ``
}
})
engine.AddFunc("dict", func(values ...interface{}) map[string]interface{} {
m := make(map[string]interface{})
for i := 0; i < len(values)-1; i += 2 {
if key, ok := values[i].(string); ok {
m[key] = values[i+1]
}
}
return m
})
engine.AddFunc("default", func(def, val interface{}) interface{} {
if val == nil || val == "" {
return def
}
return val
})
app := fiber.New(fiber.Config{
Views: engine,
})
app.Use(logger.New())
app.Use(recover.New())
app.Use(compress.New())
app.Static("/static", "./static")
app.Get("/", handler.Dashboard)
app.Get("/hosts", handler.Dashboard)
app.Get("/hosts/list", handler.DashboardList)
app.Post("/hosts", handler.CreateHost)
app.Put("/hosts/:id", handler.UpdateHost)
app.Delete("/hosts/:id", handler.DeleteHost)
app.Get("/snippets", handler.Snippets)
app.Get("/snippets/grid", handler.SnippetGrid)
app.Post("/snippets", handler.CreateSnippet)
app.Delete("/snippets/:id", handler.DeleteSnippet)
app.Get("/keychain", handler.Keychain)
app.Get("/keys/grid", handler.KeyGrid)
app.Post("/keys", handler.CreateKey)
app.Delete("/keys/:id", handler.DeleteKey)
app.Get("/settings", handler.Settings)
app.Post("/settings", handler.UpdateConfig)
app.Get("/brief", handler.Brief)
app.Get("/terminal", handler.Terminal)
app.Get("/terminal/ws/:host", websocket.New(func(c *websocket.Conn) {
handler.TerminalWS(c)
}, websocket.Config{
Origins: []string{"*"},
}))
app.Get("/sftp", handler.SFTP)
app.Get("/sftp/pane", handler.SFTPPane)
app.Post("/sftp/mkdir", handler.CreateFolder)
app.Delete("/sftp/rm", handler.DeleteFile)
app.Get("/api/health", handler.Health)
log.Fatal(app.Listen(":1947"))
}