|
1 | 1 | package main
|
2 | 2 |
|
| 3 | +import "database/sql" |
| 4 | +import "fmt" |
3 | 5 | import "log"
|
4 | 6 | import "net/http"
|
| 7 | +import "os" |
| 8 | +import _ "github.com/go-sql-driver/mysql" |
5 | 9 |
|
| 10 | +// @todo #1 /countries/count: add tests |
6 | 11 | func main() {
|
7 | 12 | log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
|
8 | 13 |
|
| 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 | + |
9 | 45 | http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
|
10 | 46 | w.Write([]byte("ok"))
|
11 | 47 | })
|
12 | 48 |
|
| 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 | + |
13 | 77 | log.Println("Running the server on port 8080")
|
14 | 78 |
|
15 | 79 | if err := http.ListenAndServe(":8080", nil); err != nil {
|
|
0 commit comments