Skip to content

Commit 24b440f

Browse files
committed
refactor(api): 🎨 rename utils to helper, lint files abd add automaxprocs
1 parent 40f6838 commit 24b440f

27 files changed

+164
-161
lines changed

cmd/main.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import (
55
"net/http"
66
"os"
77

8+
"github.com/go-chi/chi/v5"
9+
"github.com/go-chi/cors"
10+
_ "github.com/lib/pq"
11+
_ "go.uber.org/automaxprocs"
12+
813
"ctf01d/internal/config"
914
"ctf01d/internal/handler"
1015
migration "ctf01d/internal/migrations/psql"
1116
"ctf01d/internal/server"
12-
13-
"github.com/go-chi/chi/v5"
14-
"github.com/go-chi/cors"
15-
_ "github.com/lib/pq"
1617
)
1718

1819
func main() {

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
github.com/jaswdr/faker/v2 v2.3.0
1212
github.com/lib/pq v1.2.0
1313
github.com/oapi-codegen/runtime v1.1.1
14+
go.uber.org/automaxprocs v1.5.3
1415
golang.org/x/crypto v0.24.0
1516
)
1617

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ github.com/oapi-codegen/runtime v1.1.1/go.mod h1:SK9X900oXmPWilYR5/WKPzt3Kqxn/uS
3838
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
3939
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4040
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
41+
github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g=
42+
github.com/prashantv/gostub v1.1.0/go.mod h1:A5zLQHz7ieHGG7is6LLXLz7I8+3LZzsrV0P1IAHhP5U=
4143
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
4244
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
4345
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
@@ -46,6 +48,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
4648
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
4749
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
4850
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
51+
go.uber.org/automaxprocs v1.5.3 h1:kWazyxZUrS3Gs4qUpbwo5kEIMGe/DAvi5Z4tl2NW4j8=
52+
go.uber.org/automaxprocs v1.5.3/go.mod h1:eRbA25aqJrxAbsLO0xy5jVwPt7FQnRgjW+efnwa1WM0=
4953
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
5054
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
5155
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

internal/handler/games.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@ import (
55
"log/slog"
66
"net/http"
77

8+
openapi_types "github.com/oapi-codegen/runtime/types"
9+
10+
"ctf01d/internal/helper"
811
"ctf01d/internal/model"
912
"ctf01d/internal/repository"
1013
"ctf01d/internal/server"
11-
api_helpers "ctf01d/internal/utils"
12-
13-
openapi_types "github.com/oapi-codegen/runtime/types"
1414
)
1515

1616
func (h *Handler) CreateGame(w http.ResponseWriter, r *http.Request) {
1717
var game server.GameRequest
1818
var err error
1919
if err := json.NewDecoder(r.Body).Decode(&game); err != nil {
2020
slog.Warn(err.Error(), "handler", "CreateGame")
21-
api_helpers.RespondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid request payload"})
21+
helper.RespondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid request payload"})
2222
return
2323
}
2424
if game.EndTime.Before(game.StartTime) {
25-
api_helpers.RespondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "EndTime must be after StartTime"})
25+
helper.RespondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "EndTime must be after StartTime"})
2626
return
2727
}
2828
repo := repository.NewGameRepository(h.DB)
@@ -35,55 +35,55 @@ func (h *Handler) CreateGame(w http.ResponseWriter, r *http.Request) {
3535
err = repo.Create(r.Context(), newGame)
3636
if err != nil {
3737
slog.Warn(err.Error(), "handler", "CreateGame")
38-
api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to create game"})
38+
helper.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to create game"})
3939
return
4040
}
41-
api_helpers.RespondWithJSON(w, http.StatusOK, newGame.ToResponse())
41+
helper.RespondWithJSON(w, http.StatusOK, newGame.ToResponse())
4242
}
4343

4444
func (h *Handler) DeleteGame(w http.ResponseWriter, r *http.Request, id openapi_types.UUID) {
4545
repo := repository.NewGameRepository(h.DB)
4646
if err := repo.Delete(r.Context(), id); err != nil {
4747
slog.Warn(err.Error(), "handler", "DeleteGame")
48-
api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to delete game"})
48+
helper.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to delete game"})
4949
return
5050
}
51-
api_helpers.RespondWithJSON(w, http.StatusOK, map[string]string{"data": "Game deleted successfully"})
51+
helper.RespondWithJSON(w, http.StatusOK, map[string]string{"data": "Game deleted successfully"})
5252
}
5353

5454
func (h *Handler) GetGameById(w http.ResponseWriter, r *http.Request, id openapi_types.UUID) {
5555
repo := repository.NewGameRepository(h.DB)
5656
game, err := repo.GetGameDetails(r.Context(), id) // короткий ответ, если нужен см. GetById
5757
if err != nil {
5858
slog.Warn(err.Error(), "handler", "GetGameById")
59-
api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to fetch game"})
59+
helper.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to fetch game"})
6060
return
6161
}
62-
api_helpers.RespondWithJSON(w, http.StatusOK, game.ToResponseGameDetails())
62+
helper.RespondWithJSON(w, http.StatusOK, game.ToResponseGameDetails())
6363
}
6464

6565
func (h *Handler) ListGames(w http.ResponseWriter, r *http.Request) {
6666
repo := repository.NewGameRepository(h.DB)
6767
games, err := repo.ListGamesDetails(r.Context())
6868
if err != nil {
6969
slog.Warn(err.Error(), "handler", "ListGames")
70-
api_helpers.RespondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "Failed to fetch games"})
70+
helper.RespondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "Failed to fetch games"})
7171
return
7272
}
7373
gameResponses := make([]*server.GameResponse, 0, len(games))
7474
for _, game := range games {
7575
gameResponses = append(gameResponses, game.ToResponseGameDetails())
7676
}
7777

78-
api_helpers.RespondWithJSON(w, http.StatusOK, gameResponses)
78+
helper.RespondWithJSON(w, http.StatusOK, gameResponses)
7979
}
8080

8181
func (h *Handler) UpdateGame(w http.ResponseWriter, r *http.Request, id openapi_types.UUID) {
8282
// fixme update не проверяет есть ли запись в бд
8383
var game server.GameRequest
8484
if err := json.NewDecoder(r.Body).Decode(&game); err != nil {
8585
slog.Warn(err.Error(), "handler", "UpdateGame")
86-
api_helpers.RespondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid request payload"})
86+
helper.RespondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid request payload"})
8787
return
8888
}
8989
repo := repository.NewGameRepository(h.DB)
@@ -96,8 +96,8 @@ func (h *Handler) UpdateGame(w http.ResponseWriter, r *http.Request, id openapi_
9696
err := repo.Update(r.Context(), updateGame)
9797
if err != nil {
9898
slog.Warn(err.Error(), "handler", "UpdateGame")
99-
api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Invalid request payload"})
99+
helper.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Invalid request payload"})
100100
return
101101
}
102-
api_helpers.RespondWithJSON(w, http.StatusOK, map[string]string{"data": "Game updated successfully"})
102+
helper.RespondWithJSON(w, http.StatusOK, map[string]string{"data": "Game updated successfully"})
103103
}

internal/handler/interface.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package handler
33
import (
44
"net/http"
55

6-
"ctf01d/internal/server"
7-
86
openapi_types "github.com/oapi-codegen/runtime/types"
7+
8+
"ctf01d/internal/server"
99
)
1010

1111
// ServerInterfaceWrapper wraps Handler to conform to the generated interface

internal/handler/results.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ import (
55
"log/slog"
66
"net/http"
77

8+
openapi_types "github.com/oapi-codegen/runtime/types"
9+
10+
"ctf01d/internal/helper"
811
"ctf01d/internal/model"
912
"ctf01d/internal/repository"
1013
"ctf01d/internal/server"
11-
api_helpers "ctf01d/internal/utils"
12-
13-
openapi_types "github.com/oapi-codegen/runtime/types"
1414
)
1515

1616
func (h *Handler) CreateResult(w http.ResponseWriter, r *http.Request, gameId openapi_types.UUID) {
1717
var result server.ResultRequest
1818
var err error
1919
if err := json.NewDecoder(r.Body).Decode(&result); err != nil {
2020
slog.Warn(err.Error(), "handler", "CreateResultHandler")
21-
api_helpers.RespondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid request payload"})
21+
helper.RespondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid request payload"})
2222
return
2323
}
2424
repo := repository.NewResultRepository(h.DB)
@@ -29,10 +29,10 @@ func (h *Handler) CreateResult(w http.ResponseWriter, r *http.Request, gameId op
2929
}
3030
if err = repo.Create(r.Context(), newResult); err != nil {
3131
slog.Warn(err.Error(), "handler", "CreateResultHandler")
32-
api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to create result"})
32+
helper.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to create result"})
3333
return
3434
}
35-
api_helpers.RespondWithJSON(w, http.StatusOK, newResult.ToResponse(0))
35+
helper.RespondWithJSON(w, http.StatusOK, newResult.ToResponse(0))
3636
}
3737

3838
func (h *Handler) GetResult(w http.ResponseWriter, r *http.Request, gameId openapi_types.UUID, resultId openapi_types.UUID) {
@@ -41,17 +41,17 @@ func (h *Handler) GetResult(w http.ResponseWriter, r *http.Request, gameId opena
4141
if err != nil {
4242
slog.Warn(err.Error(), "handler", "GetResult")
4343
// todo - empty result ?
44-
api_helpers.RespondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "Failed to fetch result"})
44+
helper.RespondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "Failed to fetch result"})
4545
return
4646
}
47-
api_helpers.RespondWithJSON(w, http.StatusOK, result.ToResponse(0))
47+
helper.RespondWithJSON(w, http.StatusOK, result.ToResponse(0))
4848
}
4949

5050
func (h *Handler) UpdateResult(w http.ResponseWriter, r *http.Request, gameId openapi_types.UUID, resultId openapi_types.UUID) {
5151
var resultRequest server.ResultRequest
5252
if err := json.NewDecoder(r.Body).Decode(&resultRequest); err != nil {
5353
slog.Warn(err.Error(), "handler", "UpdateResult")
54-
api_helpers.RespondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid request payload"})
54+
helper.RespondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid request payload"})
5555
return
5656
}
5757

@@ -65,11 +65,11 @@ func (h *Handler) UpdateResult(w http.ResponseWriter, r *http.Request, gameId op
6565

6666
if err := repo.Update(r.Context(), result); err != nil {
6767
slog.Warn(err.Error(), "handler", "UpdateResult")
68-
api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to update result"})
68+
helper.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to update result"})
6969
return
7070
}
7171

72-
api_helpers.RespondWithJSON(w, http.StatusOK, result.ToResponse(0))
72+
helper.RespondWithJSON(w, http.StatusOK, result.ToResponse(0))
7373
}
7474

7575
// GetScoreboard retrieves the scoreboard for a given game ID
@@ -78,10 +78,10 @@ func (h *Handler) GetScoreboard(w http.ResponseWriter, r *http.Request, gameId o
7878
results, err := repo.List(r.Context(), gameId)
7979
if err != nil {
8080
slog.Warn(err.Error(), "handler", "GetScoreboard")
81-
api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to fetch scoreboard"})
81+
helper.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to fetch scoreboard"})
8282
return
8383
}
8484

8585
scoreboard := model.NewScoreboardFromResults(results)
86-
api_helpers.RespondWithJSON(w, http.StatusOK, scoreboard)
86+
helper.RespondWithJSON(w, http.StatusOK, scoreboard)
8787
}

internal/handler/services.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,93 +5,93 @@ import (
55
"log/slog"
66
"net/http"
77

8+
openapi_types "github.com/oapi-codegen/runtime/types"
9+
10+
"ctf01d/internal/helper"
811
"ctf01d/internal/model"
912
"ctf01d/internal/repository"
1013
"ctf01d/internal/server"
11-
api_helpers "ctf01d/internal/utils"
12-
13-
openapi_types "github.com/oapi-codegen/runtime/types"
1414
)
1515

1616
func (h *Handler) CreateService(w http.ResponseWriter, r *http.Request) {
1717
var service server.ServiceRequest
1818
var err error
1919
if err := json.NewDecoder(r.Body).Decode(&service); err != nil {
2020
slog.Warn(err.Error(), "handler", "CreateServiceHandler")
21-
api_helpers.RespondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid request payload"})
21+
helper.RespondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid request payload"})
2222
return
2323
}
2424
repo := repository.NewServiceRepository(h.DB)
2525
newService := &model.Service{
2626
Name: service.Name,
2727
Author: service.Author,
28-
LogoUrl: api_helpers.ToNullString(service.LogoUrl),
28+
LogoUrl: helper.ToNullString(service.LogoUrl),
2929
Description: *service.Description,
3030
IsPublic: service.IsPublic,
3131
}
3232
if err = repo.Create(r.Context(), newService); err != nil {
3333
slog.Warn(err.Error(), "handler", "CreateServiceHandler")
34-
api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to create service"})
34+
helper.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to create service"})
3535
return
3636
}
37-
api_helpers.RespondWithJSON(w, http.StatusOK, newService.ToResponse())
37+
helper.RespondWithJSON(w, http.StatusOK, newService.ToResponse())
3838
}
3939

4040
func (h *Handler) DeleteService(w http.ResponseWriter, r *http.Request, id openapi_types.UUID) {
4141
repo := repository.NewServiceRepository(h.DB)
4242
if err := repo.Delete(r.Context(), id); err != nil {
4343
slog.Warn(err.Error(), "handler", "DeleteServiceHandler")
44-
api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to delete service"})
44+
helper.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to delete service"})
4545
return
4646
}
47-
api_helpers.RespondWithJSON(w, http.StatusOK, map[string]string{"data": "Service deleted successfully"})
47+
helper.RespondWithJSON(w, http.StatusOK, map[string]string{"data": "Service deleted successfully"})
4848
}
4949

5050
func (h *Handler) GetServiceById(w http.ResponseWriter, r *http.Request, id openapi_types.UUID) {
5151
repo := repository.NewServiceRepository(h.DB)
5252
service, err := repo.GetById(r.Context(), id)
5353
if err != nil {
5454
slog.Warn(err.Error(), "handler", "GetServiceByIdHandler")
55-
api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to fetch service"})
55+
helper.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to fetch service"})
5656
return
5757
}
58-
api_helpers.RespondWithJSON(w, http.StatusOK, service.ToResponse())
58+
helper.RespondWithJSON(w, http.StatusOK, service.ToResponse())
5959
}
6060

6161
func (h *Handler) ListServices(w http.ResponseWriter, r *http.Request) {
6262
repo := repository.NewServiceRepository(h.DB)
6363
services, err := repo.List(r.Context())
6464
if err != nil {
6565
slog.Warn(err.Error(), "handler", "ListServicesHandler")
66-
api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to fetch services"})
66+
helper.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to fetch services"})
6767
return
6868
}
69-
api_helpers.RespondWithJSON(w, http.StatusOK, model.NewServiceFromModels(services))
69+
helper.RespondWithJSON(w, http.StatusOK, model.NewServiceFromModels(services))
7070
}
7171

7272
func (h *Handler) UpdateService(w http.ResponseWriter, r *http.Request, id openapi_types.UUID) {
7373
var sr server.ServiceRequest
7474
if err := json.NewDecoder(r.Body).Decode(&sr); err != nil {
7575
slog.Warn(err.Error(), "handler", "UpdateService")
76-
api_helpers.RespondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid request payload"})
76+
helper.RespondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid request payload"})
7777
return
7878
}
7979
repo := repository.NewServiceRepository(h.DB)
8080
service := &model.Service{
8181
Id: id,
8282
Name: sr.Name,
8383
Author: sr.Author,
84-
LogoUrl: api_helpers.ToNullString(sr.LogoUrl),
84+
LogoUrl: helper.ToNullString(sr.LogoUrl),
8585
Description: *sr.Description,
8686
IsPublic: sr.IsPublic,
8787
}
8888
err := repo.Update(r.Context(), service)
8989
if err != nil {
9090
slog.Warn(err.Error(), "handler", "UpdateService")
91-
api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Invalid request payload"})
91+
helper.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Invalid request payload"})
9292
return
9393
}
94-
api_helpers.RespondWithJSON(w, http.StatusOK, map[string]string{"data": "Service updated successfully"})
94+
helper.RespondWithJSON(w, http.StatusOK, map[string]string{"data": "Service updated successfully"})
9595
}
9696

9797
// fixme implement

0 commit comments

Comments
 (0)