-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
139 lines (118 loc) · 3.88 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// This script creates a web server using the net/http package and the gorilla/mux router package,
// and exposes several routes to handle different types of requests (csv file, serving json and API documentation, retrieving data from an API..)
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"github.com/gorilla/mux"
"github.com/spf13/viper"
)
type Station []struct {
CodeUic int `json:"code_uic_complet"`
NomGare string `json:"nom_gare"`
Total2015 int `json:"total_voyageurs_non_voyageurs_2015"`
Total2016 int `json:"total_voyageurs_non_voyageurs_2016"`
Total2017 int `json:"total_voyageurs_non_voyageurs_2017"`
Total2018 int `json:"total_voyageurs_non_voyageurs_2018"`
Total2019 int `json:"total_voyageurs_non_voyageurs_2019"`
Total2020 int `json:"total_voyageurs_non_voyageurs_2020"`
Total2021 int `json:"total_voyageurs_non_voyageurs_2021"`
}
type StationE []struct {
CodeUic int `json:"code_uic"`
NomGare string `json:"cp"`
Total2015 int `json:"a2015"`
Total2016 int `json:"a2016"`
Total2017 int `json:"a2017"`
Total2018 int `json:"a2018"`
Total2019 int `json:"2019"`
Total2020 int `json:"a2020"`
Total2021 int `json:"a2021"`
}
// Config struct holds the environment variables
type Config struct {
PORT string `mapstructure:"PORT"`
HOST string `mapstructure:"HOST"`
}
// LoadConfig loads the env file data to a struct
func LoadConfig(path string) (config Config, err error) {
viper.AddConfigPath(path)
viper.SetConfigName("app")
viper.SetConfigType("env")
viper.AutomaticEnv()
err = viper.ReadInConfig()
if err != nil {
return
}
err = viper.Unmarshal(&config)
return
}
// serveJson function serves a JSON file to the client
func serveJson(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "data/output.json")
}
// serveRawDoc function serves the API documentation to the client
func serveRawDoc(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "swagger.json")
}
// sendData function retrieves data from an API based on request parameters 'uic' and 'zipcode' and returns the data in json format
func sendData(w http.ResponseWriter, r *http.Request) {
// retrieve request parameters
uiccode := r.URL.Query()["uic"]
zipcode := r.URL.Query()["zipcode"]
// url from which the data will be fetched
url := "https://lab.jmg-conseil.eu/cell"
// Build the request
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal("Requete : ", err)
return
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal("Do: ", err)
return
}
defer resp.Body.Close()
var station Station
// Use json.Decode for reading streams of JSON data
if err := json.NewDecoder(resp.Body).Decode(&station); err != nil {
log.Println(err)
}
if uiccode != nil {
fmt.Printf("Parametre de recherche : Code UIC %s\n\n", uiccode)
}
if zipcode != nil {
fmt.Printf("Parametre de recherche : Code postal %s\n\n", zipcode)
}
// looping over the received data to find the station with the matching uic code
for i := 0; i < len(uiccode); i++ {
var id, err = strconv.Atoi(uiccode[i])
if err != nil {
log.Fatal("Requete: ", err)
return
}
for s := 0; s < len(station); s++ {
if station[s].CodeUic == id {
fmt.Fprint(w, station[i].Total2015, station[i].Total2016, station[i].Total2017, station[i].Total2018, station[i].Total2019, station[i].Total2020, station[i].Total2021, "&")
}
}
}
}
func main() {
// load app.env file data to struct
config, err := LoadConfig(".")
router := mux.NewRouter()
router.HandleFunc("/cell/station", sendData).Methods("GET")
//router.HandleFunc("/cell/csv", csvReader).Methods("GET")
router.HandleFunc("/cell", serveJson).Methods("GET")
router.HandleFunc("/cell/raw", serveRawDoc).Methods("GET")
log.Fatal(http.ListenAndServe(config.PORT, router))
if err != nil {
log.Fatalf("failed connection: %v", err)
}
}