-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
116 lines (94 loc) · 2.96 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"context"
"fmt"
"io"
"log"
"net/http"
"os"
"msgraph-converter/config"
"msgraph-converter/graph"
"github.com/gin-gonic/gin"
)
// getPort returns the port from environment variable or default value
func getPort() string {
port := os.Getenv("SERVICE_PORT")
if port == "" {
// Логируем использование дефолтного порта
log.Println("Warning: SERVICE_PORT not set, using default port 8181")
return "8181"
}
return port
}
func main() {
// Загружаем конфигурацию
cfg := config.LoadConfig()
// Инициализируем клиента Microsoft Graph
client := graph.NewGraphClient(cfg.AzureClientID, cfg.AzureClientSecret, cfg.AzureTenantID)
r := gin.Default()
r.POST("/convert", func(c *gin.Context) {
file, err := c.FormFile("file")
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "No file uploaded"})
return
}
localPath := "./" + file.Filename
if err := c.SaveUploadedFile(file, localPath); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save file"})
return
}
defer os.Remove(localPath) // Удаляем временный файл после обработки
ctx := context.Background()
// Создаем или находим папку
folderID, err := client.GetOrCreateFolder(ctx, cfg.DriveId, cfg.UploadDirectory)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
log.Printf("Folder ID: %s", folderID)
// Загружаем файл в OneDrive
fileID, err := client.UploadFile(ctx, cfg.DriveId, localPath, file.Filename, folderID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Конвертируем в PDF
pdfReader, err := client.ConvertToPDF(ctx, cfg.DriveId, fileID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
defer pdfReader.Close()
// Отправляем PDF в ответе
outputPath := "output.pdf"
outFile, err := os.Create(outputPath)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create output file"})
return
}
defer outFile.Close()
defer os.Remove(outputPath) // Удаляем PDF после отправки
if _, err := io.Copy(outFile, pdfReader); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save PDF"})
return
}
c.File(outputPath)
})
r.GET("/drives", func(c *gin.Context) {
ctx := context.Background()
drives, err := client.ListDrives(ctx)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"drives": drives,
})
})
port := getPort()
serverAddr := fmt.Sprintf("0.0.0.0:%s", port)
log.Printf("Starting server on port %s", port)
if err := r.Run(serverAddr); err != nil {
log.Fatalf("Failed to start server: %v", err)
}
}