diff --git a/Dockerfile b/Dockerfile index f11413d..fc384e2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,11 @@ FROM golang:1.14.6-alpine3.12 - RUN mkdir /src - ADD . /src - WORKDIR /src +EXPOSE 8080 + RUN go mod download RUN go build -o main . -CMD["/src/main"] \ No newline at end of file +ENTRYPOINT["/src/main"] diff --git a/src/client/CurrentWeatherStruct.go b/src/client/WeatherStruct.go similarity index 100% rename from src/client/CurrentWeatherStruct.go rename to src/client/WeatherStruct.go diff --git a/src/config/Configuration.go b/src/config/Configuration.go index 5108749..0c3c908 100644 --- a/src/config/Configuration.go +++ b/src/config/Configuration.go @@ -13,6 +13,8 @@ type Configuration struct { Port int JwtSecret string + + SentryDsn string } func (config Configuration) String() string { @@ -27,6 +29,7 @@ func LoadConfiguration() *Configuration { Units: getEnv("UNITS", "metric"), Port: getEnvAsInt("PORT", 8080), JwtSecret: getEnv("JWT_SECRET", "super secret value"), + SentryDsn: getEnv("SENTRY_DSN", ""), } } diff --git a/src/main.go b/src/main.go index 8011998..c56ddc6 100644 --- a/src/main.go +++ b/src/main.go @@ -1,9 +1,20 @@ package main import ( + "./config" "./server" + "github.com/getsentry/sentry-go" ) func main() { + conf := config.LoadConfiguration() + if conf.SentryDsn != "" { + if err := sentry.Init(sentry.ClientOptions{ + Dsn: conf.SentryDsn, + }); err != nil { + panic(err) + } + } + server.StartServer() } diff --git a/src/server/WeatherServer.go b/src/server/WeatherServer.go index db8c8a2..d472852 100644 --- a/src/server/WeatherServer.go +++ b/src/server/WeatherServer.go @@ -6,12 +6,13 @@ import ( "encoding/json" "github.com/auth0/go-jwt-middleware" "github.com/dgrijalva/jwt-go" + "github.com/getsentry/sentry-go/http" "github.com/gorilla/mux" "github.com/justinas/alice" "github.com/justinas/nosurf" "github.com/throttled/throttled" "github.com/throttled/throttled/store/memstore" - cache "github.com/victorspringer/http-cache" + "github.com/victorspringer/http-cache" "github.com/victorspringer/http-cache/adapter/memory" "log" "net/http" @@ -67,12 +68,30 @@ func StartServer() { }) chain := alice.New(jwtAuth.Handler, timeoutHandler, httpRateLimiter.RateLimit, nosurf.NewPure, cacheClient.Middleware) + if conf.SentryDsn != "" { + + sentryHandler := sentryhttp.New(sentryhttp.Options{ + Repanic: true, + }) + chain.Append(sentryHandler.Handle) + } router := mux.NewRouter() + router.HandleFunc("/", defaultHandler) router.HandleFunc("/city/{city}", chain.ThenFunc(cityHandler).ServeHTTP) router.HandleFunc("/coordinates/{latitude},{longitude}", chain.ThenFunc(coordinatesHandler).ServeHTTP) router.HandleFunc("/zip/{zip},{country}", chain.ThenFunc(zipCodeHandler).ServeHTTP) - _ = http.ListenAndServe(":"+port, router) + if err := http.ListenAndServe(":"+port, router); err != nil { + panic(err) + } +} + +func defaultHandler(writer http.ResponseWriter, _ *http.Request) { + writer.WriteHeader(http.StatusOK) + if _, err := writer.Write([]byte("Hello World")); err != nil { + panic(err) + } + } func cityHandler(writer http.ResponseWriter, req *http.Request) { @@ -80,7 +99,9 @@ func cityHandler(writer http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) data := client.GetByCityName(vars["city"]) writer.WriteHeader(http.StatusOK) - _ = json.NewEncoder(writer).Encode(convert(data)) + if err := json.NewEncoder(writer).Encode(convert(data)); err != nil { + panic(err) + } } func coordinatesHandler(writer http.ResponseWriter, req *http.Request) { @@ -89,15 +110,17 @@ func coordinatesHandler(writer http.ResponseWriter, req *http.Request) { longitude, err := strconv.ParseFloat(vars["longitude"], 32) if err != nil { - log.Fatal(err) + panic(err) } latitude, err := strconv.ParseFloat(vars["latitude"], 32) if err != nil { - log.Fatal(err) + panic(err) } data := client.GetByCoordinates(longitude, latitude) writer.WriteHeader(http.StatusOK) - _ = json.NewEncoder(writer).Encode(convert(data)) + if err := json.NewEncoder(writer).Encode(convert(data)); err != nil { + panic(err) + } } func zipCodeHandler(writer http.ResponseWriter, req *http.Request) { @@ -105,11 +128,13 @@ func zipCodeHandler(writer http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) zip, err := strconv.Atoi(vars["zip"]) if err != nil { - log.Fatal(err) + panic(err) } data := client.GetByZipCode(zip, vars["country"]) writer.WriteHeader(http.StatusOK) - _ = json.NewEncoder(writer).Encode(convert(data)) + if err := json.NewEncoder(writer).Encode(convert(data)); err != nil { + panic(err) + } } func timeoutHandler(h http.Handler) http.Handler {