/
main.go
154 lines (123 loc) · 3.45 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
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
148
149
150
151
152
153
154
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"github.com/gin-gonic/gin"
"github.com/hashicorp/go-hclog"
_ "github.com/lib/pq"
"github.com/spf13/viper"
)
type Config struct {
DBUsername string `mapstructure:"DB_USERNAME"`
DBPassword string `mapstructure:"DB_PASSWORD"`
DBAddress string `mapstructure:"DB_ADDRESS"`
DBPort string `mapstructure:"DB_PORT"`
DBName string `mapstructure:"DB_NAME"`
ServerAddress string `mapstructure:"SERVER_ADDRESS"`
}
func LoadConfig(path string) (config *Config, err error) {
viper.AddConfigPath(path)
viper.SetConfigName("app")
viper.SetConfigType("env")
err = viper.ReadInConfig()
if err != nil {
return
}
viper.AutomaticEnv()
viper.SetDefault("SERVER_ADDRESS", "0.0.0.0:8080")
viper.SetDefault("DB_PORT", "5432")
viper.SetDefault("DB_NAME", "test")
if !viper.IsSet("DB_ADDRESS") {
return nil, fmt.Errorf("DB_ADDRESS must be set")
}
if !viper.IsSet("DB_USERNAME") {
return nil, fmt.Errorf("DB_USERNAME must be set")
}
if !viper.IsSet("DB_PASSWORD") {
return nil, fmt.Errorf("DB_PASSWORD must be set")
}
viper.Set("DB_ADDRESS", viper.GetString("DB_ADDRESS"))
viper.Set("DB_USERNAME", viper.GetString("DB_USERNAME"))
viper.Set("DB_PASSWORD", viper.GetString("DB_PASSWORD"))
err = viper.Unmarshal(&config)
return
}
type DatabaseCreds struct {
Username string `json:"username"`
Password string `json:"password"`
}
// getCreds responds with credentials for the database
func (env *Env) getCreds(c *gin.Context) {
c.IndentedJSON(http.StatusOK, env.creds)
}
type Product struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
}
type Env struct {
db *sql.DB
creds DatabaseCreds
}
func NewDB(dataSource string, creds DatabaseCreds) (*Env, error) {
db, err := sql.Open("postgres", dataSource)
if err != nil {
return nil, err
}
if err = db.Ping(); err != nil {
return nil, err
}
return &Env{db: db, creds: creds}, nil
}
// getProducts responds with the list of all products as JSON.
func (env *Env) getProducts(c *gin.Context) {
rows, err := env.db.Query("select id,name,description from products")
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
}
defer rows.Close()
products := make([]Product, 0)
for rows.Next() {
var id string
var name string
var description string
err = rows.Scan(&id, &name, &description)
if err != nil {
log.Printf("error occurred while reading the database rows: %v", err)
break
}
products = append(products, Product{
ID: id,
Name: name,
Description: description,
})
}
c.IndentedJSON(http.StatusOK, products)
}
func health(c *gin.Context) {
c.Status(http.StatusOK)
}
func main() {
configFilePath := viper.GetString("CONFIG_FILE_PATH")
if configFilePath == "" {
configFilePath = "."
}
config, err := LoadConfig(configFilePath)
if err != nil {
log.Fatal("cannot load config:", err)
}
datasource := fmt.Sprintf("postgresql://%s:%s@%s:%s/%s?sslmode=disable",
config.DBUsername, config.DBPassword, config.DBAddress, config.DBPort, config.DBName)
hclog.Default().Info(datasource)
env, err := NewDB(datasource, DatabaseCreds{Username: config.DBUsername, Password: config.DBPassword})
if err != nil {
log.Fatal("cannot connect to db:", err)
}
router := gin.Default()
router.GET("/", health)
router.GET("/products", env.getProducts)
router.GET("/creds", env.getCreds)
router.Run(config.ServerAddress)
}