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
RUN mkdir /src
ADD . /src
WORKDIR /src
EXPOSE 8080
RUN go mod download
RUN go build -o main .
CMD["/src/main"]
ENTRYPOINT["/src/main"]

View File

@ -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", ""),
}
}

View File

@ -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()
}

View File

@ -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 {