initial commit

This commit is contained in:
Odweta
2026-03-19 09:23:13 +01:00
commit 12def174a4
7 changed files with 184 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
package main
import (
"context"
"fmt"
"os"
"github.com/jackc/pgx/v5"
)
var db *pgx.Conn
func connectDB() {
var err error
connStr := os.Getenv("DB_URL")
if connStr == "" {
panic("DB_URL not set")
}
db, err = pgx.Connect(context.Background(), connStr)
if err != nil {
panic(err)
}
fmt.Println("Connected to PostgreSQL")
}
+11
View File
@@ -0,0 +1,11 @@
module backend
go 1.26.1
require (
github.com/go-chi/chi/v5 v5.2.5 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgx/v5 v5.8.0 // indirect
golang.org/x/text v0.29.0 // indirect
)
+17
View File
@@ -0,0 +1,17 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-chi/chi/v5 v5.2.5 h1:Eg4myHZBjyvJmAFjFvWgrqDTXFyOzjj7YIm3L3mu6Ug=
github.com/go-chi/chi/v5 v5.2.5/go.mod h1:X7Gx4mteadT3eDOMTsXzmI4/rwUpOwBHLpAfupzFJP0=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.8.0 h1:TYPDoleBBme0xGSAX3/+NujXXtpZn9HBONkQC7IEZSo=
github.com/jackc/pgx/v5 v5.8.0/go.mod h1:QVeDInX2m9VyzvNeiCJVjCkNFqzsNb43204HshNSZKw=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/text v0.29.0 h1:1neNs90w9YzJ9BocxfsQNHKuAT4pkghyXc4nhZ6sJvk=
golang.org/x/text v0.29.0/go.mod h1:7MhJOA9CD2qZyOKYazxdYMF85OwPdEr9jTtBpO7ydH4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+55
View File
@@ -0,0 +1,55 @@
// TODO: READ AND UNDERSTAND + ALTER
package main
import (
"context"
"encoding/json"
"net/http"
)
// GET /users
func getUsers(w http.ResponseWriter, r *http.Request) {
rows, err := db.Query(context.Background(), "SELECT id, name FROM users")
if err != nil {
http.Error(w, err.Error(), 500)
return
}
defer rows.Close()
var users []User
for rows.Next() {
var u User
rows.Scan(&u.ID, &u.Name)
users = append(users, u)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(users)
}
// POST /users
func createUser(w http.ResponseWriter, r *http.Request) {
var u User
err := json.NewDecoder(r.Body).Decode(&u)
if err != nil {
http.Error(w, "Invalid JSON", 400)
return
}
err = db.QueryRow(
context.Background(),
"INSERT INTO users(name) VALUES($1) RETURNING id",
u.Name,
).Scan(&u.ID)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(u)
}
+67
View File
@@ -0,0 +1,67 @@
// TODO: READ, UNDERSTAND AND ALTER
package main
import (
"net/http"
"github.com/go-chi/chi/v5"
)
func main() {
connectDB()
r := chi.NewRouter()
// routes
r.Get("/users", getUsers)
r.Post("/users", createUser)
http.ListenAndServe(":8080", r)
}
/*package main
import (
"fmt"
"encoding/json"
"net/http"
)
type Message struct {
Text string `json:"text"`
}
type User struct {
Name string `json:"name"`
}
// POST method handler
func createUserHandler(w http.ResponseWriter, r *http.Request) {
var user User
// decode json from request body
err := json.NewDecoder(r.Body).Decode(&user)
if err != nil {
http.Error(w, "invalid JSON", http.StatusBadRequest)
return
}
response := Message{Text: "User created: " + user.Name}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
// handler function
func helloHandler(w http.ResponseWriter, r *http.Request) {
response := Message{Text: "Hellow from Go API!"}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
func main() {
http.HandleFunc("/hello", helloHandler)
http.HandleFunc("/createUser", createUserHandler)
fmt.Println("server running on http://localhost:8080")
http.ListenAndServe(":8080", nil)
}*/
+7
View File
@@ -0,0 +1,7 @@
package main
type Quote struct {
Id int `json:"id"`
Author string `json:"author"`
Body string `json:"body"`
}
View File