-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
86 lines (67 loc) · 1.71 KB
/
main.go
File metadata and controls
86 lines (67 loc) · 1.71 KB
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
package main
import (
_ "github.com/lib/pq"
"log"
"net/http"
"os"
"fmt"
"github.com/bnsd55/ionic-project/BL"
"strconv"
"github.com/julienschmidt/httprouter"
)
/*
func index(res http.ResponseWriter, req *http.Request) {
dbInfo := fmt.Sprintf("user=%s password=%s dbname=%s host=%s sslmode=require",
DB_USER, DB_PASSWORD, DB_NAME, DB_HOST)
db, err := sql.Open("postgres", dbInfo)
defer db.Close()
if err != nil {
log.Fatal(err)
}
rows, errr := db.Query("SELECT * FROM users")
defer rows.Close()
if errr != nil {
log.Fatal(errr)
}
var results = make([]string, 2)
for rows.Next() {
var id int
var username string
err = rows.Scan(&id, &username)
results = append(results, fmt.Sprintf("id: %v, username: %v", id, username))
}
slcB, _ := json.Marshal(results)
io.WriteString(res, string(slcB))
}
func name(res http.ResponseWriter, req *http.Request) {
io.WriteString(res, "ben")
}
*/
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprint(w, "Welcome!\n")
}
func Drivers(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprint(w, BL.GetAllDrivers())
}
func DriverById(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
id, err := strconv.Atoi(params.ByName("id"))
if err != nil {
fmt.Fprint(w, "An error occuer when trying to convert string to int")
return
}
fmt.Fprint(w, BL.GetDriverByID(id))
}
func main() {
port := os.Getenv("PORT")
if port == "" {
log.Fatal("$PORT must be set")
}
router := httprouter.New()
router.GET("/", Index)
router.GET("/drivers", Drivers)
router.GET("/driver/:id", DriverById)
err := http.ListenAndServe(":" + port, router)
if err != nil {
log.Fatalln("Server error: ", err)
}
}