-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
147 lines (127 loc) · 3.11 KB
/
server.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"database/sql"
"encoding/json"
"fmt"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/template/html"
_ "github.com/lib/pq"
"log"
"os"
)
type todo struct {
Item string
}
type TfOutput struct {
Sensitive bool
Type string
Value string
}
func indexHandler(c *fiber.Ctx, db *sql.DB) error {
var res string
var todos []string
rows, err := db.Query("SELECT * FROM todos")
defer rows.Close()
if err != nil {
log.Fatalln(err)
c.JSON("An error occured")
}
for rows.Next() {
rows.Scan(&res)
todos = append(todos, res)
}
return c.Render("index", fiber.Map{
"Todos": todos,
})
}
func postHandler(c *fiber.Ctx, db *sql.DB) error {
newTodo := todo{}
if err := c.BodyParser(&newTodo); err != nil {
log.Printf("An error occured: %v", err)
return c.SendString(err.Error())
}
fmt.Printf("%v", newTodo)
if newTodo.Item != "" {
_, err := db.Exec("INSERT into todos VALUES ($1)", newTodo.Item)
if err != nil {
log.Fatalf("An error occured while executing query: %v", err)
}
}
return c.Redirect("/")
}
func putHandler(c *fiber.Ctx, db *sql.DB) error {
olditem := c.Query("olditem")
newitem := c.Query("newitem")
db.Exec("UPDATE todos SET item=$1 WHERE item=$2", newitem, olditem)
return c.Redirect("/")
}
func deleteHandler(c *fiber.Ctx, db *sql.DB) error {
todoToDelete := c.Query("item")
db.Exec("DELETE from todos WHERE item=$1", todoToDelete)
return c.SendString("deleted")
}
func isJSON(s string) bool {
var js map[string]interface{}
return json.Unmarshal([]byte(s), &js) == nil
}
func main() {
var host TfOutput
var name TfOutput
var username TfOutput
var password TfOutput
var dbHost string
var dbName string
var dbUsername string
var dbPassword string
if isJSON(os.Getenv("DB_HOST")) {
json.Unmarshal([]byte(os.Getenv("DB_HOST")), &host)
dbHost = host.Value
} else {
dbHost = os.Getenv("DB_HOST")
}
if isJSON(os.Getenv("DB_NAME")) {
json.Unmarshal([]byte(os.Getenv("DB_NAME")), &name)
dbName = name.Value
} else {
dbName = os.Getenv("DB_NAME")
}
if isJSON(os.Getenv("DB_USERNAME")) {
json.Unmarshal([]byte(os.Getenv("DB_USERNAME")), &username)
dbUsername = username.Value
} else {
dbUsername = os.Getenv("DB_USERNAME")
}
if isJSON(os.Getenv("DB_PASSWORD")) {
json.Unmarshal([]byte(os.Getenv("DB_PASSWORD")), &password)
dbPassword = password.Value
} else {
dbPassword = os.Getenv("DB_PASSWORD")
}
connStr := "postgresql://" + dbUsername + ":" + dbPassword + "@" + dbHost + "/" + dbName + "?sslmode=disable"
db, err := sql.Open("postgres", connStr)
if err != nil {
log.Fatal(err)
}
engine := html.New("./views", ".html")
app := fiber.New(fiber.Config{
Views: engine,
})
app.Get("/", func(c *fiber.Ctx) error {
return indexHandler(c, db)
})
app.Post("/", func(c *fiber.Ctx) error {
return postHandler(c, db)
})
app.Put("/update", func(c *fiber.Ctx) error {
return putHandler(c, db)
})
app.Delete("/delete", func(c *fiber.Ctx) error {
return deleteHandler(c, db)
})
port := os.Getenv("SERVER_PORT")
if port == "" {
port = "8080"
}
app.Static("/", "./public")
log.Fatalln(app.Listen(fmt.Sprintf(":%v", port)))
}