maybe working docker build

This commit is contained in:
Johannes Theiner 2020-07-20 19:42:18 +02:00
parent 3559046dcd
commit 16138f930d
5 changed files with 50 additions and 12 deletions

View File

@ -1,12 +1,11 @@
FROM golang:1.14.6-alpine3.12 FROM golang:1.14.6-alpine3.12
RUN mkdir /src RUN mkdir /src
ADD . /src ADD . /src
WORKDIR /src WORKDIR /src
EXPOSE 8080
RUN go mod download RUN go mod download
RUN go build -o main . RUN go build -o main .
CMD["/src/main"] ENTRYPOINT["/src/main"]

View File

@ -13,6 +13,8 @@ type Configuration struct {
Port int Port int
JwtSecret string JwtSecret string
SentryDsn string
} }
func (config Configuration) String() string { func (config Configuration) String() string {
@ -27,6 +29,7 @@ func LoadConfiguration() *Configuration {
Units: getEnv("UNITS", "metric"), Units: getEnv("UNITS", "metric"),
Port: getEnvAsInt("PORT", 8080), Port: getEnvAsInt("PORT", 8080),
JwtSecret: getEnv("JWT_SECRET", "super secret value"), JwtSecret: getEnv("JWT_SECRET", "super secret value"),
SentryDsn: getEnv("SENTRY_DSN", ""),
} }
} }

View File

@ -1,9 +1,20 @@
package main package main
import ( import (
"./config"
"./server" "./server"
"github.com/getsentry/sentry-go"
) )
func main() { func main() {
conf := config.LoadConfiguration()
if conf.SentryDsn != "" {
if err := sentry.Init(sentry.ClientOptions{
Dsn: conf.SentryDsn,
}); err != nil {
panic(err)
}
}
server.StartServer() server.StartServer()
} }

View File

@ -6,12 +6,13 @@ import (
"encoding/json" "encoding/json"
"github.com/auth0/go-jwt-middleware" "github.com/auth0/go-jwt-middleware"
"github.com/dgrijalva/jwt-go" "github.com/dgrijalva/jwt-go"
"github.com/getsentry/sentry-go/http"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/justinas/alice" "github.com/justinas/alice"
"github.com/justinas/nosurf" "github.com/justinas/nosurf"
"github.com/throttled/throttled" "github.com/throttled/throttled"
"github.com/throttled/throttled/store/memstore" "github.com/throttled/throttled/store/memstore"
cache "github.com/victorspringer/http-cache" "github.com/victorspringer/http-cache"
"github.com/victorspringer/http-cache/adapter/memory" "github.com/victorspringer/http-cache/adapter/memory"
"log" "log"
"net/http" "net/http"
@ -67,12 +68,30 @@ func StartServer() {
}) })
chain := alice.New(jwtAuth.Handler, timeoutHandler, httpRateLimiter.RateLimit, nosurf.NewPure, cacheClient.Middleware) 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 := mux.NewRouter()
router.HandleFunc("/", defaultHandler)
router.HandleFunc("/city/{city}", chain.ThenFunc(cityHandler).ServeHTTP) router.HandleFunc("/city/{city}", chain.ThenFunc(cityHandler).ServeHTTP)
router.HandleFunc("/coordinates/{latitude},{longitude}", chain.ThenFunc(coordinatesHandler).ServeHTTP) router.HandleFunc("/coordinates/{latitude},{longitude}", chain.ThenFunc(coordinatesHandler).ServeHTTP)
router.HandleFunc("/zip/{zip},{country}", chain.ThenFunc(zipCodeHandler).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) { func cityHandler(writer http.ResponseWriter, req *http.Request) {
@ -80,7 +99,9 @@ func cityHandler(writer http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req) vars := mux.Vars(req)
data := client.GetByCityName(vars["city"]) data := client.GetByCityName(vars["city"])
writer.WriteHeader(http.StatusOK) 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) { 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) longitude, err := strconv.ParseFloat(vars["longitude"], 32)
if err != nil { if err != nil {
log.Fatal(err) panic(err)
} }
latitude, err := strconv.ParseFloat(vars["latitude"], 32) latitude, err := strconv.ParseFloat(vars["latitude"], 32)
if err != nil { if err != nil {
log.Fatal(err) panic(err)
} }
data := client.GetByCoordinates(longitude, latitude) data := client.GetByCoordinates(longitude, latitude)
writer.WriteHeader(http.StatusOK) 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) { func zipCodeHandler(writer http.ResponseWriter, req *http.Request) {
@ -105,11 +128,13 @@ func zipCodeHandler(writer http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req) vars := mux.Vars(req)
zip, err := strconv.Atoi(vars["zip"]) zip, err := strconv.Atoi(vars["zip"])
if err != nil { if err != nil {
log.Fatal(err) panic(err)
} }
data := client.GetByZipCode(zip, vars["country"]) data := client.GetByZipCode(zip, vars["country"])
writer.WriteHeader(http.StatusOK) 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 { func timeoutHandler(h http.Handler) http.Handler {