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

Significant cleanup and improvements

parent f7fc2e5d
No related branches found
No related tags found
1 merge request!1Replace entire project structure
Pipeline #2571 failed
Showing
with 157 additions and 37 deletions
...@@ -8,7 +8,7 @@ import ( ...@@ -8,7 +8,7 @@ import (
"net/http" "net/http"
) )
func GetAlbum(env environment.Environment) http.Handler { func GetAlbum(env environment.FrontendEnvironment) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request) vars := mux.Vars(request)
album, err := env.Repositories.Albums.Get(vars["albumId"]) album, err := env.Repositories.Albums.Get(vars["albumId"])
......
...@@ -8,7 +8,7 @@ import ( ...@@ -8,7 +8,7 @@ import (
"net/http" "net/http"
) )
func ListAlbums(env environment.Environment) http.Handler { func ListAlbums(env environment.FrontendEnvironment) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
user, err := auth.ParseUser(request, env) user, err := auth.ParseUser(request, env)
if err != nil { if err != nil {
......
...@@ -8,7 +8,7 @@ import ( ...@@ -8,7 +8,7 @@ import (
"net/http" "net/http"
) )
func GetAlbumImage(env environment.Environment) http.Handler { func GetAlbumImage(env environment.FrontendEnvironment) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request) vars := mux.Vars(request)
albumImage, err := env.Repositories.AlbumImages.Get(vars["albumId"], vars["imageId"]) albumImage, err := env.Repositories.AlbumImages.Get(vars["albumId"], vars["imageId"])
......
...@@ -8,7 +8,7 @@ import ( ...@@ -8,7 +8,7 @@ import (
"net/http" "net/http"
) )
func ListAlbumImages(env environment.Environment) http.Handler { func ListAlbumImages(env environment.FrontendEnvironment) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request) vars := mux.Vars(request)
albumImages, err := env.Repositories.AlbumImages.List(vars["albumId"]) albumImages, err := env.Repositories.AlbumImages.List(vars["albumId"])
......
...@@ -8,7 +8,7 @@ import ( ...@@ -8,7 +8,7 @@ import (
"net/http" "net/http"
) )
func GetImage(env environment.Environment) http.Handler { func GetImage(env environment.FrontendEnvironment) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request) vars := mux.Vars(request)
image, err := env.Repositories.Images.Get(vars["imageId"]) image, err := env.Repositories.Images.Get(vars["imageId"])
......
...@@ -8,7 +8,7 @@ import ( ...@@ -8,7 +8,7 @@ import (
"net/http" "net/http"
) )
func ListImages(env environment.Environment) http.Handler { func ListImages(env environment.FrontendEnvironment) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
user, err := auth.ParseUser(request, env) user, err := auth.ParseUser(request, env)
if err != nil { if err != nil {
......
package api
import (
"context"
"git.kuschku.de/justjanne/imghost-frontend/auth"
"git.kuschku.de/justjanne/imghost-frontend/environment"
"git.kuschku.de/justjanne/imghost-frontend/model"
"git.kuschku.de/justjanne/imghost-frontend/util"
"net/http"
)
func UploadImage(env environment.FrontendEnvironment) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
user, err := auth.ParseUser(request, env)
if err != nil {
http.Error(writer, err.Error(), http.StatusUnauthorized)
return
}
var image model.Image
image.Id = "testid"
image.Owner = user.Id
err = env.Repositories.Images.Create(image)
if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
err = env.Storage.Upload(
context.Background(),
env.Configuration.Storage.ConversionBucket,
image.Id,
request.Body)
if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
util.ReturnJson(writer, image)
})
}
...@@ -6,7 +6,7 @@ import ( ...@@ -6,7 +6,7 @@ import (
"net/http" "net/http"
) )
func ParseUser(request *http.Request, env environment.Environment) (user model.User, err error) { func ParseUser(request *http.Request, env environment.FrontendEnvironment) (user model.User, err error) {
// TODO: Implement actual user auth // TODO: Implement actual user auth
user = model.User{ user = model.User{
Id: "ad45284c-be4d-4546-8171-41cf126ac091", Id: "ad45284c-be4d-4546-8171-41cf126ac091",
......
...@@ -12,7 +12,7 @@ import ( ...@@ -12,7 +12,7 @@ import (
) )
func main() { func main() {
var config configuration.Configuration var config configuration.BackendConfiguration
configFile, err := os.Open("config.yaml") configFile, err := os.Open("config.yaml")
if err != nil { if err != nil {
panic(err) panic(err)
...@@ -22,14 +22,14 @@ func main() { ...@@ -22,14 +22,14 @@ func main() {
panic(err) panic(err)
} }
env, err := environment.NewServerEnvironment(config) env, err := environment.NewBackendEnvironment(config)
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer env.Destroy() defer env.Destroy()
mux := asynq.NewServeMux() mux := asynq.NewServeMux()
mux.HandleFunc(config.Conversion.ResizeTaskId, task.HandleImageResizeTask) mux.Handle(config.Conversion.TaskId, task.NewImageProcessor(env))
if err := env.QueueServer.Run(mux); err != nil { if err := env.QueueServer.Run(mux); err != nil {
log.Fatalf("could not run server: %v", err) log.Fatalf("could not run server: %v", err)
} }
......
...@@ -13,7 +13,7 @@ import ( ...@@ -13,7 +13,7 @@ import (
) )
func main() { func main() {
var config configuration.Configuration var config configuration.FrontendConfiguration
configFile, err := os.Open("config.yaml") configFile, err := os.Open("config.yaml")
if err != nil { if err != nil {
panic(err) panic(err)
...@@ -23,7 +23,7 @@ func main() { ...@@ -23,7 +23,7 @@ func main() {
panic(err) panic(err)
} }
env, err := environment.NewClientEnvironment(config) env, err := environment.NewFrontendEnvironment(config)
if err != nil { if err != nil {
panic(err) panic(err)
} }
...@@ -34,6 +34,9 @@ func main() { ...@@ -34,6 +34,9 @@ func main() {
router.Handle( router.Handle(
"/api/v1/images", "/api/v1/images",
api.ListImages(env)).Methods(http.MethodGet) api.ListImages(env)).Methods(http.MethodGet)
router.Handle(
"/api/v1/images",
api.UploadImage(env)).Methods(http.MethodPost)
router.Handle( router.Handle(
"/api/v1/images/{imageId}", "/api/v1/images/{imageId}",
api.GetImage(env)).Methods(http.MethodGet) api.GetImage(env)).Methods(http.MethodGet)
......
package configuration package configuration
type AuthConfiguration struct { type AuthConfiguration struct {
RolePrefix string `json:"role_prefix"` RolePrefix string `json:"role_prefix" yaml:"role-prefix"`
} }
package configuration
type BackendConfiguration struct {
Queue QueueConfiguration `json:"queue" yaml:"queue"`
Database DatabaseConfiguration `json:"database" yaml:"database"`
Redis RedisConfiguration `json:"redis" yaml:"redis"`
Conversion ConversionConfiguration `json:"conversion" yaml:"conversion"`
Storage StorageConfiguration `json:"storage" yaml:"storage"`
}
package configuration
type Configuration struct {
Queue QueueConfiguration `json:"queue"`
Database DatabaseConfiguration `json:"database"`
Redis RedisConfiguration `json:"redis"`
Conversion ConversionConfiguration `json:"conversion"`
Auth AuthConfiguration `json:"auth"`
}
...@@ -6,11 +6,11 @@ import ( ...@@ -6,11 +6,11 @@ import (
) )
type ConversionConfiguration struct { type ConversionConfiguration struct {
TaskId string `json:"task_id"` TaskId string `json:"task_id" yaml:"task-id"`
MaxRetry int `json:"max_retry"` MaxRetry int `json:"max_retry" yaml:"max-retry"`
Timeout types.Timeout `json:"timeout"` Timeout types.Timeout `json:"timeout" yaml:"timeout"`
Queue string `json:"queue"` Queue string `json:"queue" yaml:"queue"`
UniqueTimeout types.Timeout `json:"unique_timeout"` UniqueTimeout types.Timeout `json:"unique_timeout" yaml:"unique-timeout"`
Quality imgconv.Quality `json:"quality"` Quality imgconv.Quality `json:"quality" yaml:"quality"`
Sizes []imgconv.Size `json:"sizes"` Sizes []imgconv.Size `json:"sizes" yaml:"sizes"`
} }
package configuration package configuration
type DatabaseConfiguration struct { type DatabaseConfiguration struct {
Type string `json:"type"` Type string `json:"type" yaml:"type"`
Url string `json:"url"` Url string `json:"url" yaml:"url"`
} }
package configuration
type FrontendConfiguration struct {
Queue QueueConfiguration `json:"queue" yaml:"queue"`
Database DatabaseConfiguration `json:"database" yaml:"database"`
Redis RedisConfiguration `json:"redis" yaml:"redis"`
Conversion ConversionConfiguration `json:"conversion" yaml:"conversion"`
Storage StorageConfiguration `json:"storage" yaml:"storage"`
Auth AuthConfiguration `json:"auth" yaml:"auth"`
}
...@@ -3,8 +3,8 @@ package configuration ...@@ -3,8 +3,8 @@ package configuration
import "git.kuschku.de/justjanne/imghost-frontend/configuration/types" import "git.kuschku.de/justjanne/imghost-frontend/configuration/types"
type QueueConfiguration struct { type QueueConfiguration struct {
Concurrency int `json:"concurrency"` Concurrency int `json:"concurrency" yaml:"concurrency"`
LogLevel types.Severity `json:"log_level"` LogLevel types.Severity `json:"log_level" yaml:"log-level"`
StrictPriority bool `json:"strict_priority"` StrictPriority bool `json:"strict_priority" yaml:"strict-priority"`
Queues map[string]int `json:"queues"` Queues map[string]int `json:"queues" yaml:"queues"`
} }
package configuration package configuration
type RedisConfiguration struct { type RedisConfiguration struct {
Address string `json:"address"` Address string `json:"address" yaml:"address"`
Password string `json:"password"` Password string `json:"password" yaml:"password"`
} }
package configuration
type StorageConfiguration struct {
Endpoint string `json:"endpoint" yaml:"endpoint"`
Secure bool `json:"secure" yaml:"secure"`
ImageBucket string `json:"image_bucket" yaml:"image-bucket"`
ConversionBucket string `json:"conversion_bucket" yaml:"conversion-bucket"`
AccessKey string `json:"access_key" yaml:"access-key"`
SecretKey string `json:"secret_key" yaml:"secret-key"`
}
package environment
import (
"git.kuschku.de/justjanne/imghost-frontend/configuration"
"git.kuschku.de/justjanne/imghost-frontend/repo"
"git.kuschku.de/justjanne/imghost-frontend/storage"
"github.com/hibiken/asynq"
"github.com/jmoiron/sqlx"
)
type BackendEnvironment struct {
Configuration configuration.BackendConfiguration
QueueServer *asynq.Server
Database *sqlx.DB
Repositories Repositories
Storage storage.Storage
}
func NewBackendEnvironment(config configuration.BackendConfiguration) (env BackendEnvironment, err error) {
env.Configuration = config
if env.Database, err = sqlx.Open(config.Database.Type, config.Database.Url); err != nil {
return
}
if env.Repositories.Images, err = repo.NewImageRepo(env.Database); err != nil {
return
}
if env.Repositories.Albums, err = repo.NewAlbumRepo(env.Database); err != nil {
return
}
if env.Repositories.AlbumImages, err = repo.NewAlbumImageRepo(env.Database); err != nil {
return
}
if env.Storage, err = storage.NewStorage(env.Configuration.Storage); err != nil {
return
}
env.QueueServer = asynq.NewServer(
asynq.RedisClientOpt{
Addr: config.Redis.Address,
Password: config.Redis.Password,
},
asynq.Config{
Concurrency: config.Queue.Concurrency,
LogLevel: asynq.LogLevel(config.Queue.LogLevel),
Queues: config.Queue.Queues,
StrictPriority: config.Queue.StrictPriority,
},
)
return env, err
}
func (env BackendEnvironment) Destroy() error {
if err := env.Database.Close(); err != nil {
return err
}
env.QueueServer.Shutdown()
return nil
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment