diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ac8c83c --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.code +.vscode +.idea + diff --git a/Dockerfile b/Dockerfile index dca083f..78a871b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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"] \ No newline at end of file +CMD ["./app"] diff --git a/README.md b/README.md index a86a8f2..3ca34a8 100644 --- a/README.md +++ b/README.md @@ -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`) - diff --git a/app b/app deleted file mode 100755 index 8241526..0000000 Binary files a/app and /dev/null differ diff --git a/cmd/server/main.go b/cmd/main.go similarity index 71% rename from cmd/server/main.go rename to cmd/main.go index 81c7e03..65ee879 100644 --- a/cmd/server/main.go +++ b/cmd/main.go @@ -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 } @@ -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) } } diff --git a/cmd/server/main b/cmd/server/main deleted file mode 100755 index d21e9fc..0000000 Binary files a/cmd/server/main and /dev/null differ diff --git a/docker-compose.yml b/docker-compose.yml index dfb1376..1cab5a2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -36,4 +36,4 @@ volumes: database_postgres: networks: fullstack: - driver: bridge \ No newline at end of file + driver: bridge diff --git a/cmd/server/transport/http/auth.go b/internal/server/transport/http/auth.go similarity index 100% rename from cmd/server/transport/http/auth.go rename to internal/server/transport/http/auth.go diff --git a/cmd/server/transport/http/handler.go b/internal/server/transport/http/handler.go similarity index 77% rename from cmd/server/transport/http/handler.go rename to internal/server/transport/http/handler.go index cacb79a..9485269 100644 --- a/cmd/server/transport/http/handler.go +++ b/internal/server/transport/http/handler.go @@ -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 { diff --git a/cmd/server/transport/http/message.go b/internal/server/transport/http/message.go similarity index 88% rename from cmd/server/transport/http/message.go rename to internal/server/transport/http/message.go index 3d58756..c75f7c8 100644 --- a/cmd/server/transport/http/message.go +++ b/internal/server/transport/http/message.go @@ -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") +} diff --git a/cmd/server/transport/http/middleware.go b/internal/server/transport/http/middleware.go similarity index 89% rename from cmd/server/transport/http/middleware.go rename to internal/server/transport/http/middleware.go index 27d36ce..0ffb9e8 100644 --- a/cmd/server/transport/http/middleware.go +++ b/internal/server/transport/http/middleware.go @@ -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) { @@ -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)) diff --git a/tests/integration/message/message_test.go b/tests/integration/message/message_test.go index 13db272..06cd6b3 100644 --- a/tests/integration/message/message_test.go +++ b/tests/integration/message/message_test.go @@ -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) {