Skip to content

Commit 85d5363

Browse files
committed
⚡ Better frontend asset serving
Signed-off-by: Muhammed Hussain Karimi <[email protected]>
1 parent cf984fb commit 85d5363

File tree

3 files changed

+38
-26
lines changed

3 files changed

+38
-26
lines changed

cmd/root.go

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -212,27 +212,36 @@ func start(_ *cobra.Command, _ []string) {
212212

213213
uiGroup := rootGroup.Group("/ui")
214214
uiGroup.Any("", nil, addTrailingSlashMiddleware)
215+
uiCache := make(map[string]string)
215216
uiGroup.GET("/*", func(c echo.Context) error {
216217
prefix := filepath.Join("/"+strings.TrimPrefix(cfg.BaseURI, "/"), "/ui/")
217218
filePath := strings.TrimPrefix(strings.TrimPrefix(c.Request().URL.Path, prefix), "/")
218-
file, err := ui.MainFS.Open(filePath)
219-
if err != nil {
220-
filePath = "index.html"
221-
file, _ = ui.MainFS.Open(filePath)
222-
}
223-
defer file.Close()
224-
buf := new(bytes.Buffer)
225-
_, err = io.Copy(buf, file)
226-
if err != nil {
227-
return err
228-
}
229-
newURI, err := neturl.JoinPath("/BASE_URI/", "../"+cfg.BaseURI+"/")
230-
if err != nil {
231-
return err
219+
modifiedContent, ok := uiCache[filePath]
220+
if !ok {
221+
file, err := ui.MainFS.Open(filePath)
222+
if err != nil {
223+
filePath = "index.html"
224+
file, _ = ui.MainFS.Open(filePath)
225+
}
226+
defer file.Close()
227+
buf := new(bytes.Buffer)
228+
_, err = io.Copy(buf, file)
229+
if err != nil {
230+
return err
231+
}
232+
newURI, err := neturl.JoinPath("/BASE_URI/", "../"+cfg.BaseURI+"/")
233+
if err != nil {
234+
return err
235+
}
236+
modifiedContent = strings.ReplaceAll(buf.String(), "/BASE_URI/", newURI)
237+
uiCache[filePath] = modifiedContent
232238
}
233-
modifiedContent := strings.ReplaceAll(buf.String(), "/BASE_URI/", newURI)
234239
fileParts := strings.Split(filePath, ".")
235-
return c.Blob(200, mime.TypeByExtension("."+fileParts[len(fileParts)-1]), []byte(modifiedContent))
240+
mimeType := mime.TypeByExtension("." + fileParts[len(fileParts)-1])
241+
if mimeType == "" {
242+
mimeType = "text/html; charset=utf-8"
243+
}
244+
return c.Blob(200, mimeType, []byte(modifiedContent))
236245
})
237246

238247
if configuration.CurrentConfig.RunServer {
@@ -243,7 +252,6 @@ func start(_ *cobra.Command, _ []string) {
243252
)
244253
}
245254

246-
commandGroup := apiGroup.Group("/command", checkUserAdmin)
247-
commandGroup.DELETE("/remove-old-links", url.RemoveOldIds)
248-
255+
commandGroup := apiGroup.Group("/command")
256+
commandGroup.DELETE("/remove-old-links", url.RemoveUnusedUrls)
249257
}

constrains/params.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ const (
66
ShortCodeParamName = "ShortCode"
77
IdParamName = "Id"
88
RefererQueryParam = "referer"
9+
CutoffQueryParamName = "Cutoff"
910
)

internal/endpoint/url/url.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,25 @@ func Delete(c echo.Context) error {
8080
return c.NoContent(http.StatusNoContent)
8181
}
8282

83-
func RemoveOldIds(c echo.Context) error {
83+
func RemoveUnusedUrls(c echo.Context) error {
8484
user := c.Get(constrains.UserInfoContextVar).(databasemodels.User)
85+
cutoffStr := c.QueryParam(constrains.CutoffQueryParamName) // Example: 2006-01-02T15:04:05Z07:00
86+
if cutoffStr == "" {
87+
cutoffStr = time.Now().AddDate(0, -1, 0).Format(time.RFC3339)
88+
}
8589

86-
// Define cutoff date (6 months ago)
87-
cutoff := time.Now().AddDate(0, -1, 0)
90+
cutoff, err := time.Parse(time.RFC3339, cutoffStr)
91+
if err != nil {
92+
return err
93+
}
8894

89-
// Prepare query conditions
9095
session := database.Engine.Where("last_visited_at < ?", cutoff)
9196

9297
if !user.Admin {
93-
// Non-admins can only delete their own URLs
9498
session = session.And("creator_id = ?", user.Id)
9599
}
96100

97-
// Delete matching records
98-
_, err := session.Delete(&databasemodels.Url{})
101+
_, err = session.Delete(&databasemodels.Url{})
99102
if err != nil {
100103
return err
101104
}

0 commit comments

Comments
 (0)