Merge remote-tracking branch 'origin/dev/guerby' into dev/cedric
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user