Skip to content

Commit 8579598

Browse files
committed
feat(/v0.1/countries/count): implement.
Fix #1
1 parent 0a1b7cc commit 8579598

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/php-coder/mystamps-country
2+
3+
go 1.13
4+
5+
require github.com/go-sql-driver/mysql v1.4.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA=
2+
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=

main.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,79 @@
11
package main
22

3+
import "database/sql"
4+
import "fmt"
35
import "log"
46
import "net/http"
7+
import "os"
8+
import _ "github.com/go-sql-driver/mysql"
59

10+
// @todo #1 /countries/count: add tests
611
func main() {
712
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
813

14+
// @todo #1 Load configuration from a file
15+
user := os.Getenv("MYSQL_USER")
16+
if user == "" {
17+
log.Printf("MYSQL_USER env variable is not set. Defaults to 'mystamps'")
18+
user = "mystamps"
19+
}
20+
21+
pass := os.Getenv("MYSQL_PASSWORD")
22+
if pass == "" {
23+
log.Fatalf("MYSQL_PASSWORD env variable is not set")
24+
}
25+
26+
dbName := os.Getenv("MYSQL_DB")
27+
if dbName == "" {
28+
log.Printf("MYSQL_DB env variable is not set. Defaults to 'mystamps'")
29+
dbName = "mystamps"
30+
}
31+
32+
// @todo #1 Consider passing params to db driver
33+
dsn := fmt.Sprintf("%s:%s@tcp(localhost:3306)/%s", user, pass, dbName)
34+
35+
db, err := sql.Open("mysql", dsn)
36+
if err != nil {
37+
log.Fatalf("Open() has failed: %v", err)
38+
}
39+
40+
err = db.Ping()
41+
if err != nil {
42+
log.Fatalf("Ping() has failed: %v", err)
43+
}
44+
945
http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
1046
w.Write([]byte("ok"))
1147
})
1248

49+
// @todo #1 /countries/count: extract handler
50+
// @todo #1 /countries/count: extract SQL query
51+
// @todo #1 /countries/count: return JSON response
52+
http.HandleFunc("/v0.1/countries/count", func(w http.ResponseWriter, r *http.Request) {
53+
if r.Method != "GET" {
54+
http.Error(w, http.StatusText(405), 405)
55+
return
56+
}
57+
58+
// There is no check for ErrNoRows because COUNT(*) always returns a single row
59+
row := db.QueryRow("SELECT COUNT(*) FROM countries")
60+
if err != nil {
61+
log.Printf("QueryRow() has failed: %v", err)
62+
http.Error(w, http.StatusText(500), 500)
63+
return
64+
}
65+
66+
var count int
67+
err := row.Scan(&count)
68+
if err != nil {
69+
log.Printf("Scan() has failed: %v", err)
70+
http.Error(w, http.StatusText(500), 500)
71+
return
72+
}
73+
74+
fmt.Fprintf(w, "%d", count)
75+
})
76+
1377
log.Println("Running the server on port 8080")
1478

1579
if err := http.ListenAndServe(":8080", nil); err != nil {

0 commit comments

Comments
 (0)