Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.21'
go-version: '1.24'

- name: Install dependencies
run: go mod tidy
Expand Down
14 changes: 13 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,16 @@ module Autotester

go 1.23

require github.com/joho/godotenv v1.5.1
require github.com/joho/godotenv v1.5.1

require (
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.27.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
golang.org/x/crypto v0.33.0 // indirect
golang.org/x/net v0.34.0 // indirect
golang.org/x/sys v0.30.0 // indirect
golang.org/x/text v0.22.0 // indirect
)
18 changes: 18 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.27.0 h1:w8+XrWVMhGkxOaaowyKH35gFydVHOvC0/uWoy2Fzwn4=
github.com/go-playground/validator/v10 v10.27.0/go.mod h1:I5QpIEbmr8On7W0TktmJAumgzX4CA1XNl4ZmDuVHKKo=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M=
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
2 changes: 1 addition & 1 deletion internal/domain/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package domain

// UrlRequest represents the payload for URL scanning requests.
type UrlRequest struct {
Url string `json:"url"`
Url string `json:"url" validate:"required,http_url"`
Tests []string `json:"tests,omitempty"`
}

Expand Down
2 changes: 1 addition & 1 deletion internal/handlers/checkurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (h *CheckUrlHandler) Check(w http.ResponseWriter, req *http.Request) {
res.ErrorResponce(w, "Failed to parse JSON: "+err.Error(), http.StatusBadRequest)
return
}
if err := util.ValidateUrl(&payload.Url); err != nil {
if err := util.ValidateUrl(&payload); err != nil {
log.Println("URL validation failed:", err)
res.ErrorResponce(w, err.Error(), http.StatusBadRequest)
return
Expand Down
36 changes: 22 additions & 14 deletions internal/util/Validator.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
package util

import (
"Autotester/internal/domain"
"errors"
"net/url"
"strings"
"log"

"github.com/go-playground/validator/v10"
)

// ValidateUrl validates the given URL string pointer.
func ValidateUrl(link *string) error {
if link == nil || *link == "" {
return errors.New("url cannot be empty")
}
if !strings.HasPrefix(*link, "http://") && !strings.HasPrefix(*link, "https://") {
return errors.New("url does not start with http:// or https://")
}
parsed, err := url.ParseRequestURI(*link)
func ValidateUrl(payload *domain.UrlRequest) error {
// if link == nil || *link == "" {
// return errors.New("url cannot be empty")
// }
// if !strings.HasPrefix(*link, "http://") && !strings.HasPrefix(*link, "https://") {
// return errors.New("url does not start with http:// or https://")
// }
// parsed, err := url.ParseRequestURI(*link)
// if err != nil {
// return errors.New("wrong url format")
// }
// if parsed.Host == "" || !strings.Contains(parsed.Host, ".") {
// return errors.New("url must have a hostname")
// }
validate := validator.New()
err := validate.Struct(*payload)
if err != nil {
return errors.New("wrong url format")
}
if parsed.Host == "" || !strings.Contains(parsed.Host, ".") {
return errors.New("url must have a hostname")
log.Println("Validation error:", err)
return errors.New("Validator error: " + err.Error())
}
return nil
}