Skip to content

Commit

Permalink
Merge pull request #5 from IgorPolyakov/main
Browse files Browse the repository at this point in the history
New project struct, init work on frontend & add user and team pages
  • Loading branch information
IgorPolyakov committed Apr 20, 2024
2 parents 1cb4ef2 + b091981 commit 2cdfcaa
Show file tree
Hide file tree
Showing 50 changed files with 3,096 additions and 303 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
- name: WriteGoList
run: cd src && go list -json -m all > ../go.list
run: go list -json -m all > go.list
- name: Nancy
uses: sonatype-nexus-community/nancy-github-action@main
continue-on-error: true
Expand All @@ -55,4 +55,3 @@ jobs:
uses: reviewdog/action-golangci-lint@v2
with:
go_version: 1.22
workdir: ./src
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ $ go build main.go

```shell
$ cd src
$ go run main.go
$ go run cmd/ctf01d/main.go
```

## Run DB for local devlopment
Expand Down
15 changes: 12 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,19 @@

## Общее

- [ ] Авторизация (пока простая)
- [ ] Авторизация
- [ ] BasicAuth
- [ ] Регистрацию думаю пока не надо - пусть через админку будет добавление новых
- [x] База данных (postgre) - версионирование чтобы проще было обновлять сервер.
- [ ] CI/CD - линтеры, securego/gosec, сборка контейнера и отправка в regestry
- [ ] База данных (postgre)
- [x] Переехать с mysql на psql.
- [ ] Версионирование чтобы проще было обновлять сервер.
- [ ] Сделать изменения через миграции, а не init.sql
- [ ] Добавить seed?
- [ ] CI/CD
- [x] линтер
- [x] проверка зависимостей nancy
- [ ] сборка контейнера и отправка в regestry


## Админ

Expand Down
32 changes: 16 additions & 16 deletions api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ info:
contact: {}
version: 1.0.0
servers:
- url: 'https://api.ctf01d.com'
- url: 'https://ctf01d.com'
description: Production server
- url: 'https://api.staging.ctf01d.com'
- url: 'https://staging.ctf01d.com'
description: Staging server
- url: 'http://localhost:4102'
description: Local server
paths:
/users:
/api/users:
get:
tags:
- Users
Expand Down Expand Up @@ -44,7 +44,7 @@ paths:
responses:
'201':
description: User created successfully
'/users/{id}':
'/api/users/{id}':
get:
tags:
- Users
Expand Down Expand Up @@ -103,7 +103,7 @@ paths:
responses:
'204':
description: User deleted successfully
/games:
/api/games:
get:
tags:
- Games
Expand Down Expand Up @@ -135,7 +135,7 @@ paths:
responses:
'201':
description: Game created successfully
'/games/{id}':
'/api/games/{id}':
get:
tags:
- Games
Expand Down Expand Up @@ -194,7 +194,7 @@ paths:
responses:
'200':
description: Game deleted successfully
/teams:
/api/teams:
get:
tags:
- Teams
Expand Down Expand Up @@ -226,7 +226,7 @@ paths:
responses:
'201':
description: Team created successfully
'/teams/{id}':
'/api/teams/{id}':
get:
tags:
- Teams
Expand Down Expand Up @@ -285,7 +285,7 @@ paths:
responses:
'204':
description: Team deleted successfully
/results:
/api/results:
get:
tags:
- Results
Expand Down Expand Up @@ -317,7 +317,7 @@ paths:
responses:
'201':
description: Result created successfully
'/results/{id}':
'/api/results/{id}':
get:
tags:
- Results
Expand All @@ -338,7 +338,7 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/ResultResponse'
/services:
/api/services:
get:
tags:
- Services
Expand Down Expand Up @@ -370,7 +370,7 @@ paths:
responses:
'201':
description: Service created successfully
'/services/{id}':
'/api/services/{id}':
get:
tags:
- Services
Expand Down Expand Up @@ -607,10 +607,10 @@ components:
description: Boolean indicating if the service is public
TeamRequest:
required:
- team_name
- name
type: object
properties:
team_name:
name:
type: string
description: Name of the team
description:
Expand All @@ -628,13 +628,13 @@ components:
TeamResponse:
required:
- id
- team_name
- name
type: object
properties:
id:
type: string
description: Unique identifier for the team
team_name:
name:
type: string
description: Name of the team
description:
Expand Down
4 changes: 2 additions & 2 deletions src/main.go → cmd/ctf01d/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"ctf01d/config"
"ctf01d/routers"
config "ctf01d/configs"
"ctf01d/internal/app/routers"
"database/sql"
"log"
"net/http"
Expand Down
2 changes: 1 addition & 1 deletion src/config/config.go → configs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type (
func NewConfig() (*Config, error) {
cfg := &Config{}

err := cleanenv.ReadConfig("./config/config.yml", cfg)
err := cleanenv.ReadConfig("./configs/config.yml", cfg)
if err != nil {
return nil, fmt.Errorf("%w", err)
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions src/lib/api/games.go → internal/app/api/games.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package api

import (
"ctf01d/lib/models"
"ctf01d/lib/repository"
"ctf01d/lib/view"
"ctf01d/internal/app/models"
"ctf01d/internal/app/repository"
"ctf01d/internal/app/view"
"database/sql"
"encoding/json"
"log"
Expand All @@ -22,7 +22,7 @@ type RequestGame struct {
func CreateGameHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) {
var game RequestGame
if err := json.NewDecoder(r.Body).Decode(&game); err != nil {
respondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid request payload"})
respondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid request payload: " + err.Error()})
return
}
if game.EndTime.Before(game.StartTime) {
Expand Down
File renamed without changes.
File renamed without changes.
96 changes: 96 additions & 0 deletions internal/app/api/teams.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package api

import (
"ctf01d/internal/app/models"
"ctf01d/internal/app/repository"
"ctf01d/internal/app/view"
"database/sql"
"encoding/json"
"net/http"

"github.com/gorilla/mux"
)

type RequestTeam struct {
Name string `json:"name"`
SocialLinks string `json:"social_links"`
Description string `json:"description"`
UniversityId string `json:"university_id"`
}

func CreateTeamHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) {
var team RequestTeam
if err := json.NewDecoder(r.Body).Decode(&team); err != nil {
respondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid request payload: " + err.Error()})
return
}

teamRepo := repository.NewTeamRepository(db)
newTeam := &models.Team{
Name: team.Name,
SocialLinks: team.SocialLinks,
Description: team.Description,
UniversityId: team.UniversityId,
}
if err := teamRepo.Create(r.Context(), newTeam); err != nil {
respondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to create team: " + err.Error()})
return
}
respondWithJSON(w, http.StatusOK, map[string]string{"data": "Team created successfully"})
}

func DeleteTeamHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
teamRepo := repository.NewTeamRepository(db)
if err := teamRepo.Delete(r.Context(), id); err != nil {
respondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to delete team"})
return
}
respondWithJSON(w, http.StatusOK, map[string]string{"data": "Team deleted successfully"})
}

func GetTeamByIdHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
teamRepo := repository.NewTeamRepository(db)
team, err := teamRepo.GetById(r.Context(), id)
if err != nil {
respondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to fetch team"})
return
}
respondWithJSON(w, http.StatusOK, view.NewTeamFromModel(team))
}

func ListTeamsHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) {
teamRepo := repository.NewTeamRepository(db)
teams, err := teamRepo.List(r.Context())
if err != nil {
respondWithJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
return
}
respondWithJSON(w, http.StatusOK, view.NewTeamsFromModels(teams))
}

func UpdateTeamHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) {
var team RequestTeam
if err := json.NewDecoder(r.Body).Decode(&team); err != nil {
respondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid request payload"})
return
}
teamRepo := repository.NewTeamRepository(db)
updateTeam := &models.Team{
Name: team.Name,
SocialLinks: team.SocialLinks,
Description: team.Description,
UniversityId: team.UniversityId,
}
vars := mux.Vars(r)
id := vars["id"]
updateTeam.Id = id
if err := teamRepo.Update(r.Context(), updateTeam); err != nil {
respondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
return
}
respondWithJSON(w, http.StatusOK, map[string]string{"data": "Team updated successfully"})
}
30 changes: 30 additions & 0 deletions internal/app/api/university.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package api

import (
"ctf01d/internal/app/models"
"ctf01d/internal/app/repository"
"ctf01d/internal/app/view"
"database/sql"
"net/http"
)

func ListUniversitiesHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) {
queryParam := r.URL.Query().Get("term")

universityRepo := repository.NewUniversityRepository(db)
var universities []*models.University
var err error

if queryParam != "" {
universities, err = universityRepo.Search(r.Context(), queryParam)
} else {
universities, err = universityRepo.List(r.Context())
}

if err != nil {
respondWithJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
return
}
respondWithJSON(w, http.StatusOK, view.NewUniversitiesFromModels(universities))

}
6 changes: 3 additions & 3 deletions src/lib/api/users.go → internal/app/api/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package api

import (
"crypto/sha1"
"ctf01d/lib/models"
"ctf01d/lib/repository"
"ctf01d/lib/view"
"ctf01d/internal/app/models"
"ctf01d/internal/app/repository"
"ctf01d/internal/app/view"
"database/sql"
"encoding/hex"
"encoding/json"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions internal/app/models/team.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package models

type Team struct {
Id string `db:"id"`
Name string `db:"name"`
Description string `db:"description"`
UniversityId string `db:"university_id"`
SocialLinks string `db:"social_links"`
AvatarUrl string `db:"avatar_url"`
}
6 changes: 6 additions & 0 deletions internal/app/models/university.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package models

type University struct {
Id string `db:"id"`
Name string `db:"name"`
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package repository

import (
"context"
"ctf01d/lib/models"
"ctf01d/internal/app/models"
"database/sql"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package repository

import (
"context"
"ctf01d/lib/models"
"ctf01d/internal/app/models"
"database/sql"
)

Expand Down
Loading

0 comments on commit 2cdfcaa

Please sign in to comment.