From 08f8693e8d5b40ee6dc30ac2b5467f2cf04fc7d3 Mon Sep 17 00:00:00 2001 From: conzer Date: Sat, 7 Dec 2024 17:45:46 -0500 Subject: [PATCH] create sevrer --- db/db.go | 19 ++++++++++++ go.mod | 5 ++++ go.sum | 2 ++ server.go | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+) create mode 100644 db/db.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 server.go diff --git a/db/db.go b/db/db.go new file mode 100644 index 0000000..998f0fd --- /dev/null +++ b/db/db.go @@ -0,0 +1,19 @@ +package db + +import ( + "database/sql" + _ "github.com/lib/pq" + "log" +) + +// Replace the "connStr" string with your DB connection username and password. + +func ConnectDB() (*sql.DB, error) { + connStr := "user=username password=password123 dbname=cooldatabase sslmode=disable" + db, err := sql.Open("postgres", connStr) + if err != nil { + log.Fatalf("Failed to connect to the database: %v", err) + return nil, err + } + return db, nil +} \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..47e6f7e --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module goblog-backend + +go 1.23.3 + +require github.com/lib/pq v1.10.9 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..aeddeae --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= +github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= diff --git a/server.go b/server.go new file mode 100644 index 0000000..f9a902e --- /dev/null +++ b/server.go @@ -0,0 +1,86 @@ +package main + +import ( + "encoding/json" + "fmt" + "net/http" + "log" + "database/sql" + "os" + "net" + "goblog-backend/db" +) + +func main() { + db, err := db.ConnectDB() + if err != nil { + log.Fatal(err) + } + defer db.Close() + + socketPath := "/tmp/goblog.sock" + if err := os.RemoveAll(socketPath); err != nil { + log.Fatalf("Failed to delete socket: %v", err) + } + + listener, err := net.Listen("unix", socketPath) + if err != nil { + log.Fatalf("Failed to create socket: %v", err) + } + defer listener.Close() + + http.HandleFunc("/posts", func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodGet { + posts, err := GetPosts(db) + if err != nil { + http.Error(w, "Failed to fetch posts", http.StatusInternalServerError) + return + } + json.NewEncoder(w).Encode(posts) + } else if r.Method == http.MethodPost { + var post Post + err := json.NewDecoder(r.Body).Decode(&post) + if err != nil { + http.Error(w, "Failed to create post", http.StatusInternalServerError) + return + } + w.WriteHeader(http.StatusCreated) + } + }) + // replace with the open port! + fmt.Println("[INFO] Welcome to goblog!") + log.Println ("[INFO] Server starting on socket:", socketPath) + log.Fatal(http.Serve(listener, nil)) +} + +func GetPosts(db *sql.DB) ([]Post, error) { + rows, err := db.Query("SELECT id, title, content, created_at FROM posts") + if err != nil { + return nil, err + } + defer rows.Close() + + var posts []Post + for rows.Next() { + var post Post + if err := rows.Scan(&post.ID, &post.Title, &post.Content, &post.CreatedAt); err != nil { + return nil, err + } + posts = append(posts, post) + } + return posts, nil +} + +func CreatePost(db *sql.DB, post Post) error { + _, err := db.Exec("INSERT INTO posts (title, content, created_at) VALUES ($1, $2, NOW())", post.Title, post.Content) + return err +} + +type Post struct { + ID int `json:"id"` + Title string `json:"title"` + Content string `json:"title"` + CreatedAt string `json:"created_at"` +} + +