-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
110 lines (94 loc) · 3.03 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
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/gorilla/mux"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"net/http"
"os"
)
var service Service
var (
version = "dev"
commit = "none"
date = "unknown"
)
func main() {
var (
httpAddr = flag.String("http.addr", ":6066", "Address for HTTP server")
cacheTime = flag.Int("cache_time", 10, "Cache API calls for X minutes")
debug = flag.Bool("debug", false, "sets log level to debug")
jsonFile = flag.String("file", "", "Local json file to use instead of weather api")
)
flag.Parse()
// Default level for this example is info, unless debug flag is present
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if *debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout})
log.Info().Str("version", version).Str("commit", commit).Str("date", date).Msg("")
apiKey := os.Getenv("TOMORROW_APIKEY")
if apiKey == "" {
log.Fatal().Msg("missing required environment variable TOMORROW_APIKEY")
os.Exit(1)
}
service = Service{apiKey, *jsonFile, *cacheTime}
r := mux.NewRouter()
c := LogHttp(log.Logger)
r.Handle("/forecast/", c.Then(http.HandlerFunc(WeatherHandler)))
r.Handle("/interface/api", c.Then(http.HandlerFunc(ApiHandler)))
r.Handle("/", c.Then(http.HandlerFunc(HealthHandler)))
r.Handle("/interface/api.php", c.Then(http.HandlerFunc(ApiHandler)))
if err := http.ListenAndServe(*httpAddr, r); err != nil {
log.Fatal().Str("status", "fatal").Err(err).Msg("fatal error")
os.Exit(1)
}
}
func WeatherHandler(w http.ResponseWriter, r *http.Request) {
asl := r.URL.Query().Get("asl")
coord := r.URL.Query().Get("coord") // 50.993290,4.869030 // reverse coordinates ...
format := r.URL.Query().Get("format")
result, err := service.GetForecast(coord, asl)
if err != nil {
log.Error().Interface("result", result).Err(err).Msg("error while getting forecast")
w.WriteHeader(http.StatusInternalServerError)
} else {
w.Header().Set("Vary", "Accept-Encoding")
w.Header().Set("Connection", "close")
w.Header().Set("Transfer-Encoding", "chunked")
if format == "json" {
w.Header().Set("Content-Type", "text/json")
service.WriteJSON(w, result)
} else {
w.Header().Set("Content-Type", "text/xml")
service.WriteXML(w, result)
}
}
}
func HealthHandler(w http.ResponseWriter, r *http.Request) {
log.Info().Msg("received health check")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Server is healthy")
}
func ApiHandler(w http.ResponseWriter, r *http.Request) {
log.Info().Msg("received /interface/api call")
w.Header().Set("Vary", "Accept-Encoding")
w.Header().Set("Connection", "close")
w.Header().Set("Transfer-Encoding", "chunked")
w.Header().Set("Content-Type", "text/json")
result := map[string]string{
"1": "2100-01-01",
"2": "2100-01-01",
}
response, err := json.Marshal(result)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Error().Interface("result", result).Err(err).Msg("cannot marshall object")
return
}
w.WriteHeader(http.StatusOK)
w.Write(response)
}