Skip to content
Snippets Groups Projects
Verified Commit ee0b3c6c authored by Janne Mareike Koschinski's avatar Janne Mareike Koschinski
Browse files

feat: merge frontend and backend into one project

parent 35d1ac71
No related branches found
No related tags found
No related merge requests found
Pipeline #2798 failed
Showing
with 34 additions and 40 deletions
File moved
File moved
File moved
File moved
......@@ -3,13 +3,20 @@ package main
import (
"context"
"database/sql"
"github.com/go-redis/redis/v8"
"git.kuschku.de/justjanne/imghost-frontend/shared"
"github.com/hibiken/asynq"
_ "github.com/lib/pq"
"log"
"net/http"
"os"
)
func main() {
config := NewConfigFromEnv()
configFile, err := os.Open("config.yaml")
if err != nil {
log.Fatalf("Could not open config file: %s", err.Error())
}
config := shared.LoadConfigFromFile(configFile)
db, err := sql.Open(config.Database.Format, config.Database.Url)
if err != nil {
......@@ -19,10 +26,8 @@ func main() {
pageContext := PageContext{
context.Background(),
&config,
redis.NewClient(&redis.Options{
Addr: config.Redis.Address,
Password: config.Redis.Password,
}),
asynq.NewClient(config.AsynqOpts()),
config.UploadTimeoutDuration(),
db,
http.FileServer(http.Dir(config.TargetFolder)),
http.FileServer(http.Dir("assets")),
......
File moved
File moved
package main
import (
"net/http"
"fmt"
"net/http"
"path"
)
......
File moved
File moved
File moved
File moved
......@@ -5,6 +5,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"git.kuschku.de/justjanne/imghost-frontend/shared"
"io"
"mime/multipart"
"net/http"
......@@ -49,7 +50,7 @@ func writeBody(reader io.ReadCloser, path string) error {
return out.Close()
}
func createImage(config *Config, body io.ReadCloser, fileHeader *multipart.FileHeader) (Image, error) {
func createImage(config *shared.Config, body io.ReadCloser, fileHeader *multipart.FileHeader) (Image, error) {
id := generateId()
path := filepath.Join(config.SourceFolder, id)
......@@ -94,47 +95,35 @@ func pageUpload(ctx PageContext) http.Handler {
return
}
pubsub := ctx.Redis.Subscribe(ctx.Context, ctx.Config.ResultChannel)
if _, err = ctx.Database.Exec("INSERT INTO images (id, owner, created_at, updated_at, original_name, type) VALUES ($1, $2, $3, $4, $5, $6)", image.Id, user.Id, image.CreatedAt, image.CreatedAt, image.OriginalName, image.MimeType); err != nil {
formatError(w, ErrorData{500, user, r.URL, err}, "json")
return
}
data, err := json.Marshal(image)
if err != nil {
formatError(w, ErrorData{500, user, r.URL, err}, "json")
return
}
fmt.Printf("Created task %s at %d\n", image.Id, time.Now().Unix())
ctx.Redis.RPush(ctx.Context, fmt.Sprintf("queue:%s", ctx.Config.ImageQueue), data)
t, err := shared.NewImageResizeTask(image.Id)
fmt.Printf("Submitted task %s at %d\n", image.Id, time.Now().Unix())
waiting := true
for waiting {
message, err := pubsub.ReceiveMessage(ctx.Context)
if err != nil {
formatError(w, ErrorData{500, user, r.URL, err}, "json")
return
}
result := Result{}
err = json.Unmarshal([]byte(message.Payload), &result)
info, err := ctx.Async.Enqueue(t)
if err != nil {
formatError(w, ErrorData{500, user, r.URL, err}, "json")
return
}
fmt.Printf("Returned task %s at %d\n", result.Id, time.Now().Unix())
if result.Id == image.Id {
waiting = false
if err = returnJson(w, result); err != nil {
if err := waitOnTask(info, ctx.UploadTimeout); err != nil {
formatError(w, ErrorData{500, user, r.URL, err}, "json")
return
}
var result Result
if err := json.Unmarshal(info.Result, &result); err != nil {
formatError(w, ErrorData{500, user, r.URL, err}, "json")
return
}
if err = returnJson(w, result); err != nil {
formatError(w, ErrorData{500, user, r.URL, err}, "json")
return
}
return
} else {
......
File moved
File moved
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment