-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
54 lines (44 loc) · 1.14 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
package main
import (
"fmt"
"log"
"os"
"strconv"
"github.com/go-pg/pg/v9"
_ "github.com/go-pg/pg/v9/orm"
)
func main() {
repository := NewRepository(&pg.Options{
User: getenv("DB_USER", "postgres"),
Password: getenv("DB_PASS", "postgres"),
Database: getenv("DB_NAME", "vehicles"),
Addr: getenv("DB_ADDR", "localhost:5432"),
})
defer repository.Close()
s := NewService(repository)
defer s.Close()
server := NewServer()
server.Get("/", s.GetRoot)
server.Get("/manufacturers", s.GetManufacturers)
server.Get("/manufacturers/{hsn}", s.GetManufacturer)
server.Get("/manufacturers/{hsn}/vehicles", s.GetVehicles)
server.Get("/manufacturers/{hsn}/vehicles/{tsn}", s.GetVehicle)
server.Get("/powerSources", s.GetPowerSources)
server.Get("/powerSources/{id}", s.GetPowerSource)
log.Fatal(server.Start(fmt.Sprintf(":%d", getPort())))
}
func getenv(name, defaultValue string) string {
if value, ok := os.LookupEnv(name); ok {
return value
}
return defaultValue
}
func getPort() int {
portStr := os.Getenv("PORT")
if portStr != "" {
if port, err := strconv.Atoi(portStr); err == nil {
return port
}
}
return 8080
}