resolving conflicts
This commit is contained in:
@@ -1,17 +1,16 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang-jwt/jwt/v5"
|
|
||||||
core "fr.latosa-escrima/api/core"
|
core "fr.latosa-escrima/api/core"
|
||||||
|
"github.com/golang-jwt/jwt/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
var MySigningKey = []byte("COUCOU")
|
var MySigningKey = []byte("COUCOU")
|
||||||
@@ -28,21 +27,37 @@ type Claims struct {
|
|||||||
|
|
||||||
func HandleLogin(w http.ResponseWriter, r *http.Request) {
|
func HandleLogin(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
log.Fatal("Not post method")
|
core.JSONError{
|
||||||
|
Status: core.Error,
|
||||||
|
Message: "Method is not allowed",
|
||||||
|
}.Respond(w, http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.Body == nil {
|
if r.Body == nil {
|
||||||
log.Fatal("No body")
|
core.JSONError{
|
||||||
|
Status: core.Error,
|
||||||
|
Message: "No body has been provided.",
|
||||||
|
}.Respond(w, http.StatusNoContent)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
core.JSONError{
|
||||||
|
Status: core.Error,
|
||||||
|
Message: err.Error(),
|
||||||
|
}.Respond(w, http.StatusNoContent)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
var login LoginInformation
|
var login LoginInformation
|
||||||
err = json.Unmarshal(body, &login)
|
err = json.Unmarshal(body, &login)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
core.JSONError{
|
||||||
|
Status: core.Error,
|
||||||
|
Message: err.Error(),
|
||||||
|
}.Respond(w, http.StatusNoContent)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var user core.User
|
var user core.User
|
||||||
@@ -53,7 +68,11 @@ func HandleLogin(w http.ResponseWriter, r *http.Request) {
|
|||||||
Scan(context.Background())
|
Scan(context.Background())
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
core.JSONError{
|
||||||
|
Status: core.Error,
|
||||||
|
Message: err.Error(),
|
||||||
|
}.Respond(w, http.StatusNoContent)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
claims := Claims{
|
claims := Claims{
|
||||||
@@ -69,10 +88,18 @@ func HandleLogin(w http.ResponseWriter, r *http.Request) {
|
|||||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||||
signed, err := token.SignedString([]byte("hello"))
|
signed, err := token.SignedString([]byte("hello"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
core.JSONError{
|
||||||
|
Status: core.Error,
|
||||||
|
Message: err.Error(),
|
||||||
|
}.Respond(w, http.StatusNoContent)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(signed)
|
core.JSONSuccess{
|
||||||
|
Status: core.Success,
|
||||||
|
Message: "JWT Created",
|
||||||
|
Data: map[string]string{"jwt": signed},
|
||||||
|
}.Respond(w, http.StatusCreated)
|
||||||
}
|
}
|
||||||
|
|
||||||
func AuthJWT(next http.Handler) http.Handler {
|
func AuthJWT(next http.Handler) http.Handler {
|
||||||
@@ -81,8 +108,8 @@ func AuthJWT(next http.Handler) http.Handler {
|
|||||||
fmt.Println("Coucou")
|
fmt.Println("Coucou")
|
||||||
authHeader := r.Header.Get("Authorization")
|
authHeader := r.Header.Get("Authorization")
|
||||||
if authHeader == "" {
|
if authHeader == "" {
|
||||||
JSONError{
|
core.JSONError{
|
||||||
Status: Error,
|
Status: core.Error,
|
||||||
Message: "Missing Authorization header",
|
Message: "Missing Authorization header",
|
||||||
}.Respond(w, http.StatusUnauthorized)
|
}.Respond(w, http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
@@ -91,8 +118,8 @@ func AuthJWT(next http.Handler) http.Handler {
|
|||||||
// Bearer token is expected, so split the header into "Bearer <token>"
|
// Bearer token is expected, so split the header into "Bearer <token>"
|
||||||
tokenString := strings.TrimPrefix(authHeader, "Bearer ")
|
tokenString := strings.TrimPrefix(authHeader, "Bearer ")
|
||||||
if tokenString == authHeader {
|
if tokenString == authHeader {
|
||||||
JSONError{
|
core.JSONError{
|
||||||
Status: Error,
|
Status: core.Error,
|
||||||
Message: "Invalid Authorization header format",
|
Message: "Invalid Authorization header format",
|
||||||
}.Respond(w, http.StatusUnauthorized)
|
}.Respond(w, http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
@@ -108,8 +135,8 @@ func AuthJWT(next http.Handler) http.Handler {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if err != nil || !token.Valid {
|
if err != nil || !token.Valid {
|
||||||
JSONError{
|
core.JSONError{
|
||||||
Status: Error,
|
Status: core.Error,
|
||||||
Message: "Invalid Token",
|
Message: "Invalid Token",
|
||||||
}.Respond(w, http.StatusUnauthorized)
|
}.Respond(w, http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -3,8 +3,8 @@ package core
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"time"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/uptrace/bun"
|
"github.com/uptrace/bun"
|
||||||
|
|||||||
@@ -2,15 +2,15 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/joho/godotenv"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"github.com/joho/godotenv"
|
|
||||||
|
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
|
|
||||||
api "fr.latosa-escrima/api"
|
api "fr.latosa-escrima/api"
|
||||||
schemas "fr.latosa-escrima/api/core"
|
"fr.latosa-escrima/api/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
func handler(w http.ResponseWriter, r *http.Request) {
|
func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -28,29 +28,25 @@ func main() {
|
|||||||
port = os.Getenv("BACKEND_PORT")
|
port = os.Getenv("BACKEND_PORT")
|
||||||
}
|
}
|
||||||
|
|
||||||
dsn := schemas.DSN{
|
dsn := core.DSN{
|
||||||
Hostname: "localhost",
|
Hostname: "localhost",
|
||||||
Port: os.Getenv("POSTGRES_PORT"),
|
Port: os.Getenv("POSTGRES_PORT"),
|
||||||
DBName: os.Getenv("POSTGRES_DB"),
|
DBName: os.Getenv("POSTGRES_DB"),
|
||||||
User: os.Getenv("POSTGRES_USER"),
|
User: os.Getenv("POSTGRES_USER"),
|
||||||
Password: os.Getenv("POSTGRES_PASSWORD"),
|
Password: os.Getenv("POSTGRES_PASSWORD"),
|
||||||
}
|
}
|
||||||
schemas.DB, err = schemas.InitDatabase(dsn)
|
core.DB, err = core.InitDatabase(dsn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
HandleRoutes(mux, map[string]Handler{
|
core.HandleRoutes(mux, map[string]core.Handler{
|
||||||
"/": { handler, nil},
|
"/": {Handler: handler, Middleware: nil},
|
||||||
"/users/login": { api.HandleLogin, nil},
|
"/users/login": {Handler: api.HandleLogin, Middleware: nil},
|
||||||
"/users/new": { api.HandleCreateUser, api.AuthJWT},
|
"/blogs/new": {Handler: api.HandleCreateBlog, Middleware: nil},
|
||||||
// "/blogs": { api.HandleGetBlogs, nil},
|
"/blogs/{uuid}": {Handler: api.HandleGetBlog, Middleware: nil},
|
||||||
"/blogs/new": { api.HandleCreateBlog, nil},
|
|
||||||
"/blogs/{uuid}": { api.HandleGetBlog, nil},
|
|
||||||
// "/events": { api.HandleGetEvents, api.AuthJWT },
|
|
||||||
// "/events/new": { api.HandleCreateEvents, api.AuthJWT }
|
|
||||||
})
|
})
|
||||||
|
|
||||||
fmt.Printf("Serving on port %s\n", port)
|
fmt.Printf("Serving on port %s\n", port)
|
||||||
|
|||||||
Reference in New Issue
Block a user