diff --git a/glide.lock b/glide.lock index 7d7ba861fdaa0292e946ea644a6c29be77f4f9b7..eaae1d1584d1a100e09156220969ad2fe19caa1b 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ -hash: 55e100566d38affe5f77251e4ea0b07d3ebcca1480b2931c9cdc0e606398f5f3 -updated: 2018-03-16T16:44:29.59466378+01:00 +hash: a4dc37eb677d9e7f409018b840b435b3dfbd02ad229d24f42ac942d2a07116b5 +updated: 2018-03-16T23:16:25.752919561+01:00 imports: - name: github.com/go-redis/redis version: 877867d2845fbaf86798befe410b6ceb6f5c29a3 @@ -11,9 +11,8 @@ imports: - internal/proto - internal/singleflight - internal/util -- name: gopkg.in/gographics/imagick.v2 - version: 589a8ef16fd4f08a29ad26afe5c33fb5673e4efc +- name: github.com/lib/pq + version: 88edab0803230a3898347e77b474f8c1820a1f20 subpackages: - - imagick - - imagick/types + - oid testImports: [] diff --git a/glide.yaml b/glide.yaml index 255ba35984fce0a3927c1506a012a54e80e41080..e608bfd568ffc63c907952b46782931a9fab100c 100644 --- a/glide.yaml +++ b/glide.yaml @@ -2,3 +2,4 @@ package: github.com/justjanne/imghost-frontend import: - package: github.com/go-redis/redis version: ^6.10.2 +- package: github.com/lib/pq diff --git a/imghost-frontend b/imghost-frontend new file mode 100755 index 0000000000000000000000000000000000000000..331e1035ced2e2a7baab02db5ec2b470b9b43adc Binary files /dev/null and b/imghost-frontend differ diff --git a/main.go b/main.go index 4ee9d5ed6d7a8dba4aa3b7f0a59087f18aaeaed1..4e2774f11a7e3757622380ab6966de731035090c 100644 --- a/main.go +++ b/main.go @@ -87,67 +87,48 @@ func returnResult(writer http.ResponseWriter, result Result) error { return nil } -func main() { - config := Config{} +func printHeaders(r *http.Request) { + fmt.Println(r.URL) + for key, value := range r.Header { + fmt.Printf(" %s: %s\n", key, value) + } +} - 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") +func main() { + config := NewConfigFromEnv() client := redis.NewClient(&redis.Options{ Addr: config.Redis.Address, Password: config.Redis.Password, }) - http.HandleFunc("/upload", func(w http.ResponseWriter, r *http.Request) { - r.ParseMultipartForm(32 << 20) - file, _, err := r.FormFile("file") - image, err := createImage(&config, file) - if err != nil { - returnResult(w, Result{ - Id: "", - Success: false, - Errors: []string{err.Error()}, - }) - return - } + //db, err := sql.Open(config.Database.Format, config.Database.Url) + //if err != nil { + //panic(err) + //} - fmt.Printf("Created task %s at %d\n", image.Id, time.Now().Unix()) - - data, err := json.Marshal(image) - if err != nil { - returnResult(w, Result{ - Id: image.Id, - Success: false, - Errors: []string{err.Error()}, - }) - return - } - - pubsub := client.Subscribe(config.ResultChannel) - client.RPush(fmt.Sprintf("queue:%s", config.ImageQueue), data) + staticServer := http.FileServer(http.Dir("static/")) + imageServer := http.FileServer(http.Dir(config.TargetFolder)) - fmt.Printf("Submitted task %s at %d\n", image.Id, time.Now().Unix()) + http.HandleFunc("/upload/", func(w http.ResponseWriter, r *http.Request) { + printHeaders(r) - waiting := true - for waiting { - message, err := pubsub.ReceiveMessage() + if r.Method == "POST" { + r.ParseMultipartForm(32 << 20) + file, _, err := r.FormFile("file") + image, err := createImage(&config, file) if err != nil { returnResult(w, Result{ - Id: image.Id, + Id: "", Success: false, Errors: []string{err.Error()}, }) return } - result := Result{} - err = json.Unmarshal([]byte(message.Payload), &result) + fmt.Printf("Created task %s at %d\n", image.Id, time.Now().Unix()) + + data, err := json.Marshal(image) if err != nil { returnResult(w, Result{ Id: image.Id, @@ -157,22 +138,53 @@ func main() { return } - fmt.Printf("Returned task %s at %d\n", result.Id, time.Now().Unix()) - - if result.Id == image.Id { - waiting = false - - returnResult(w, result) - return + pubsub := client.Subscribe(config.ResultChannel) + client.RPush(fmt.Sprintf("queue:%s", config.ImageQueue), data) + + fmt.Printf("Submitted task %s at %d\n", image.Id, time.Now().Unix()) + + waiting := true + for waiting { + message, err := pubsub.ReceiveMessage() + if err != nil { + returnResult(w, Result{ + Id: image.Id, + Success: false, + Errors: []string{err.Error()}, + }) + return + } + + result := Result{} + err = json.Unmarshal([]byte(message.Payload), &result) + if err != nil { + returnResult(w, Result{ + Id: image.Id, + Success: false, + Errors: []string{err.Error()}, + }) + return + } + + fmt.Printf("Returned task %s at %d\n", result.Id, time.Now().Unix()) + + if result.Id == image.Id { + waiting = false + + returnResult(w, result) + return + } } + } else { + staticServer.ServeHTTP(w, r) } }) - fs := http.FileServer(http.Dir("static/")) - http.Handle("/", fs) - - imageServer := http.FileServer(http.Dir(config.TargetFolder)) http.Handle("/i/", http.StripPrefix("/i/", imageServer)) + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + printHeaders(r) + staticServer.ServeHTTP(w, r) + }) err := http.ListenAndServe(":8080", nil) if err != nil { diff --git a/static/index.html b/static/index.html index 890bcbeaef6b68da31696eab2088ad2661c133b0..8025f96dc2023271dc97f9f5b84927934da5f3f4 100644 --- a/static/index.html +++ b/static/index.html @@ -1 +1 @@ -<html><form action="/upload" method="POST" enctype="multipart/form-data"><input type="file" name="file"/><input type="submit"/></form></html> \ No newline at end of file +<html><a href="/upload/">Upload images</a></html> \ No newline at end of file diff --git a/static/index.pug b/static/index.pug index b8715557a93b073574ba9ab00d9a83305938c966..a9efe59eafde849c7e10a8ef0aa391638a26b31d 100644 --- a/static/index.pug +++ b/static/index.pug @@ -1,4 +1,3 @@ html - form(action="/upload", method="POST" enctype="multipart/form-data") - input(type="file", name="file") - input(type="submit") \ No newline at end of file + a(href="/upload/"). + Upload images \ No newline at end of file diff --git a/static/upload/index.html b/static/upload/index.html new file mode 100644 index 0000000000000000000000000000000000000000..01cb74421a0320b6f90f0d6c696796fa004e006b --- /dev/null +++ b/static/upload/index.html @@ -0,0 +1 @@ +<html><form action="/upload/" method="POST" enctype="multipart/form-data"><input type="file" name="file"/><input type="submit"/></form></html> \ No newline at end of file diff --git a/static/upload/index.pug b/static/upload/index.pug new file mode 100644 index 0000000000000000000000000000000000000000..00c790bb31b334e8d2b66307f9169faa0f192dde --- /dev/null +++ b/static/upload/index.pug @@ -0,0 +1,4 @@ +html + form(action="/upload/", method="POST" enctype="multipart/form-data") + input(type="file", name="file") + input(type="submit") \ No newline at end of file diff --git a/types.go b/types.go index cd426c05945b8b4c2d9f03babc70e14a3d29c57f..ea1f2fec44e3470c7ace23fcc383817d49c8004d 100644 --- a/types.go +++ b/types.go @@ -1,5 +1,10 @@ package main +import ( + "os" + "encoding/json" +) + type Image struct { Id string `json:"id"` MimeType string `json:"mime_type"` @@ -37,12 +42,35 @@ type RedisConfig struct { 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 +} \ No newline at end of file