67 lines
1.5 KiB
Go
67 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"github.com/joho/godotenv"
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
api "fr.latosa-escrima/api"
|
|
schemas "fr.latosa-escrima/api/core"
|
|
)
|
|
|
|
func handler(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Fprintf(w, "<html><body><h1>Hello, World!</h1></body></html>")
|
|
}
|
|
|
|
type ErrorResponse struct {
|
|
ErrorCode string `json:"errorCode"`
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
func main() {
|
|
err := godotenv.Load()
|
|
if err != nil {
|
|
log.Fatalf("Error loading .env file: %v", err)
|
|
}
|
|
environ := os.Getenv("ENVIRONMENT")
|
|
port := os.Getenv("BACKEND_DOCKER_PORT")
|
|
if environ == "DEV" {
|
|
port = os.Getenv("BACKEND_PORT")
|
|
}
|
|
|
|
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"),
|
|
}
|
|
schemas.DB, err = schemas.InitDatabase(dsn)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
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), mux)
|
|
if err != nil {
|
|
fmt.Printf("Error starting server: %s\n", err)
|
|
}
|
|
}
|