forked from TannerGabriel/learning-go
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
92 lines (74 loc) · 1.88 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"fmt"
"os"
"github.com/gofiber/fiber"
"github.com/tannergabriel/learning-go/advanced-programs/FiberPostgresCRUD/database"
"github.com/tannergabriel/learning-go/advanced-programs/FiberPostgresCRUD/item"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
)
func helloWorld(c *fiber.Ctx) {
c.Send("Hello, World!")
}
func setupRoutes(app *fiber.App) {
app.Get("/", helloWorld)
app.Get("/api/v1/item", item.GetItems)
app.Get("/api/v1/item/:id", item.GetItem)
app.Post("/api/v1/item", item.NewItem)
app.Delete("/api/v1/item/:id", item.DeleteItem)
}
func initDatabase() {
var connectionString string
// Get environment variable for connection string
host := os.Getenv("HOST")
if "" == host {
connectionString = "host=localhost port=5432 user=postgres dbname=pq-demo password=example sslmode=disable"
} else {
connectionString = "host=" + host + " port=5432 user=postgres dbname=pq-demo password=example sslmode=disable"
}
var err error
database.DBConn, err = gorm.Open("postgres", connectionString)
if err != nil {
fmt.Println(err)
panic("failed to connect database")
}
fmt.Println("Connection Opened to Database")
}
func createTable() {
db := database.DBConn
// Delete table if you changed/updated the schema
// deleteQuery := `DROP TABLE IF EXISTS items`
// db.Exec(deleteQuery)
query := `
CREATE TABLE IF NOT EXISTS items
(
id serial NOT NULL,
Title character varying NOT NULL,
Owner character varying,
Rating integer,
created_at date,
updated_at date,
deleted_at date,
CONSTRAINT pk_books PRIMARY KEY (id )
)
WITH (
OIDS=FALSE
);
ALTER TABLE items
OWNER TO postgres;`
db.Exec(query)
}
func main() {
app := Setup()
app.Listen(3000)
defer database.DBConn.Close()
}
// Setup fiber app and database
func Setup() *fiber.App {
app := fiber.New()
initDatabase()
createTable()
setupRoutes(app)
return app
}