-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (46 loc) · 1.38 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
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"log"
"fmt"
_ "github.com/lib/pq"
"database/sql"
"net/http"
"github.com/gorilla/mux"
"github.com/fgx/flightgear-tracker-go/config"
"github.com/fgx/flightgear-tracker-go/tracker"
)
func main(){
//= Load config (LoadConfig will sys exit(1) if error)
conf := config.LoadConfig()
log.Println("Loaded config: ", conf)
//= Create DB Connection (tracker.Db is the connection pointer
url := fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable", conf.DbUser, conf.DbPassword, conf.DbServer, conf.Database)
var err error
tracker.Db, err = sql.Open("postgres", url)
if err != nil {
log.Fatal(err)
}
//= Test db connection
con_err := tracker.Db.Ping()
if con_err != nil {
log.Fatal("DB error: (check config)", con_err)
}
log.Println("DB connected: OK ")
defer tracker.Db.Close()
//= Setup routing
router := mux.NewRouter()
//m.Get("/", func() string {
// return "yes"
//})
router.HandleFunc("/flights/{callsign}", tracker.FlightsByCallsign_AjaxHandler)
router.HandleFunc("/flight/{flight_id}", tracker.Flight_AjaxHandler)
http.Handle("/", router)
//= Start Server
server_address := fmt.Sprintf(":%d", conf.HttpPort)
log.Println("Listening on: ", server_address)
if err := http.ListenAndServe(server_address, nil); err != nil {
panic(err)
}
// Lets go !
log.Println("Listening on: ", conf.HttpPort)
}