-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
87 lines (74 loc) · 2.04 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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"github.com/go-pg/pg"
"github.com/go-pg/pg/orm"
)
var (
opts pg.Options
)
// User represents a user in our database
type User struct {
Id int64
Name string
Emails []string
}
func init() {
// We take the various configuration parameters from environment variables.
// This makes it easy to have the docker image wrapping this application
// to run in various environments, say with k8s config maps.
// In a production application, you might want to use something like
// github.com/spf13/cobra/cobra or
// gopkg.in/alecthomas/kingpin.v2
// instead
opts.User = mustHaveEnv("POSTGRES_USER")
opts.Password = mustHaveEnv("POSTGRES_PASSWORD")
opts.Addr = fmt.Sprintf("%s:%s", mustHaveEnv("POSTGRES_HOST"), mustHaveEnv("POSTGRES_PORT"))
opts.Database = mustHaveEnv("POSTGRES_DB")
// We want to make sure we are connected
opts.OnConnect = func(*pg.Conn) error {
log.Println("Callback: Connected with database!")
return nil
}
}
func main() {
log.Println("Starting up...")
// Beware: go-pg/pg seems to lazily connect...
db := pg.Connect(&opts)
defer db.Close()
// We want to make sure we actually do have a schema
if err := createSchema(db); err != nil {
panic(err)
}
// Here, you could set up your api. This is obviously just a crude example.
http.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(db.PoolStats())
})
log.Fatal(http.ListenAndServe(":8080", nil))
}
// Helper function to ensure the required parameters are existing.
func mustHaveEnv(env string) string {
val := os.Getenv(env)
if val == "" {
panic(env + " must be present")
}
return val
}
// Helper function to create a schema
// Blatantly copied from https://github.com/go-pg/pg#look--feel
func createSchema(db *pg.DB) error {
for _, model := range []interface{}{(*User)(nil)} {
err := db.CreateTable(model, &orm.CreateTableOptions{
// Difference: We want a persistent table
Temp: false,
})
if err != nil {
return err
}
}
return nil
}