Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
357c3d9
crate server with mock data
0oM4R Jul 23, 2022
9cab1c8
connect api with mysql using gorm
0oM4R Jul 24, 2022
eb6e969
adda swagger API documentation
0oM4R Jul 24, 2022
2ce0570
container connection error
0oM4R Jul 25, 2022
a6407b3
container connection error resolved
0oM4R Jul 25, 2022
eaa2760
create dependes on condetion on go app container
0oM4R Jul 25, 2022
9794975
add postman collection
0oM4R Jul 25, 2022
0e714c7
handle more error, use sqlit
0oM4R Jul 26, 2022
00c5637
docker compose
0oM4R Jul 26, 2022
69c85cf
Create docker.yaml
0oM4R Jul 26, 2022
f488728
Update docker.yaml
0oM4R Jul 26, 2022
35485f3
Create go.yml
0oM4R Jul 27, 2022
bbfea86
create go tests
0oM4R Jul 27, 2022
3016933
Merge branch 'omarTest' of https://github.com/codescalersinternships/…
0oM4R Jul 27, 2022
151f41e
some comment resolved
0oM4R Jul 29, 2022
6f2d5e9
more test added
0oM4R Jul 29, 2022
6441844
add info logger middleware
0oM4R Jul 29, 2022
2232247
integrate swagger Ui
0oM4R Jul 30, 2022
c035864
reorganize files
0oM4R Aug 4, 2022
747a7f1
set go action in backend
0oM4R Aug 4, 2022
7f1318e
Update go.yml
0oM4R Aug 4, 2022
090429e
handle null env variables
0oM4R Aug 4, 2022
f26c33f
Merge branch 'omarTest' of https://github.com/codescalersinternships/…
0oM4R Aug 4, 2022
fe44254
resolve some tests
0oM4R Aug 4, 2022
e37847f
Update go.yml
0oM4R Aug 4, 2022
5b9fd5b
Update docker.yaml
0oM4R Aug 4, 2022
11b4e51
Update docker.yaml
0oM4R Aug 4, 2022
626a46f
Update docker.yaml
0oM4R Aug 4, 2022
dde9c72
adding docker file for front end
0oM4R Aug 4, 2022
dc79e22
Merge branch 'omarTest' of https://github.com/codescalersinternships/…
0oM4R Aug 4, 2022
c3dae36
docker compose err
0oM4R Aug 5, 2022
3b8a62e
docker error resolved
0oM4R Aug 5, 2022
20815ed
add cypress integration
0oM4R Aug 5, 2022
61af4ab
Update docker.yaml
0oM4R Aug 5, 2022
1565c64
add frontend image
0oM4R Aug 5, 2022
e009ced
Create cypress.yaml
0oM4R Aug 5, 2022
b264fcd
Create cypress.yaml
0oM4R Aug 5, 2022
5a2a971
Update cypress.yaml
0oM4R Aug 5, 2022
1538982
Merge branch 'omarTest' into omarTest
0oM4R Aug 5, 2022
194da99
Merge pull request #10 from 0oM4R/omarTest
0oM4R Aug 5, 2022
a8e7cb9
multi stage image
0oM4R Oct 4, 2022
05b13a2
path solved
0oM4R Oct 4, 2022
4981094
path solved
0oM4R Oct 4, 2022
d147881
path error resolved
0oM4R Oct 5, 2022
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
19 changes: 19 additions & 0 deletions .github/workflows/cypress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Docker Compose
on: push
jobs:
test_cypress:
runs-on: ubuntu-latest
steps:

- uses: actions/checkout@v2
- name: Build the stack
run: docker-compose up -d

- name: run cypress tests
run: |
cd frontend
npm i
npx cypress run



45 changes: 45 additions & 0 deletions .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Docker

on:
push:
branches:
- omarTest

jobs:

Backend:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3


- name: Login to DockerHub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Build and Push Docker Image
uses: docker/build-push-action@v3
with:
context: ./backend
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/todo_1:sqlite
frontedn:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3


- name: Login to DockerHub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Build and Push Docker Image
uses: docker/build-push-action@v3
with:
context: ./frontend
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/todo_1:fe
34 changes: 34 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Go

on:
push:
branches:
- main
- omarTest
pull_request:
branches:
- main

jobs:

test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18

- name: Test
run: |
cd backend
export DB_FILE="./todo.db"
export Port=":5000"
go test -v ./...

- name: code coverage
run: |
cd backend
go test -cover
2 changes: 2 additions & 0 deletions backend/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Port=":5000"
DB_FILE="./todo.db"
19 changes: 19 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

FROM golang:latest as builder
WORKDIR /app
COPY go.mod ./
COPY go.sum ./
RUN go mod download
COPY . .
ENV DB_FILE="./todo.db"
ENV Port=":5000"
RUN go build -o main -a -ldflags "-linkmode external -extldflags '-static' -s -w" .

WORKDIR /app
FROM alpine:3.11.3
COPY --from=builder /app/main /app/

EXPOSE 5000

CMD ["./app/main"]

File renamed without changes.
Binary file added backend/Todo
Binary file not shown.
150 changes: 150 additions & 0 deletions backend/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package main

import (
"encoding/json"

"fmt"
"net/http"

"strconv"

"gorm.io/gorm"
)

func (a *App) home(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "http://localhost:8080")
w.Header().Set("Access-Control-Allow-Headers", "authentication, content-type")
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode("ToDo home")
w.WriteHeader(http.StatusOK)
}

func (a *App) getALlToDoHandler(w http.ResponseWriter, r *http.Request) {
//w.Header().Set("Access-Control-Allow-Origin", "http://localhost:8080")
w.Header().Set("Content-Type", "application/json")

res, err := a.db.GetALlToDo()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode("Error :" + err.Error())

return
}

w.WriteHeader(http.StatusOK)
w.Write(res)

}

func (a *App) getTodoByIdHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "http://localhost:8080")
w.Header().Set("Content-Type", "application/json")

id := r.URL.Query().Get("taskId")
res, err := a.db.GetTodoById(id)
if err == gorm.ErrRecordNotFound {
errResp := Response{Response: "Task not found"}
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(errResp)
} else if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(&err)
} else {
w.WriteHeader(http.StatusOK)
w.Write(res)
}
}

// add new task to todo database
func (a *App) newTaskHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "http://localhost:8080")
w.Header().Set("Access-Control-Allow-Methods", "*")
w.Header().Set("Content-Type", "application/json")
var task ToDo
//get body data
err := json.NewDecoder(r.Body).Decode(&task)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(err.Error())
return
}

resp, respErr := a.db.AddTodo(&task)
if respErr != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(respErr)
} else {
w.WriteHeader(http.StatusOK)
w.Write(resp)
}

}

func (a *App) updateTaskHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*") // 允许访问所有域,可以换成具体url,注意仅具体url才能带cookie信息
w.Header().Add("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token") //header的类型
w.Header().Add("Access-Control-Allow-Credentials", "true") //设置为true,允许ajax异步请求带cookie信息
w.Header().Add("Access-Control-Allow-Methods", "PUT, GET, OPTIONS, POST, DELETE") //允许请求方法
w.Header().Set("content-type", "application/json;charset=UTF-8")

id := r.URL.Query().Get("taskId")
var tmp ToDo
tmp.ID, _ = strconv.Atoi(id)
err := json.NewDecoder(r.Body).Decode(&tmp)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(err.Error())
return
}
if tmp.Task == "" {
errResp := Response{Response: "Make sure to add task"}
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(errResp)
return
}
res, err := a.db.UpdateTodo(&tmp, id)
if err == gorm.ErrRecordNotFound {
errResp := Response{Response: "Task not found"}
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(errResp)
} else if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(&err)
} else {
w.WriteHeader(http.StatusOK)
w.Write(res)
}

}

func (a *App) DeleteTaskHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
id := r.URL.Query().Get("taskId")
res, err := a.db.DeleteTask(id)

if err == gorm.ErrRecordNotFound {
errResp := Response{Response: "Task not found"}
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(errResp)
} else if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(err.Error())
return
} else {
w.WriteHeader(http.StatusNoContent)
w.Write(res)
}
}

func NewApp(DB_FILE, Port string, router http.Handler) (App, error) {
a := App{}
a.db = &database{}
a.server = &http.Server{}
err := a.db.connectDatabase(DB_FILE)
fmt.Printf("start sever on port: %s\n", Port)

a.db.DB.AutoMigrate(&ToDo{})
a.server.Addr = Port
a.server.Handler = router
return a, err
}
Loading