-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from denisushakov/feature/backend-init
Feature/backend init
- Loading branch information
Showing
35 changed files
with
1,543 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
TODO_PORT=7540 | ||
TODO_DBFILE="./storage/scheduler.db" | ||
TODO_PASSWORD="password" | ||
TODO_JWT_SECRET_KEY="secret_key" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
name: golang-pipeline | ||
on: push | ||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
container: golang:1.23 | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Run Unit Tests | ||
env: | ||
ENV TODO_DBFILE: /storage/scheduler.db | ||
run: GOOS=linux GOARCH=amd64 go test ./... | ||
|
||
- name: Vet | ||
run: | | ||
go vet ./... | ||
deploy: | ||
name: Push Docker image to Docker Hub | ||
runs-on: ubuntu-latest | ||
needs: test | ||
if: startsWith(github.ref, 'refs/tags') | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
|
||
- name: Set up Docker buildx | ||
uses: docker/setup-buildx-action@v3 | ||
|
||
- name: Log in to Docker Hub | ||
uses: docker/login-action@v3 | ||
with: | ||
username: ${{ secrets.DOCKER_USERNAME }} | ||
password: ${{ secrets.DOCKER_ACCESS_TOKEN }} | ||
|
||
- name: Extract metadata (tags, labels) for Docker | ||
id: meta | ||
uses: docker/[email protected] | ||
with: | ||
images: denisushakov/todo-rest | ||
|
||
- name: Build and push Docker Inage | ||
uses: docker/build-push-action@v5 | ||
with: | ||
context: . | ||
push: true | ||
tags: ${{ steps.meta.outputs.tags }} | ||
labels: ${{ steps.meta.outputs.labels }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.vscode | ||
|
||
.exe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
FROM golang:1.23.2-alpine AS builder | ||
|
||
RUN apk add --no-cache build-base | ||
|
||
WORKDIR /app | ||
|
||
COPY . . | ||
|
||
RUN go mod download | ||
|
||
RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -o my_app ./cmd/scheduler/main.go | ||
|
||
FROM alpine:3.18 | ||
|
||
ENV TODO_PORT=7540 | ||
ENV TODO_DBFILE=/app/storage/scheduler.db | ||
|
||
WORKDIR /app | ||
|
||
COPY --from=builder /app/my_app /app/ | ||
COPY --from=builder /app/.env . | ||
COPY --from=builder /app/web ./web | ||
|
||
RUN mkdir -p /app/storage | ||
|
||
CMD ["./my_app"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Denis Ushakov | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
IMAGE_NAME = todo-app | ||
CONTAINER_NAME = todo-container | ||
PORT = 7540 | ||
|
||
build: | ||
docker build -t $(IMAGE_NAME) . | ||
|
||
run: | ||
docker run -d -p $(PORT):$(PORT) --name $(CONTAINER_NAME) $(IMAGE_NAME) | ||
|
||
stop: | ||
docker stop $(CONTAINER_NAME) | ||
|
||
rm: | ||
docker rm $(CONTAINER_NAME) | ||
|
||
restart: stop rm build run | ||
|
||
exec: | ||
docker exec -it $(CONTAINER_NAME) /bin/sh | ||
|
||
rmi: | ||
docker rmi $(IMAGE_NAME) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,141 @@ | ||
# Файлы для итогового задания | ||
# Todo-Rest API | ||
|
||
В директории `tests` находятся тесты для проверки API, которое должно быть реализовано в веб-сервере. | ||
**Todo-Rest API** — это серверное приложение для управления задачами с использованием Go. Оно предоставляет REST API для создания, редактирования и удаления задач. Приложение поддерживает аутентификацию с использованием JWT-токенов, а также хранит данные задач в SQLite базе данных. | ||
|
||
Директория `web` содержит файлы фронтенда. | ||
## Описание | ||
Данное приложение представляет собой API для системы управления задачами, которое позволяет пользователям создавать задачи с указанием сроков, описания и повторяющихся событий. Также реализован механизм аутентификации и авторизации на основе токенов. | ||
|
||
## Функциональные возможности: | ||
- Создание, редактирование и удаление задач | ||
- Поддержка повторяющихся задач | ||
- Аутентификация с использованием JWT | ||
- Хранение данных в базе данных SQLite | ||
- Валидация данных на уровне API | ||
- Поддержка переменных окружения для конфигурации | ||
## Технологии | ||
- Go 1.23.2 | ||
- SQLite для хранения данных | ||
- Chi для роутинга | ||
- Docker для контейнеризации | ||
- Buildx и GitHub Actions для CI/CD | ||
- JWT для аутентификации | ||
- Окружение для разработки: WSL/Ubuntu | ||
|
||
## Установка и запуск | ||
|
||
### Локальная установка | ||
Для установки и запуска приложения на локальной машине, следуйте инструкциям ниже: | ||
|
||
1. Склонируйте репозиторий: | ||
|
||
```bash | ||
git clone https://github.com/denisushakov/todo-rest.git | ||
``` | ||
|
||
2. Перейдите в папку проекта: | ||
|
||
```bash | ||
cd todo-rest | ||
``` | ||
|
||
3. Установите зависимости: | ||
|
||
```bash | ||
go mod download | ||
``` | ||
|
||
4. Запустите приложение: | ||
|
||
```bash | ||
go run ./cmd/scheduler/main.go | ||
``` | ||
|
||
### Запуск в Docker | ||
Для запуска приложения в контейнере Docker, выполните следующие шаги: | ||
|
||
1. Соберите Docker-образ: | ||
|
||
```bash | ||
docker build -t todo-rest . | ||
``` | ||
|
||
2. Запустите контейнер: | ||
|
||
```bash | ||
docker run -d -p 7540:7540 --name todo-rest todo-rest | ||
``` | ||
|
||
### Переменные окружения | ||
Приложение использует следующие переменные окружения для конфигурации: | ||
|
||
- TODO_PORT — порт, на котором работает сервер (по умолчанию: 7540) | ||
- TODO_DBFILE — путь к файлу базы данных SQLite (по умолчанию: ./storage/scheduler.db) | ||
- TODO_PASSWORD — пароль для аутентификации | ||
Вы можете изменить их, добавив файл .env в корень проекта. | ||
|
||
## Пример использования API | ||
|
||
1. Создание задачи | ||
|
||
Запрос: | ||
|
||
```bash | ||
POST /api/task | ||
Content-Type: application/json | ||
|
||
{ | ||
"title": "Новая задача", | ||
"description": "Описание задачи", | ||
"date": "2024-12-01", | ||
"repeat": "d 5" | ||
} | ||
``` | ||
|
||
Ответ: | ||
|
||
```bash | ||
{ | ||
"id": "1", | ||
"title": "Новая задача", | ||
"description": "Описание задачи", | ||
"date": "2024-12-01", | ||
"repeat": "d 5" | ||
} | ||
``` | ||
|
||
2. Получение задачи | ||
|
||
Запрос: | ||
|
||
```bash | ||
GET /api/task?id=1 | ||
``` | ||
|
||
Ответ: | ||
|
||
```bash | ||
{ | ||
"id": "1", | ||
"title": "Новая задача", | ||
"description": "Описание задачи", | ||
"date": "2024-12-01", | ||
"repeat": "d 5" | ||
} | ||
``` | ||
|
||
## Тестирование | ||
|
||
Для запуска тестов выполните команду: | ||
|
||
```bash | ||
go test ./tests | ||
``` | ||
|
||
Вы также можете запустить тесты в Docker-контейнере: | ||
|
||
```bash | ||
docker run --rm todo-rest go test ./tests | ||
``` | ||
|
||
## Лицензия | ||
Данный проект лицензируется под лицензией MIT. См. файл LICENSE для получения подробной информации. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
|
||
"github.com/denisushakov/todo-rest/internal/config" | ||
"github.com/denisushakov/todo-rest/pkg/router" | ||
) | ||
|
||
func main() { | ||
config.MustLoad() | ||
|
||
port := ":" + config.Port | ||
|
||
router := router.SetupRouter() | ||
|
||
log.Printf("Server is running at %s", port) | ||
if err := http.ListenAndServe(port, router); err != nil { | ||
log.Fatalf("failed to start server: %v", err) | ||
} | ||
|
||
log.Fatalf("server stopped") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module github.com/denisushakov/todo-rest | ||
|
||
go 1.23.2 | ||
|
||
require ( | ||
github.com/go-chi/chi/v5 v5.1.0 | ||
github.com/golang-jwt/jwt/v5 v5.2.1 | ||
github.com/jmoiron/sqlx v1.4.0 | ||
github.com/mattn/go-sqlite3 v1.14.23 | ||
github.com/stretchr/testify v1.9.0 | ||
github.com/subosito/gotenv v1.6.0 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
golang.org/x/text v0.12.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= | ||
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= | ||
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= | ||
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y= | ||
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= | ||
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk= | ||
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= | ||
github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o= | ||
github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY= | ||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= | ||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= | ||
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= | ||
github.com/mattn/go-sqlite3 v1.14.23 h1:gbShiuAP1W5j9UOksQ06aiiqPMxYecovVGwmTxWtuw0= | ||
github.com/mattn/go-sqlite3 v1.14.23/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= | ||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= | ||
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= | ||
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= | ||
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc= | ||
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
Oops, something went wrong.