create sevrer
This commit is contained in:
parent
088e83d6e6
commit
08f8693e8d
4 changed files with 112 additions and 0 deletions
19
db/db.go
Normal file
19
db/db.go
Normal file
|
@ -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
|
||||
}
|
5
go.mod
Normal file
5
go.mod
Normal file
|
@ -0,0 +1,5 @@
|
|||
module goblog-backend
|
||||
|
||||
go 1.23.3
|
||||
|
||||
require github.com/lib/pq v1.10.9 // indirect
|
2
go.sum
Normal file
2
go.sum
Normal file
|
@ -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=
|
86
server.go
Normal file
86
server.go
Normal file
|
@ -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"`
|
||||
}
|
||||
|
||||
|
Loading…
Reference in a new issue