Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: handler level code refactor + a few import formating #5

Open
wants to merge 4 commits into
base: chore/binaries-removed+gitignore
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.code
.vscode
.idea

2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ RUN CGO_ENABLED=0 GOOS=linux go build -o app cmd/server/main.go

FROM alpine:latest AS production
COPY --from=builder /app .
CMD ["./app"]
CMD ["./app"]
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,3 @@ Steps to authenticate:
- Go to the `docker-compose.yml` file and change or copy the value that is set into the `TOKEN_SECRET` env variable
- Go to http://jwtbuilder.jamiekurtz.com/ scroll down to the bottom put the key you set into the `Key` input and click on `Create Signed JWT`
- Copy the JWT token and use it to authenticate with the Authorization header (example `Authorization: Bearer TOKEN`)

Binary file removed app
Binary file not shown.
11 changes: 5 additions & 6 deletions cmd/server/main.go → cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ package main

import (
"fmt"
transportHttp "golang/golang-study/cmd/server/transport/http"

"golang/golang-study/internal/conversation"
"golang/golang-study/internal/database"
transportHttp "golang/golang-study/internal/server/transport/http"
)

func Run() error {
fmt.Println("Starting API")
db, err := database.NewDatabase()

if err != nil {
fmt.Println("Failed to connect")
return err
}

if err := db.MigrateDB(); err != nil {
fmt.Errorf("ERROR RUNNING UP %w", err)
if err = db.MigrateDB(); err != nil {
fmt.Printf("ERROR MIGRATING UP THE DATABASE %v\n", err)
return err
}

Expand All @@ -33,7 +33,6 @@ func Run() error {

func main() {
if err := Run(); err != nil {
fmt.Println("ERROR running the API")
fmt.Println(err)
fmt.Printf("ERROR running the API %v\n", err)
}
}
Binary file removed cmd/server/main
Binary file not shown.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ volumes:
database_postgres:
networks:
fullstack:
driver: bridge
driver: bridge
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ func (h *Handler) mapRoutes() {
w.WriteHeader(http.StatusOK)
return
}).Methods("GET")
h.Router.HandleFunc("/api/v1/message", JWtAuth(h.PostMessage)).Methods("POST")
h.Router.HandleFunc("/api/v1/message/{id}", h.GetMessage).Methods("GET")
h.Router.HandleFunc("/api/v1/message/{id}", JWtAuth(h.DeleteMessage)).Methods("DELETE")
h.Router.HandleFunc("/api/v1/message/{id}", JWtAuth(h.UpdateMessage)).Methods("PUT")
h.messageRoutes()
}

func (h *Handler) Serve() error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,10 @@ func (h *Handler) GetMessage(w http.ResponseWriter, r *http.Request) {
return
}
}

func (h *Handler) messageRoutes() {
h.Router.HandleFunc("/api/v1/message", JWtAuth(h.PostMessage)).Methods("POST")
h.Router.HandleFunc("/api/v1/message/{id}", h.GetMessage).Methods("GET")
h.Router.HandleFunc("/api/v1/message/{id}", JWtAuth(h.DeleteMessage)).Methods("DELETE")
h.Router.HandleFunc("/api/v1/message/{id}", JWtAuth(h.UpdateMessage)).Methods("PUT")
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/sirupsen/logrus"
)

const RequestTimetouSeconds = 15
const RequestTimeoutSeconds = 15

func JSONMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -33,7 +33,7 @@ func LogRequest(next http.Handler) http.Handler {

func TimeoutMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), RequestTimetouSeconds*time.Second)
ctx, cancel := context.WithTimeout(r.Context(), RequestTimeoutSeconds*time.Second)
defer cancel()

next.ServeHTTP(w, r.WithContext(ctx))
Expand Down
5 changes: 3 additions & 2 deletions tests/integration/message/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package tests

import (
"context"
"golang/golang-study/internal/conversation"
"golang/golang-study/internal/database"
"testing"

"github.com/stretchr/testify/assert"

"golang/golang-study/internal/conversation"
"golang/golang-study/internal/database"
)

func TestMessageDatabase(t *testing.T) {
Expand Down