Skip to content

Commit

Permalink
🔧 feat: add golangci-lint config and improve code organization
Browse files Browse the repository at this point in the history
The commit includes:
- Add golangci-lint configuration file
- Reorganize route handlers for better readability
- Remove unused wordWrap function
- Fix code formatting and imports order
  • Loading branch information
watzon committed Nov 18, 2024
1 parent b0fc470 commit 2a9ce17
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 40 deletions.
38 changes: 38 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
linters:
enable:
- gofmt
- govet
- gosimple
- staticcheck
- unused
- misspell
- ineffassign
- goimports

run:
# Timeout for analysis
timeout: 5m

# Output configuration
output:
# Format: colored-line-number|line-number|json|tab|checkstyle|code-climate|junit-xml|github-actions
formats:
- format: colored-line-number

# Print lines of code with issue
print-issued-lines: true

# Print linter name in the end of issue text
print-linter-name: true

issues:
exclude-dirs:
- uploads
- tmp
exclude-files:
- ".*\\.pb\\.go$"

# Linters settings
linters-settings:
goimports:
local-prefixes: github.com/your-username/0x45
27 changes: 13 additions & 14 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,10 @@ func (s *Server) SetupRoutes() {
keys.Post("/request", s.handlers.APIKey.HandleRequestAPIKey)
keys.Get("/verify", s.handlers.APIKey.HandleVerifyAPIKey)

// Paste routes
pastes := s.app.Group("/p")
pastes.Post("/", s.middleware.Auth.Auth(false), s.handlers.Paste.HandleUpload)
pastes.Get("/list", s.middleware.Auth.Auth(true), s.handlers.Paste.HandleListPastes)
pastes.Delete("/:id", s.middleware.Auth.Auth(false), s.handlers.Paste.HandleDeletePaste)
pastes.Put("/:id/expiry", s.middleware.Auth.Auth(true), s.handlers.Paste.HandleUpdateExpiration)
// URL redirect route - must be before the group to avoid auth middleware
s.app.Get("/u/:id", s.handlers.URL.HandleRedirect)

// URL routes
// URL management routes
urls := s.app.Group("/u")
urls.Use(s.middleware.Auth.Auth(true))
urls.Post("/", s.handlers.URL.HandleURLShorten)
Expand All @@ -134,8 +130,14 @@ func (s *Server) SetupRoutes() {
urls.Delete("/:id", s.handlers.URL.HandleDeleteURL)
urls.Put("/:id/expiry", s.handlers.URL.HandleUpdateURLExpiration)

// Public paste routes
// Handle paste routes with extensions
// Paste routes - authenticated routes first
pastes := s.app.Group("/p")
pastes.Post("/", s.middleware.Auth.Auth(false), s.handlers.Paste.HandleUpload)
pastes.Get("/list", s.middleware.Auth.Auth(true), s.handlers.Paste.HandleListPastes)
pastes.Delete("/:id", s.middleware.Auth.Auth(false), s.handlers.Paste.HandleDeletePaste)
pastes.Put("/:id/expiry", s.middleware.Auth.Auth(true), s.handlers.Paste.HandleUpdateExpiration)

// Public paste routes - extension routes first (more specific)
s.app.Get("/p/:id.:ext", func(c *fiber.Ctx) error {
c.Locals("extension", c.Params("ext"))
return s.handlers.Paste.HandleView(c)
Expand All @@ -153,14 +155,11 @@ func (s *Server) SetupRoutes() {
return s.handlers.Paste.HandleGetPasteImage(c)
})

// Handle paste routes without extensions
// Non-extension paste routes last (more general)
s.app.Get("/p/:id/raw", s.handlers.Paste.HandleRawView)
s.app.Get("/p/:id/download", s.handlers.Paste.HandleDownload)
s.app.Delete("/p/:id/:key", s.handlers.Paste.HandleDeleteWithKey)
s.app.Get("/p/:id/image", s.handlers.Paste.HandleGetPasteImage)

// Handle URL redirects
s.app.Get("/u/:id", s.handlers.URL.HandleRedirect)
s.app.Delete("/p/:id/:key", s.handlers.Paste.HandleDeleteWithKey)
}

// Error handler
Expand Down
23 changes: 0 additions & 23 deletions internal/server/services/ogimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,29 +92,6 @@ func DefaultImageConfig() ImageConfig {
}
}

// wordWrap wraps text at the specified width
func wordWrap(text string, dc *gg.Context, maxWidth float64) []string {
var lines []string
words := strings.Fields(text)
if len(words) == 0 {
return []string{text}
}

currentLine := words[0]

for _, word := range words[1:] {
width, _ := dc.MeasureString(currentLine + " " + word)
if width <= maxWidth {
currentLine += " " + word
} else {
lines = append(lines, currentLine)
currentLine = word
}
}
lines = append(lines, currentLine)
return lines
}

type codeImageContext struct {
dc *gg.Context
style *chroma.Style
Expand Down
3 changes: 2 additions & 1 deletion internal/server/services/services.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package services

import (
"time"

"github.com/watzon/0x45/internal/config"
"go.uber.org/zap"
"gorm.io/gorm"
"time"
)

// Services holds all service instances
Expand Down
4 changes: 2 additions & 2 deletions internal/utils/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ func GetFilenameFromURL(urlStr string) string {
if err != nil {
return ""
}

// Get the last segment of the path
segments := strings.Split(u.Path, "/")
for i := len(segments) - 1; i >= 0; i-- {
if segments[i] != "" {
return segments[i]
}
}

return ""
}

0 comments on commit 2a9ce17

Please sign in to comment.