-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
52 changed files
with
2,970 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package dashboard | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"log/slog" | ||
"net/http" | ||
"os" | ||
"time" | ||
|
||
"github.com/amalshaji/portr/internal/client/config" | ||
"github.com/amalshaji/portr/internal/client/dashboard/handler" | ||
"github.com/amalshaji/portr/internal/client/dashboard/service" | ||
"github.com/amalshaji/portr/internal/client/dashboard/ui/dist" | ||
"github.com/amalshaji/portr/internal/client/db" | ||
"github.com/amalshaji/portr/internal/client/vite" | ||
"github.com/amalshaji/portr/internal/constants" | ||
"github.com/amalshaji/portr/internal/utils" | ||
"github.com/gofiber/fiber/v2" | ||
"github.com/gofiber/fiber/v2/middleware/filesystem" | ||
"github.com/gofiber/fiber/v2/middleware/recover" | ||
"github.com/gofiber/template/django/v3" | ||
) | ||
|
||
type Dashboard struct { | ||
app *fiber.App | ||
config *config.Config | ||
db *db.Db | ||
logger *slog.Logger | ||
port int | ||
} | ||
|
||
func New(db *db.Db, config *config.Config) *Dashboard { | ||
engine := django.New("./internal/client/dashboard/templates", ".html") | ||
engine.SetAutoEscape(false) | ||
|
||
app := fiber.New(fiber.Config{ | ||
DisableStartupMessage: true, | ||
Views: engine, | ||
}) | ||
|
||
app.Use(recover.New()) | ||
|
||
if config.UseVite { | ||
app.Static("/", "./internal/server/admin/web/dist") | ||
app.Static("/static", "./internal/client/dashboard/static") | ||
} else { | ||
app.Use("/static", filesystem.New(filesystem.Config{ | ||
Root: http.FS(dist.EmbededDirStatic), | ||
PathPrefix: "static", | ||
})) | ||
} | ||
|
||
rootTemplateView := func(c *fiber.Ctx) error { | ||
return c.Render("index", fiber.Map{ | ||
"UseVite": config.UseVite, | ||
"ViteTags": vite.GenerateViteTags(constants.ClientUiViteDistDir), | ||
}) | ||
} | ||
|
||
service := service.New(db, config) | ||
handler := handler.New(config, service) | ||
|
||
app.Get("/", rootTemplateView) | ||
app.Get("/:id", rootTemplateView) | ||
|
||
tunnelsGroup := app.Group("/api/tunnels") | ||
handler.RegisterTunnelRoutes(tunnelsGroup) | ||
|
||
return &Dashboard{ | ||
app: app, | ||
config: config, | ||
db: db, | ||
logger: utils.GetLogger(), | ||
port: 7777, | ||
} | ||
} | ||
|
||
func (d *Dashboard) Start() { | ||
fmt.Println("Dashboard running on http://localhost:7777") | ||
|
||
if err := d.app.Listen(":" + fmt.Sprint(d.port)); err != nil && !errors.Is(err, http.ErrServerClosed) { | ||
if d.config.Debug { | ||
d.logger.Error("failed to start dashboard server", "error", err) | ||
} | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func (d *Dashboard) Shutdown() { | ||
if d.config.Debug { | ||
d.logger.Info("stopping dashboard server") | ||
} | ||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
|
||
defer func() { cancel() }() | ||
|
||
if err := d.app.ShutdownWithContext(ctx); err != nil { | ||
if d.config.Debug { | ||
d.logger.Error("failed to stop dashboard server", "error", err) | ||
} | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package handler | ||
|
||
import ( | ||
"log/slog" | ||
|
||
"github.com/amalshaji/portr/internal/client/config" | ||
"github.com/amalshaji/portr/internal/client/dashboard/service" | ||
"github.com/amalshaji/portr/internal/utils" | ||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
type Handler struct { | ||
config *config.Config | ||
service *service.Service | ||
log *slog.Logger | ||
} | ||
|
||
func New(config *config.Config, service *service.Service) *Handler { | ||
return &Handler{ | ||
config: config, | ||
service: service, | ||
log: utils.GetLogger(), | ||
} | ||
} | ||
|
||
func (h *Handler) RegisterTunnelRoutes(group fiber.Router) { | ||
group.Get("/", h.GetTunnels) | ||
group.Get("/render/:id", h.RenderResponse) | ||
group.Get("/:subdomain/:port", h.GetRequests) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package handler | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
func (h *Handler) GetTunnels(c *fiber.Ctx) error { | ||
tunnels, err := h.service.GetTunnels() | ||
if err != nil { | ||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"message": "failed to get tunnels"}) | ||
} | ||
|
||
return c.JSON(fiber.Map{"tunnels": tunnels}) | ||
} | ||
|
||
func (h *Handler) GetRequests(c *fiber.Ctx) error { | ||
subdomain := c.Params("subdomain") | ||
port := c.Params("port") | ||
tunnels, err := h.service.GetRequests(subdomain, port) | ||
if err != nil { | ||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"message": "failed to get requests"}) | ||
} | ||
|
||
return c.JSON(fiber.Map{"requests": tunnels}) | ||
} | ||
|
||
func (h *Handler) RenderResponse(c *fiber.Ctx) error { | ||
requestId := c.Params("id") | ||
request, err := h.service.GetRequestById(requestId) | ||
if err != nil { | ||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"message": "failed to get requests"}) | ||
} | ||
|
||
headersMap := make(map[string][]string) | ||
err = json.Unmarshal([]byte(request.ResponseHeaders), &headersMap) | ||
if err != nil { | ||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"message": "failed to parse response headers"}) | ||
} | ||
|
||
contentType := headersMap["Content-Type"] | ||
if len(contentType) == 0 { | ||
contentType = []string{"text/html; charset=utf-8"} | ||
} | ||
|
||
contentLength := headersMap["Content-Length"] | ||
if len(contentLength) == 0 { | ||
contentLength = []string{fmt.Sprintf("%d", len(request.ResponseBody))} | ||
} | ||
|
||
c.Response().Header.Set("Content-Type", contentType[0]) | ||
c.Response().Header.Set("Content-Length", contentLength[0]) | ||
|
||
c.Response().BodyWriter().Write(request.ResponseBody) | ||
return nil | ||
} |
Oops, something went wrong.