Implemented logger, write to file on disk

This commit is contained in:
2022-12-17 14:22:56 +01:00
parent 4c1686cf49
commit 576a4e1579
6 changed files with 160 additions and 28 deletions

View File

@@ -2,15 +2,16 @@ package handler
import (
"encoding/json"
log "github.com/kevinmidboe/planetposen-images/logger"
"net/http"
log "github.com/sirupsen/logrus"
)
var logger = log.InitLogger()
// handleError - Logs the error (if shouldLog is true), and outputs the error message (msg)
func handleError(w http.ResponseWriter, err error, msg string, statusCode int, shouldLog bool) {
if shouldLog {
log.WithField("err", err).Error(msg)
logger.Error(msg, err)
}
w.Header().Set("Content-Type", "application/json")
@@ -21,4 +22,19 @@ func handleError(w http.ResponseWriter, err error, msg string, statusCode int, s
Error: msg,
})
w.Write(errorJSON)
}
}
func handleGoogleApiError(w http.ResponseWriter, err error, msg string, statusCode int, shouldLog bool) {
if shouldLog {
logger.GoogleApiError(msg, err)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
errorJSON, _ := json.Marshal(struct {
Error string `json:"error"`
}{
Error: msg,
})
w.Write(errorJSON)
}

View File

@@ -40,16 +40,17 @@ func UploadImages(hostname string, gcsClient gcs.Client) http.HandlerFunc {
filename := strings.ReplaceAll(fileHeader.Filename, "/", "-")
defer file.Close()
logger.InfoWithFilename("uploading image with filename", filename)
writer, path, err := gcsClient.FileWriter(ctx, filename)
if err != nil {
handleError(w, err, "File unable to write file to gcs", http.StatusServiceUnavailable, true)
handleGoogleApiError(w, err, "File unable to write file to gcs", http.StatusServiceUnavailable, true)
return
}
defer writer.Close()
_, err = io.Copy(writer, file)
if err != nil {
handleError(w, err, "Error copying file to GCS", http.StatusInternalServerError, true)
handleGoogleApiError(w, err, "Error copying file to GCS", http.StatusInternalServerError, true)
}
finalURL := util.ImageURL(hostname, string(path))
@@ -57,6 +58,7 @@ func UploadImages(hostname string, gcsClient gcs.Client) http.HandlerFunc {
Path: string(path),
URL: finalURL,
}
logger.UploadSuccessMessage(string(path), finalURL)
responseData, _ := json.Marshal(responseStruct)
_, _ = w.Write(responseData)
@@ -68,6 +70,9 @@ func FetchImage(gcsClient gcs.Client) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
path := gcs.EncodedPath(mux.Vars(r)["path"])
logger.InfoWithPath("Getting image", string(path))
if path == "" {
handleError(w, nil, "missing image path ", http.StatusBadRequest, true)
return
@@ -75,7 +80,7 @@ func FetchImage(gcsClient gcs.Client) http.HandlerFunc {
reader, err := gcsClient.FileReader(ctx, path)
if err != nil {
handleError(w, err, "error from gcs file reader ", http.StatusBadRequest, true)
handleGoogleApiError(w, err, "error from gcs file reader ", http.StatusBadRequest, true)
return
}
defer reader.Close()
@@ -87,6 +92,8 @@ func FetchImage(gcsClient gcs.Client) http.HandlerFunc {
w.Header().Set("Content-Type", fmt.Sprintf("image/%s", extension[1:]))
}
logger.InfoWithFilename("found and returning file from bucket", string(filename))
_, err = io.Copy(w, reader)
if err != nil {
handleError(w, err, "Couldn't copy the file from GCS ", http.StatusInternalServerError, true)
@@ -96,11 +103,12 @@ func FetchImage(gcsClient gcs.Client) http.HandlerFunc {
func ListImages(gcsClient gcs.Client) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
logger.Info("Listing images")
ctx := r.Context()
files, err := gcsClient.FileLister(ctx)
if err != nil {
handleError(w, err, "error from gcs file lister ", http.StatusBadRequest, true)
handleGoogleApiError(w, err, "error from gcs file lister ", http.StatusBadRequest, true)
return
}

View File

@@ -1,12 +1,14 @@
// Package server provides functionality to easily set up an HTTTP server.
//
// Clients:
// Database
//
// Database
package server
import (
"context"
"fmt"
log "github.com/kevinmidboe/planetposen-images/logger"
"net/http"
"os"
"os/signal"
@@ -15,7 +17,6 @@ import (
"github.com/gorilla/mux"
"github.com/kevinmidboe/planetposen-images/clients/gcs"
"github.com/kevinmidboe/planetposen-images/config"
log "github.com/sirupsen/logrus"
)
// Server holds the HTTP server, router, config and all clients.
@@ -26,6 +27,8 @@ type Server struct {
GCSClient gcs.Client
}
var logger = log.InitLogger()
// Create sets up the HTTP server, router and all clients.
// Returns an error if an error occurs.
func (s *Server) Create(ctx context.Context, config *config.Config) error {
@@ -68,17 +71,17 @@ func (s *Server) Serve(ctx context.Context) error {
<-stop
log.Info("Shutdown signal received")
logger.Info("Shutdown signal received")
if err := s.HTTP.Shutdown(ctx); err != nil {
log.Error(err.Error())
logger.Error("Error causing shutdown", err)
}
}(ctx, s)
log.Infof("Ready at: %s", s.Config.Port)
logger.Info("Ready at: " + s.Config.Port)
if err := s.HTTP.ListenAndServe(); err != http.ErrServerClosed {
log.Fatalf(err.Error())
logger.Fatal(err)
}
return nil