Backend file structure modifications, blogs routes ajustement
This commit is contained in:
162
backend/main.go
162
backend/main.go
@@ -5,15 +5,12 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"github.com/joho/godotenv"
|
||||
|
||||
"context"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/uptrace/bun"
|
||||
|
||||
api "fr.latosa-escrima/api"
|
||||
schemas "fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
func handler(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -25,138 +22,6 @@ type ErrorResponse struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
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 {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
w.Write([]byte(`{"message": "Resource created successfully"}`))
|
||||
}
|
||||
|
||||
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 handlerCreateBlog(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 {
|
||||
return string(emptyJSON)
|
||||
}
|
||||
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
return string(emptyJSON)
|
||||
}
|
||||
|
||||
user := &Blog{
|
||||
BaseModel: bun.BaseModel{},
|
||||
BlogID: [16]byte{},
|
||||
Slug: "",
|
||||
Content: "",
|
||||
Label: "",
|
||||
AuthorID: [16]byte{},
|
||||
Published: time.Time{},
|
||||
Summary: "",
|
||||
Image: "",
|
||||
Href: "",
|
||||
Author: &User{},
|
||||
}
|
||||
|
||||
_, err_db := DB.NewInsert().Model(user).Exec(context.Background())
|
||||
if err_db != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println("User inserted successfully")
|
||||
return string(emptyJSON)
|
||||
}
|
||||
|
||||
func handlerGetBlog(w http.ResponseWriter, r *http.Request) (string) {
|
||||
log.Println("ok")
|
||||
emptyObject := make(map[string]interface{})
|
||||
|
||||
emptyJSON, json_err := json.Marshal(emptyObject)
|
||||
if json_err != nil {
|
||||
fmt.Println("Couldn't create the json object")
|
||||
return string(emptyJSON)
|
||||
}
|
||||
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "Wrong method", http.StatusMethodNotAllowed)
|
||||
return string(emptyJSON)
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(r.URL.Path, "/blog/") {
|
||||
http.Redirect(w, r, "/blog", http.StatusFound)
|
||||
return string(emptyJSON)
|
||||
}
|
||||
|
||||
blog_uuid := strings.TrimPrefix(r.URL.Path, "/blog/")
|
||||
if blog_uuid == "" {
|
||||
http.Error(w, "Slug is required", http.StatusBadRequest)
|
||||
return string(emptyJSON)
|
||||
}
|
||||
|
||||
blog := &Blog{
|
||||
BaseModel: bun.BaseModel{},
|
||||
BlogID: [16]byte{},
|
||||
Slug: "",
|
||||
Content: "",
|
||||
Label: "",
|
||||
AuthorID: [16]byte{},
|
||||
Published: time.Time{},
|
||||
Summary: "",
|
||||
Image: "",
|
||||
Href: "",
|
||||
Author: &User{},
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
err_db := 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 string(emptyJSON)
|
||||
}
|
||||
|
||||
fmt.Println("Blog inserted successfully")
|
||||
return string(emptyJSON)
|
||||
}
|
||||
|
||||
func main() {
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
@@ -168,26 +33,33 @@ func main() {
|
||||
port = os.Getenv("BACKEND_PORT")
|
||||
}
|
||||
|
||||
dsn := DSN{
|
||||
dsn := schemas.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)
|
||||
schemas.DB, err = schemas.InitDatabase(dsn)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
http.HandleFunc("/", handler)
|
||||
mux := http.NewServeMux()
|
||||
|
||||
http.HandleFunc("/user/new", handlerCreateUser)
|
||||
http.HandleFunc("/users/login", HandleLogin)
|
||||
http.HandleFunc("/blog/", handlerGetBlog)
|
||||
HandleRoutes(mux, map[string]Handler{
|
||||
"/": { handler, nil},
|
||||
"/users/login": { api.HandleLogin, nil},
|
||||
// "/users/new": { api.HandleCreateUser, api.AuthJWT},
|
||||
// "/blogs": { api.HandleGetBlogs, 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)
|
||||
err = http.ListenAndServe(fmt.Sprintf(":%s", port), nil)
|
||||
err = http.ListenAndServe(fmt.Sprintf(":%s", port), mux)
|
||||
if err != nil {
|
||||
fmt.Printf("Error starting server: %s\n", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user