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
Branches
No related tags found
No related merge requests found
Pipeline #2798 failed
File moved
File moved
File moved
File moved
File moved
File moved
File moved
......@@ -5,9 +5,11 @@ import (
"database/sql"
"encoding/json"
"fmt"
"github.com/go-redis/redis/v8"
"git.kuschku.de/justjanne/imghost-frontend/shared"
"github.com/hibiken/asynq"
"html/template"
"net/http"
"strings"
"time"
)
......@@ -29,8 +31,9 @@ func (info UserInfo) HasRole(role string) bool {
type PageContext struct {
Context context.Context
Config *Config
Redis *redis.Client
Config *shared.Config
Async *asynq.Client
UploadTimeout time.Duration
Database *sql.DB
Images http.Handler
AssetServer http.Handler
......@@ -52,20 +55,12 @@ type Album struct {
}
func parseUser(r *http.Request) UserInfo {
/*
return UserInfo{
r.Header.Get("X-Auth-Subject"),
r.Header.Get("X-Auth-Username"),
r.Header.Get("X-Auth-Email"),
strings.Split(r.Header.Get("X-Auth-Roles"), ","),
}
*/
return UserInfo{
"ad45284c-be4d-4546-8171-41cf126ac091",
"justJanne",
"janne@kuschku.de",
[]string{"imghost:user", "imghost:admin"},
}
}
func returnJson(w http.ResponseWriter, data interface{}) error {
......@@ -110,3 +105,28 @@ func formatTemplate(w http.ResponseWriter, templateName string, data interface{}
return nil
}
func waitOnTask(info *asynq.TaskInfo, timeout time.Duration) error {
total := time.Duration(0)
for total < timeout && info.State != asynq.TaskStateCompleted {
for info.State == asynq.TaskStateScheduled {
duration := info.NextProcessAt.Sub(time.Now())
total += duration
if total < timeout {
time.Sleep(duration)
}
}
if info.State != asynq.TaskStateCompleted {
duration := time.Duration(1 * time.Second)
total += duration
if total < timeout {
time.Sleep(duration)
}
}
}
if info.State == asynq.TaskStateCompleted {
return nil
} else {
return fmt.Errorf("timed out waiting on task: %s (%s)", info.Type, info.ID)
}
}
......@@ -4,5 +4,14 @@ go 1.15
require (
github.com/go-redis/redis/v8 v8.11.5
github.com/google/uuid v1.3.0 // indirect
github.com/hibiken/asynq v0.23.0 // indirect
github.com/justjanne/imgconv v1.4.1
github.com/lib/pq v1.10.4
github.com/prometheus/client_golang v1.11.1
github.com/spf13/cast v1.5.0 // indirect
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 // indirect
golang.org/x/time v0.0.0-20220411224347-583f2d630306 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/gographics/imagick.v3 v3.4.0
)
This diff is collapsed.
package shared
import (
"github.com/hibiken/asynq"
"github.com/justjanne/imgconv"
"gopkg.in/yaml.v2"
"log"
"os"
"time"
)
type SizeDefinition struct {
Size imgconv.Size `yaml:"size"`
Suffix string `yaml:"suffix"`
}
type RedisConfig struct {
Address string `yaml:"address"`
Password string `yaml:"password"`
}
type DatabaseConfig struct {
Format string `yaml:"format"`
Url string `yaml:"url"`
}
type Config struct {
Sizes []SizeDefinition `yaml:"sizes"`
Quality imgconv.Quality `yaml:"quality"`
SourceFolder string `yaml:"source-folder"`
TargetFolder string `yaml:"target-folder"`
Redis RedisConfig `yaml:"redis"`
Database DatabaseConfig `yaml:"database"`
Concurrency int `yaml:"concurrency"`
UploadTimeout string `yaml:"upload-timeout"`
}
func LoadConfigFromFile(file *os.File) Config {
var config Config
if err := yaml.NewDecoder(file).Decode(&config); err != nil {
log.Fatalf("Could not load config, %s", err.Error())
}
return config
}
func (config Config) UploadTimeoutDuration() time.Duration {
duration, err := time.ParseDuration(config.UploadTimeout)
if err != nil {
log.Fatalf("Could not load config: Could not parse upload timeout, %s", err.Error())
}
return duration
}
func (config Config) AsynqOpts() asynq.RedisClientOpt {
return asynq.RedisClientOpt{
Addr: config.Redis.Address,
Password: config.Redis.Password,
}
}
package shared
import (
"encoding/json"
"github.com/hibiken/asynq"
)
const (
TypeImageResize = "image:resize"
)
type ImageTaskPayload struct {
ImageId string
}
func NewImageResizeTask(imageId string) (*asynq.Task, error) {
payload, err := json.Marshal(ImageTaskPayload{ImageId: imageId})
if err != nil {
return nil, err
}
return asynq.NewTask(TypeImageResize, payload), nil
}
package shared
import (
"time"
)
type Image struct {
Id string `json:"id"`
Title string
Description string
CreatedAt time.Time
OriginalName string
MimeType string `json:"mime_type"`
}
type Result struct {
Id string `json:"id"`
Success bool `json:"success"`
Errors []string `json:"errors"`
}
package main
import (
"encoding/json"
"os"
"time"
)
type Image struct {
Id string `json:"id"`
Title string
Description string
CreatedAt time.Time
OriginalName string
MimeType string `json:"mime_type"`
}
type Result struct {
Id string `json:"id"`
Success bool `json:"success"`
Errors []string `json:"errors"`
}
type Size struct {
Width uint `json:"width"`
Height uint `json:"height"`
Format string `json:"format"`
}
const (
sizeFormatCover = "cover"
sizeFormatContain = "contain"
)
type Quality struct {
CompressionQuality uint `json:"compression_quality"`
SamplingFactors []float64 `json:"sampling_factors"`
}
type SizeDefinition struct {
Size Size `json:"size"`
Suffix string `json:"suffix"`
}
type RedisConfig struct {
Address string
Password string
}
type DatabaseConfig struct {
Format string
Url string
}
type Config struct {
Sizes []SizeDefinition
Quality Quality
SourceFolder string
TargetFolder string
Redis RedisConfig
Database DatabaseConfig
ImageQueue string
ResultChannel string
}
func NewConfigFromEnv() Config {
config := Config{}
json.Unmarshal([]byte(os.Getenv("IK8R_SIZES")), &config.Sizes)
json.Unmarshal([]byte(os.Getenv("IK8R_QUALITY")), &config.Quality)
config.SourceFolder = os.Getenv("IK8R_SOURCE_FOLDER")
config.TargetFolder = os.Getenv("IK8R_TARGET_FOLDER")
config.Redis.Address = os.Getenv("IK8R_REDIS_ADDRESS")
config.Redis.Password = os.Getenv("IK8R_REDIS_PASSWORD")
config.ImageQueue = os.Getenv("IK8R_REDIS_IMAGE_QUEUE")
config.ResultChannel = os.Getenv("IK8R_REDIS_RESULT_CHANNEL")
config.Database.Format = os.Getenv("IK8R_DATABASE_TYPE")
config.Database.Url = os.Getenv("IK8R_DATABASE_URL")
return config
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment