Skip to content

Commit 6bec219

Browse files
committed
Move PGConfig to separate file
1 parent 62a26d6 commit 6bec219

File tree

2 files changed

+40
-35
lines changed

2 files changed

+40
-35
lines changed

main.go

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"database/sql"
66
"encoding/json"
7-
"fmt"
87
"log"
98
"os"
109
"time"
@@ -13,40 +12,6 @@ import (
1312
_ "github.com/lib/pq"
1413
)
1514

16-
// PGConfig contains configurations to connect to a PG database
17-
type PGConfig struct {
18-
Username string
19-
Password string
20-
Host string
21-
DBName string
22-
Query string
23-
FrequencyInMS int32
24-
Debug bool
25-
}
26-
27-
// ConnStr returns a connection string to connect to postgres
28-
func (c *PGConfig) ConnStr() string {
29-
return fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable", c.Username, c.Password, c.Host, c.DBName)
30-
}
31-
32-
// GetQuery returns the query to use to ping
33-
func (c *PGConfig) GetQuery() string {
34-
if c.Query == "" {
35-
return "select 1"
36-
}
37-
38-
return c.Query
39-
}
40-
41-
// GetFrequency returns the frequence in MS in which the query should be run
42-
func (c *PGConfig) GetFrequency() time.Duration {
43-
if c.FrequencyInMS == 0 {
44-
return time.Second
45-
}
46-
47-
return time.Duration(c.FrequencyInMS) * time.Millisecond
48-
}
49-
5015
func main() {
5116
var conf PGConfig
5217
err := envconfig.Process("PGPING", &conf)

pg_config.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
// PGConfig contains configurations to connect to a PG database
9+
type PGConfig struct {
10+
Username string
11+
Password string
12+
Host string
13+
DBName string
14+
Query string
15+
FrequencyInMS int32
16+
Debug bool
17+
}
18+
19+
// ConnStr returns a connection string to connect to postgres
20+
func (c *PGConfig) ConnStr() string {
21+
return fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable", c.Username, c.Password, c.Host, c.DBName)
22+
}
23+
24+
// GetQuery returns the query to use to ping
25+
func (c *PGConfig) GetQuery() string {
26+
if c.Query == "" {
27+
return "select 1"
28+
}
29+
30+
return c.Query
31+
}
32+
33+
// GetFrequency returns the frequence in MS in which the query should be run
34+
func (c *PGConfig) GetFrequency() time.Duration {
35+
if c.FrequencyInMS == 0 {
36+
return time.Second
37+
}
38+
39+
return time.Duration(c.FrequencyInMS) * time.Millisecond
40+
}

0 commit comments

Comments
 (0)