-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
194 lines (160 loc) · 4.55 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/base64"
"encoding/pem"
"fmt"
"log"
"math/big"
"net"
"net/http"
"os"
"os/exec"
"runtime"
"time"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
// ExecuteCommand takes a command as a string and executes it
func sys(command string) (string, error) {
// Determine the shell based on the OS
var shell, flag string
if runtime.GOOS == "windows" {
shell = "cmd"
flag = "/C"
} else {
shell = "sh"
flag = "-c"
}
// Execute the command using the shell
cmd := exec.Command(shell, flag, command)
outputBytes, err := cmd.CombinedOutput()
return string(outputBytes), err
}
// Get key from the env file
func env(key string) string {
// load .env file
err := godotenv.Load("../.env")
if err != nil {
log.Fatalf("Error loading .env file")
}
return os.Getenv(key)
}
func generateSalt(length int) (string, error) {
salt := make([]byte, length)
_, err := rand.Read(salt)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(salt), nil
}
// Returns error code and ends handler function for gin routes
func abortWithError(statusCode int, err error, c *gin.Context) {
_ = c.AbortWithError(statusCode, err)
c.JSON(statusCode, gin.H{"status": fmt.Sprint(err)})
}
func generateSSL() {
// Generate a private key
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
log.Fatal("Error generating private key:", err)
return
}
// Generate a self-signed certificate
certTemplate := x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{CommonName: "localhost"},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(10, 0, 0),
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
IsCA: true,
IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
}
derBytes, err := x509.CreateCertificate(rand.Reader, &certTemplate, &certTemplate, &privateKey.PublicKey, privateKey)
if err != nil {
log.Fatal("Error creating certificate:", err)
return
}
// Write the private key and certificate to files
keyOut, err := os.Create("./private.key")
if err != nil {
log.Fatal("Error creating private key file:", err)
return
}
defer keyOut.Close()
err = pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)})
if err != nil {
log.Fatal("Error creating certificate file: ", err)
return
}
certOut, err := os.Create("./cert.pem")
if err != nil {
log.Fatal("Error creating certificate file: ", err)
return
}
defer certOut.Close()
err = pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
if err != nil {
log.Fatal("Error creating certificate file: ", err)
return
}
fmt.Println("TLS certificate and private key generated successfully.")
}
func fileExists(filename string) bool {
_, err := os.Stat(filename)
if err == nil {
return true // File exists
}
if os.IsNotExist(err) {
return false // File does not exist
}
return false // Error occurred (e.g., permission denied)
}
func main() {
protocol := "https"
port := "8080"
//Ensure valid protocol env entry
if protocol != "http" && protocol != "https" {
log.Fatal("Invalid protocol. Must be HTTP or HTTPS")
}
//Generate TLS keys if they do not already exist
if !(fileExists("./cert.pem") && fileExists("./private.key")) && protocol == "https" {
generateSSL()
}
// Initialize Gin
gin.SetMode(gin.ReleaseMode) // Turn off debugging mode
r := gin.Default() // Initialize Gin
//Route for health check
r.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
})
r.GET("/", func(c *gin.Context) {
c.Data(http.StatusOK, "text/plain", []byte("You have reached the main page of dist_encode testing"))
})
output, err := sys("ffmpeg -i input.mp4 -c:v libx265 -crf 28 -preset slow output.mp4\n")
if err != nil {
print(err.Error())
}
fmt.Println(output)
fmt.Printf("Listening for %v on port %v...\n", protocol, port) //Notifies that server is running on X port
if protocol == "http" { //Start running the Gin server
err = r.Run(":" + port)
if err != nil {
fmt.Println(err)
}
} else if protocol == "https" {
err = r.RunTLS(":"+port, "./cert.pem", "./private.key")
if err != nil {
fmt.Println(err)
}
} else {
log.Fatal("Something went wrong starting the Gin server")
}
}