-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
244 lines (214 loc) · 6.34 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main
import (
"fmt"
"io"
"io/fs"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
_ "github.com/H3rmt/FileSharing/migrations"
"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/models"
"github.com/pocketbase/pocketbase/plugins/migratecmd"
)
const (
env_host = "HOST"
env_port = "PORT"
env_name = "APP_NAME"
env_admin_email = "ADMIN_EMAIL"
env_admin_password = "ADMIN_PASSWORD"
env_user_password = "USER_PASSWORD"
)
const node_port = "4321"
const node_host = "0.0.0.0"
const username = "generated-user"
func redirect(c echo.Context) error {
// println(c.Request().RequestURI)
// Erstellt eine neue Anfrage an den Node.js-Server
req, err := http.NewRequest(c.Request().Method, fmt.Sprintf("http://%s:%s%s", node_host, node_port, c.Request().RequestURI), c.Request().Body)
if err != nil {
err = fmt.Errorf("error creating request: %w", err)
println(err.Error())
return c.String(http.StatusInternalServerError, err.Error())
}
// Kopiert die Header aus der ursprünglichen Anfrage
for name, values := range c.Request().Header {
for _, value := range values {
req.Header.Add(name, value)
}
}
// Sendet die Anfrage und erhält eine Antwort
resp, err := http.DefaultClient.Do(req)
if err != nil {
err = fmt.Errorf("error sending request: %w", err)
println(err.Error())
return c.String(http.StatusInternalServerError, err.Error())
}
defer resp.Body.Close()
// Liest den Antwortkörper und sendet ihn zurück an den Client
body, err := io.ReadAll(resp.Body)
if err != nil {
err = fmt.Errorf("error reading response: %w", err)
println(err.Error())
return c.String(http.StatusInternalServerError, err.Error())
}
return c.Blob(http.StatusOK, resp.Header.Get("Content-Type"), body)
}
func main() {
app := pocketbase.New()
var host string
var port string
// run if serve command is executed
if len(os.Args) > 1 && os.Args[1] == "serve" {
var present bool
host, present = os.LookupEnv(env_host)
if !present {
host = "0.0.0.0"
fmt.Printf("missing %s env, using '%s'\n", env_host, host)
}
port, present = os.LookupEnv(env_port)
if !present {
port = "80"
fmt.Printf("missing %s env, using '%s'\n", env_port, port)
}
os.Args = append(os.Args, fmt.Sprintf("--http=%s:%s", host, port))
}
migratecmd.MustRegister(app, app.RootCmd, migratecmd.Config{
// enable auto creation of migration files when making collection changes in the Admin UI
// (the isGoRun check is to enable it only during development)
Automigrate: strings.HasPrefix(os.Args[0], os.TempDir()),
})
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
// start node server
cmd := exec.Command("node", "./dist/server/entry.mjs")
cmd.Env = append(os.Environ(), fmt.Sprintf("HOST=%s", node_host), fmt.Sprintf("PORT=%s", node_port), fmt.Sprintf("POCKETBASE_URL=http://%s:%s", host, port))
cmd.Stdout = os.Stdout // Leitet die Standardausgabe des Node.js-Servers in die Standardausgabe des Go-Programms um
cmd.Stderr = os.Stderr // Leitet die Standardfehler des Node.js-Servers in die Standardfehler des Go-Programms um
err := cmd.Start()
if err != nil {
log.Fatal(err)
}
log.Printf("Node server started with PID %d\n", cmd.Process.Pid)
go func() {
err := cmd.Wait()
if err != nil {
log.Printf("Node server terminated with error: %v\n", err)
} else {
log.Println("Node server terminated successfully")
}
}()
e.Router.GET("/*", redirect)
e.Router.GET("/info", func(c echo.Context) error {
return c.File("info.json")
})
e.Router.GET("/api/name", func(c echo.Context) error {
name, present := os.LookupEnv(env_name)
if !present {
name = "File Sharing"
}
Jname := struct {
Name string `json:"name"`
}{
Name: name,
}
return c.JSON(http.StatusOK, Jname)
}, apis.ActivityLogger(app))
e.Router.GET("/api/size", func(c echo.Context) error {
var totalSize int64
filepath.WalkDir("pb_data/storage", func(path string, d fs.DirEntry, err error) error {
if d != nil && !d.IsDir() {
info, err := d.Info()
if err != nil {
log.Print(err)
return c.String(http.StatusInternalServerError, "")
}
if !strings.HasSuffix(info.Name(), ".attrs") {
totalSize += info.Size()
}
}
return nil
})
Jsize := struct {
Size int64 `json:"size"`
}{
Size: totalSize,
}
return c.JSON(http.StatusOK, Jsize)
}, apis.ActivityLogger(app), apis.RequireAdminOrRecordAuth())
return nil
})
// app.OnAfterBootstrap().Add(func(e *core.BootstrapEvent) error {
// migrations were not applied yet
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
// create admin
admin_email, present := os.LookupEnv(env_admin_email)
if !present {
admin_email = "[email protected]"
fmt.Printf("missing %s env, using '%s'\n", env_admin_email, admin_email)
}
admin_passwd, present := os.LookupEnv(env_admin_password)
if !present {
return fmt.Errorf("%s env not set", env_admin_password)
}
user_passwd, present := os.LookupEnv(env_user_password)
if !present {
return fmt.Errorf("%s env not set", env_user_password)
}
old_admin, _ := e.App.Dao().FindAdminByEmail(admin_email)
if old_admin == nil {
// create new admin
admin := &models.Admin{}
admin.Email = admin_email
admin.SetPassword(admin_passwd)
err := e.App.Dao().SaveAdmin(admin)
if err != nil {
return err
} else {
fmt.Println("created admin")
}
} else {
old_admin.SetPassword(admin_passwd)
err := e.App.Dao().SaveAdmin(old_admin)
if err != nil {
return err
} else {
fmt.Println("updated admin")
}
}
// create user
old_user, _ := e.App.Dao().FindAuthRecordByUsername("users", username)
if old_user == nil {
collection, err := e.App.Dao().FindCollectionByNameOrId("users")
if err != nil {
return err
}
user := models.NewRecord(collection)
user.SetUsername(username)
user.SetPassword(user_passwd)
err = e.App.Dao().SaveRecord(user)
if err != nil {
return err
} else {
fmt.Println("created user")
}
} else {
old_user.SetPassword(user_passwd)
err := e.App.Dao().SaveRecord(old_user)
if err != nil {
return err
} else {
fmt.Println("updated user")
}
}
return nil
})
if err := app.Start(); err != nil {
log.Fatal(err)
}
}