Merge remote-tracking branch 'origin/dev/guerby' into dev/cedric
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
core "fr.latosa-escrima/api/core"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
@@ -26,16 +27,16 @@ type Claims struct {
|
||||
|
||||
func HandleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
JSONError{
|
||||
Status: Error,
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: "Method is not allowed",
|
||||
}.Respond(w, http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
if r.Body == nil {
|
||||
JSONError{
|
||||
Status: Error,
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: "No body has been provided.",
|
||||
}.Respond(w, http.StatusNoContent)
|
||||
return
|
||||
@@ -43,8 +44,8 @@ func HandleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
JSONError{
|
||||
Status: Error,
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusNoContent)
|
||||
return
|
||||
@@ -52,23 +53,23 @@ func HandleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
var login LoginInformation
|
||||
err = json.Unmarshal(body, &login)
|
||||
if err != nil {
|
||||
JSONError{
|
||||
Status: Error,
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
var user User
|
||||
err = DB.NewSelect().
|
||||
var user core.User
|
||||
err = core.DB.NewSelect().
|
||||
Model(&user).
|
||||
Where("email = ? AND password = ?", login.Email, login.Password).
|
||||
Limit(1).
|
||||
Scan(context.Background())
|
||||
|
||||
if err != nil {
|
||||
JSONError{
|
||||
Status: Error,
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusNoContent)
|
||||
return
|
||||
@@ -87,15 +88,15 @@ func HandleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
signed, err := token.SignedString([]byte("hello"))
|
||||
if err != nil {
|
||||
JSONError{
|
||||
Status: Error,
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
JSONSuccess{
|
||||
Status: Success,
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "JWT Created",
|
||||
Data: map[string]string{"jwt": signed},
|
||||
}.Respond(w, http.StatusCreated)
|
||||
@@ -107,8 +108,8 @@ func AuthJWT(next http.Handler) http.Handler {
|
||||
fmt.Println("Coucou")
|
||||
authHeader := r.Header.Get("Authorization")
|
||||
if authHeader == "" {
|
||||
JSONError{
|
||||
Status: Error,
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: "Missing Authorization header",
|
||||
}.Respond(w, http.StatusUnauthorized)
|
||||
return
|
||||
@@ -117,8 +118,8 @@ func AuthJWT(next http.Handler) http.Handler {
|
||||
// Bearer token is expected, so split the header into "Bearer <token>"
|
||||
tokenString := strings.TrimPrefix(authHeader, "Bearer ")
|
||||
if tokenString == authHeader {
|
||||
JSONError{
|
||||
Status: Error,
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: "Invalid Authorization header format",
|
||||
}.Respond(w, http.StatusUnauthorized)
|
||||
return
|
||||
@@ -134,8 +135,8 @@ func AuthJWT(next http.Handler) http.Handler {
|
||||
})
|
||||
|
||||
if err != nil || !token.Valid {
|
||||
JSONError{
|
||||
Status: Error,
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: "Invalid Token",
|
||||
}.Respond(w, http.StatusUnauthorized)
|
||||
return
|
||||
58
backend/api/blogs.go
Normal file
58
backend/api/blogs.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
core "fr.latosa-escrima/api/core"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
func HandleGetBlog(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
fmt.Println("salut")
|
||||
emptyObject := make(map[string]interface{})
|
||||
|
||||
emptyJSON, json_err := json.Marshal(emptyObject)
|
||||
if json_err != nil {
|
||||
fmt.Println("Couldn't create the json object")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(emptyJSON))
|
||||
return
|
||||
}
|
||||
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "Wrong method", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
blog := &core.Blog{
|
||||
BaseModel: bun.BaseModel{},
|
||||
BlogID: [16]byte{},
|
||||
Slug: "",
|
||||
Content: "",
|
||||
Label: "",
|
||||
AuthorID: [16]byte{},
|
||||
Published: time.Time{},
|
||||
Summary: "",
|
||||
Image: "",
|
||||
Href: "",
|
||||
Author: &core.User{},
|
||||
}
|
||||
|
||||
blog_uuid := r.PathValue("uuid")
|
||||
fmt.Println(blog_uuid)
|
||||
ctx := context.Background()
|
||||
err_db := core.DB.NewSelect().Model(blog).Where("uuid = ?", blog_uuid).Scan(ctx)
|
||||
if err_db != nil {
|
||||
log.Fatal(err_db)
|
||||
http.Error(w, "Can't use select", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
w.Write([]byte(`{message: "Successfuly responded to request}`))
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package core
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
@@ -1,8 +1,9 @@
|
||||
package main
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -11,6 +12,20 @@ import (
|
||||
"github.com/uptrace/bun/driver/pgdriver"
|
||||
)
|
||||
|
||||
var DB *bun.DB
|
||||
|
||||
type DSN struct {
|
||||
Hostname string
|
||||
Port string
|
||||
DBName string
|
||||
User string
|
||||
Password string
|
||||
}
|
||||
|
||||
func (dsn *DSN) ToString() string {
|
||||
return fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", dsn.User, dsn.Password, dsn.Hostname, dsn.Port, dsn.DBName)
|
||||
}
|
||||
|
||||
type Role string
|
||||
|
||||
const (
|
||||
52
backend/api/new_blog.go
Normal file
52
backend/api/new_blog.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
core "fr.latosa-escrima/api/core"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
func HandleCreateBlog(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
emptyObject := make(map[string]interface{})
|
||||
|
||||
emptyJSON, json_err := json.Marshal(emptyObject)
|
||||
if json_err != nil {
|
||||
fmt.Println("Couldn't create the json object")
|
||||
w.Write(emptyJSON)
|
||||
}
|
||||
|
||||
if r.Method != http.MethodPost {
|
||||
}
|
||||
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
}
|
||||
|
||||
user := &core.Blog{
|
||||
BaseModel: bun.BaseModel{},
|
||||
BlogID: [16]byte{},
|
||||
Slug: "",
|
||||
Content: "",
|
||||
Label: "",
|
||||
AuthorID: [16]byte{},
|
||||
Published: time.Time{},
|
||||
Summary: "",
|
||||
Image: "",
|
||||
Href: "",
|
||||
Author: &core.User{},
|
||||
}
|
||||
|
||||
_, err_db := core.DB.NewInsert().Model(user).Exec(context.Background())
|
||||
if err_db != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println("User inserted successfully")
|
||||
}
|
||||
35
backend/api/new_user.go
Normal file
35
backend/api/new_user.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
core "fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
func HandleCreateUser(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
if r.Method != http.MethodPost {
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
w.Write([]byte(`{"message": "Resource created successfully"}`))
|
||||
}
|
||||
|
||||
user := &core.User{
|
||||
FirstName: "John",
|
||||
LastName: "Doe",
|
||||
Email: "john.doe@example.com",
|
||||
Phone: "1234567890",
|
||||
Password: "1234",
|
||||
}
|
||||
|
||||
_, err := core.DB.NewInsert().Model(user).Exec(context.Background())
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println("User inserted successfully")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"message": "Inserted the user"}`))
|
||||
}
|
||||
@@ -2,58 +2,21 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/joho/godotenv"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
|
||||
"context"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/uptrace/bun"
|
||||
|
||||
api "fr.latosa-escrima/api"
|
||||
"fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
func handler(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, "<html><body><h1>Hello, World!</h1></body></html>")
|
||||
}
|
||||
|
||||
var DB *bun.DB
|
||||
|
||||
type DSN struct {
|
||||
Hostname string
|
||||
Port string
|
||||
DBName string
|
||||
User string
|
||||
Password string
|
||||
}
|
||||
|
||||
func (dsn *DSN) ToString() string {
|
||||
return fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", dsn.User, dsn.Password, dsn.Hostname, dsn.Port, dsn.DBName)
|
||||
}
|
||||
|
||||
func handlerCreateUser(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
if r.Method != http.MethodPost {
|
||||
// return an empty json
|
||||
}
|
||||
|
||||
user := &User{
|
||||
FirstName: "John",
|
||||
LastName: "Doe",
|
||||
Email: "john.doe@example.com",
|
||||
Phone: "1234567890",
|
||||
Password: "1234",
|
||||
}
|
||||
|
||||
_, err := DB.NewInsert().Model(user).Exec(context.Background())
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println("User inserted successfully")
|
||||
}
|
||||
|
||||
func main() {
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
@@ -65,24 +28,25 @@ func main() {
|
||||
port = os.Getenv("BACKEND_PORT")
|
||||
}
|
||||
|
||||
dsn := DSN{
|
||||
dsn := core.DSN{
|
||||
Hostname: "localhost",
|
||||
Port: os.Getenv("POSTGRES_PORT"),
|
||||
DBName: os.Getenv("POSTGRES_DB"),
|
||||
User: os.Getenv("POSTGRES_USER"),
|
||||
Password: os.Getenv("POSTGRES_PASSWORD"),
|
||||
}
|
||||
DB, err = InitDatabase(dsn)
|
||||
core.DB, err = core.InitDatabase(dsn)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
|
||||
HandleRoutes(mux, map[string]Handler{
|
||||
"/": {handler, nil},
|
||||
"/users/login": {HandleLogin, nil},
|
||||
"/users/new": {handlerCreateUser, AuthJWT},
|
||||
core.HandleRoutes(mux, map[string]core.Handler{
|
||||
"/": {Handler: handler, Middleware: nil},
|
||||
"/users/login": {Handler: api.HandleLogin, Middleware: nil},
|
||||
"/blogs/new": {Handler: api.HandleCreateBlog, Middleware: nil},
|
||||
"/blogs/{uuid}": {Handler: api.HandleGetBlog, Middleware: nil},
|
||||
})
|
||||
|
||||
fmt.Printf("Serving on port %s\n", port)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"use server";
|
||||
|
||||
export default async function About() {
|
||||
const res = await fetch("http://localhost:8000/api");
|
||||
const res = await fetch("api");
|
||||
console.log(res);
|
||||
return (
|
||||
<div>
|
||||
|
||||
@@ -24,12 +24,12 @@ export default function RootLayout({
|
||||
children: React.ReactNode;
|
||||
}>) {
|
||||
return (
|
||||
<SWRConfig
|
||||
value={{
|
||||
fetcher: (url: string) => fetch(url).then((res) => res.json()),
|
||||
revalidateOnFocus: false,
|
||||
}}
|
||||
>
|
||||
//<SWRConfig
|
||||
// value={{
|
||||
// fetcher: (url: string) => fetch(url).then((res) => res.json()),
|
||||
// revalidateOnFocus: false,
|
||||
// }}
|
||||
//>
|
||||
<html lang="fr">
|
||||
<body
|
||||
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
|
||||
@@ -37,6 +37,6 @@ export default function RootLayout({
|
||||
{children}
|
||||
</body>
|
||||
</html>
|
||||
</SWRConfig>
|
||||
//</SWRConfig>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -11,8 +11,8 @@ const nextConfig: NextConfig = {
|
||||
}
|
||||
return [
|
||||
{
|
||||
source: "/api",
|
||||
destination: `http://localhost:${process.env.BACKEND_PORT}`,
|
||||
source: "/api/:path",
|
||||
destination: `http://localhost:${process.env.BACKEND_PORT}/:path`,
|
||||
permanent: false,
|
||||
},
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user