-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (58 loc) · 1.87 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
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
Main entry point for our web app. Initialises the router and the mux ware,
sets the config variables, and serves the web application
Hassaan Ali Wattoo <[email protected]>
*/
package main
import (
"fmt"
"github.com/bndr/gotabulate"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/hassaanaliw/seekhlai-api/api"
"github.com/hassaanaliw/seekhlai-api/config"
"net/http"
"os"
)
// Main entry point for this application
func main() {
// Load Configuration
loadStatus, configuration := config.LoadConfig()
if !loadStatus {
// Kill if we can't load config file
fmt.Print("Error Loading Configuration File")
return
}
// Initialise the configuration logger
configOutputHeaders := []string{"Config Variables", "Config Value"}
row_debug := []interface{}{"Debug", configuration.Debug}
row_db_url := []interface{}{"Database URL", configuration.DatabaseURL}
row_port := []interface{}{"Port", configuration.Port}
if configuration.Debug {
TableOutput(configOutputHeaders, [][]interface{}{row_debug, row_db_url, row_port})
}
// Setup Router
router := mux.NewRouter()
api.RegisterAPIRoutes(router)
// Serve the HTTP Application
fmt.Printf("Serving web app on url: http://localhost:%d\n", configuration.Port)
err := http.ListenAndServe(fmt.Sprintf(":%d", configuration.Port),
handlers.LoggingHandler(os.Stdout, router))
if err != nil {
fmt.Println(err)
}
}
// Helper function to output data in a nice tabular format
// headers is a slice of table headers, rows are the rows to print
func TableOutput(headers []string, rows [][]interface{}) {
// Create an object from 2D interface array
t := gotabulate.Create(rows)
// Set the Headers (optional)
t.SetHeaders(headers)
// Set the Empty String (optional)
t.SetEmptyString("None")
// Set Align (Optional)
t.SetAlign("right")
// Print the result: grid, or simple
fmt.Println(t.Render("grid"))
}