-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
61 lines (50 loc) · 1.26 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"os"
_ "github.com/lib/pq"
"github.com/yourusername/go-user-service/api"
)
func main() {
// Database connection
var err error
dbHost := os.Getenv("DB_HOST")
dbPort := os.Getenv("DB_PORT")
dbUser := os.Getenv("DB_USER")
dbPassword := os.Getenv("DB_PASSWORD")
dbName := os.Getenv("DB_NAME")
psqlInfo := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
dbHost, dbPort, dbUser, dbPassword, dbName)
api.Db, err = sql.Open("postgres", psqlInfo)
if err != nil {
log.Fatal(err)
}
defer api.Db.Close()
// Create users table if it doesn't exist
createTable()
// Set up router
router := api.SetupRoutes()
// Start server
fmt.Println("Server is running on port 8000...")
log.Fatal(http.ListenAndServe(":8000", router))
}
func createTable() {
query := `
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
email TEXT NOT NULL UNIQUE,
name TEXT NOT NULL,
password TEXT NOT NULL
);
`
_, err := api.Db.Exec(query)
if err != nil {
log.Fatalf("Error creating table: %v", err)
} else {
log.Println("Table 'users' created or already exists.")
}
}