Alex Borisov
8cf28c6ccd
commit2df31908d6
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 22:11:21 2024 +0300 add license commit358d4a13c9
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 22:07:48 2024 +0300 Update project name in readme commit06d5114fb2
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 22:04:16 2024 +0300 ._. commit1b64366283
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 22:01:34 2024 +0300 added readme commit3fab4d1717
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 21:47:10 2024 +0300 Update Dockerfile to fix COPY command commit12fece4ebc
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 21:46:00 2024 +0300 Update Docker build command and variable names commitb12bc0a52f
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 21:43:42 2024 +0300 Update build.yaml to include Docker image listing commitf89595f34d
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 21:36:36 2024 +0300 Update build.yaml file commite41d8a3244
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 21:34:50 2024 +0300 Update build.yaml with Docker commands commita4681c3cc6
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 20:59:53 2024 +0300 Update Docker service startup command commit95effac38b
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 20:54:32 2024 +0300 Update Docker service startup command commit3b7331e6fd
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 20:53:31 2024 +0300 Update Docker service startup in build.yaml commit8761116752
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 20:52:12 2024 +0300 Update build.yaml file commitb2e9f5c8dd
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 20:33:00 2024 +0300 Add dependency on Build-Binary job commitdf90be4af3
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 20:32:01 2024 +0300 Fix date formatting in build.yaml commitf2a11e0378
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 20:30:55 2024 +0300 Remove Docker and Golang build workflows commit9e71dc47db
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 20:27:38 2024 +0300 Remove build.yaml and update Dockerfile commitfa4a13f428
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 20:14:56 2024 +0300 Fix casing issue in package and branch names commit5e018c5bcc
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 20:03:53 2024 +0300 final commita0276453a6
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 20:02:59 2024 +0300 Update build.yaml file commitc962a1985c
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 20:00:34 2024 +0300 Add URL to package in build.yaml commit7b6f68f47b
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 19:59:21 2024 +0300 Add environment variable for production deployment commit913843ff3b
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 19:58:00 2024 +0300 Add environment variable and build script for production deployment commit0078d4551b
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 19:45:55 2024 +0300 Fix Docker image tagging and binary build paths commitcc6b6ed103
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 19:16:04 2024 +0300 Add package_name and branch_name variables to Docker image upload script commitd07dcbaa3b
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 19:14:47 2024 +0300 Update build.yaml to include binary name in go build command commit048e31987f
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 19:13:31 2024 +0300 Refactor build.yaml to simplify go build command commitb5e6fbf388
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 19:11:14 2024 +0300 Update build.yaml to include current date in echo statement commitdc8b94b35c
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 19:08:11 2024 +0300 Update Docker image tagging and pushing commit6affab02dd
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 19:01:57 2024 +0300 Update build.yaml and go.mod files\ Update user authentication logic commita9610c054f
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 18:00:34 2024 +0300 Update build.yaml with Dockerfile and go installation commit7344ec2288
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 17:51:31 2024 +0300 actions?? commit5f0f91adcf
Author: Alex Borisov <79996669747@ya.ru> Date: Thu Feb 1 17:47:50 2024 +0300 Simple API to access files using link
131 lines
3.2 KiB
Go
131 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/gorilla/mux"
|
|
"github.com/joho/godotenv"
|
|
"github.com/minio/minio-go"
|
|
)
|
|
|
|
func main() {
|
|
// Get environment variables
|
|
accessKeyID, secretAccessKey, bucketName, endpoint, port := getEnv()
|
|
|
|
// Initialize MinIO client
|
|
minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, true)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Create a new router
|
|
router := mux.NewRouter()
|
|
|
|
// Define the file handler
|
|
router.HandleFunc("/{path:.*}", func(w http.ResponseWriter, r *http.Request) {
|
|
log.Printf("Received request: %s %s", r.Method, r.RequestURI)
|
|
vars := mux.Vars(r)
|
|
path := vars["path"]
|
|
|
|
log.Print("Path: ", path)
|
|
|
|
// Get the file from S3
|
|
object, err := minioClient.GetObject(bucketName, path, minio.GetObjectOptions{})
|
|
if err != nil {
|
|
if minio.ToErrorResponse(err).Code == "NoSuchKey" {
|
|
log.Printf("Object not found in S3: %s", err)
|
|
http.Error(w, "Object not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
log.Printf("Error getting object from S3: %s", err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
// Get the object info
|
|
info, err := object.Stat()
|
|
if err != nil {
|
|
if minio.ToErrorResponse(err).Code == "NoSuchKey" {
|
|
log.Printf("Object not found in S3: %s", err)
|
|
http.Error(w, "Object not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
log.Printf("Error getting object info: %s", err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
// Set the content type header
|
|
contentType := info.ContentType
|
|
w.Header().Set("Content-Type", contentType)
|
|
|
|
// Copy the file to the response writer
|
|
_, err = io.Copy(w, object)
|
|
if err != nil {
|
|
log.Printf("Error copying file to response writer: %s", err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}).Methods("GET")
|
|
|
|
router.Use(loggingMiddleware)
|
|
|
|
// Start the server
|
|
|
|
if port == "" {
|
|
port = "8080"
|
|
}
|
|
addr := fmt.Sprintf(":%s", port)
|
|
log.Printf("Server listening on %s", addr)
|
|
log.Fatal(http.ListenAndServe(addr, router))
|
|
}
|
|
|
|
func getEnv() (string, string, string, string, string) {
|
|
if os.Getenv("ENVIRONMENT") != "production" {
|
|
err := godotenv.Load()
|
|
if err != nil {
|
|
log.Fatal("Error loading .env file")
|
|
}
|
|
}
|
|
|
|
// Get environment variables
|
|
port := os.Getenv("PORT")
|
|
accessKeyID := os.Getenv("S3_ACCESS_KEY_ID")
|
|
secretAccessKey := os.Getenv("S3_SECRET_ACCESS_KEY")
|
|
bucketName := os.Getenv("S3_BUCKET_NAME")
|
|
endpoint := os.Getenv("S3_ENDPOINT_URL")
|
|
|
|
return accessKeyID, secretAccessKey, bucketName, endpoint, port
|
|
}
|
|
|
|
func loggingMiddleware(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
startTime := time.Now()
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
duration := time.Since(startTime)
|
|
log.Printf("Processed request: %s %s. Duration: %v\n", r.Method, r.RequestURI, duration)
|
|
})
|
|
}
|
|
|
|
func GetObjectList(bucketName string, minioClient *minio.Client) ([]string, error) {
|
|
var objectList []string
|
|
|
|
// Get the list of objects
|
|
objectCh := minioClient.ListObjects(bucketName, "", true, nil)
|
|
for object := range objectCh {
|
|
if object.Err != nil {
|
|
return nil, object.Err
|
|
}
|
|
objectList = append(objectList, object.Key)
|
|
}
|
|
|
|
return objectList, nil
|
|
}
|