current state

master
RuisPipe 2021-08-01 12:23:23 +02:00
parent af39be1b15
commit 9357d03ab2
8 changed files with 205 additions and 2 deletions

11
config.toml Normal file
View File

@ -0,0 +1,11 @@
[server]
debug = true
host = "127.0.0.1:50051"
[rabbitmq]
host = "localhost:5672"
username = "guest"
password = "guest"
[storage]
path = "./storage/"

6
go.mod
View File

@ -1,3 +1,9 @@
module git.umbach.dev/picture-storage-handler module git.umbach.dev/picture-storage-handler
go 1.16 go 1.16
require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/streadway/amqp v1.0.0 // indirect
)

11
go.sum Normal file
View File

@ -0,0 +1,11 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/streadway/amqp v1.0.0 h1:kuuDrUJFZL1QYL9hUNuCxNObNzB0bV/ZG5jV3RWAQgo=
github.com/streadway/amqp v1.0.0/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

16
main.go
View File

@ -1,7 +1,19 @@
package main package main
import "fmt" import (
"git.umbach.dev/picture-storage-handler/modules/config"
"git.umbach.dev/picture-storage-handler/modules/rabbitmq"
log "github.com/sirupsen/logrus"
)
func main() { func main() {
fmt.Println("Hello") config.LoadConfig()
cfg := &config.Cfg.Server
if cfg.Debug {
log.SetLevel(log.DebugLevel)
}
rabbitmq.Init()
} }

30
modules/config/config.go Normal file
View File

@ -0,0 +1,30 @@
package config
import "github.com/BurntSushi/toml"
var Cfg Config
type Config struct {
Server server
RabbitMq rabbitmq
Storage storage
}
type server struct {
Debug bool
Host string
}
type rabbitmq struct {
Host string
Username string
Password string
}
type storage struct {
Path string
}
func LoadConfig() {
toml.DecodeFile("./config.toml", &Cfg)
}

View File

@ -0,0 +1,45 @@
package picture
import (
"bytes"
"image"
"image/jpeg"
"os"
"git.umbach.dev/picture-storage-handler/modules/structs"
log "github.com/sirupsen/logrus"
)
func Save(pictureMessage structs.RabbitmqPictureMessage) error {
img, _, err := image.Decode(bytes.NewReader(pictureMessage.Picture))
if err != nil {
log.Fatalln("imageDecod failed", err)
return err
}
err = os.Mkdir("./storage/"+pictureMessage.UserId, 0755)
if err != nil && !os.IsExist(err) {
log.Infoln("mkdir failed", err)
return err
}
out, err := os.Create("./storage/" + pictureMessage.UserId + "/" + pictureMessage.Filename)
if err != nil {
log.Infoln("osCreate failed", err)
return err
}
defer out.Close()
err = jpeg.Encode(out, img, nil)
if err != nil {
log.Println("jpegEncode failed", err)
return err
}
return nil
}

View File

@ -0,0 +1,81 @@
package rabbitmq
import (
"encoding/json"
"fmt"
"git.umbach.dev/picture-storage-handler/modules/config"
"git.umbach.dev/picture-storage-handler/modules/picture"
"git.umbach.dev/picture-storage-handler/modules/structs"
log "github.com/sirupsen/logrus"
"github.com/streadway/amqp"
)
var Conn *amqp.Connection
var Channel *amqp.Channel
var PictureQueue amqp.Queue
func getConnectionString() string {
cfg := &config.Cfg.RabbitMq
return fmt.Sprintf("amqp://%s:%s@%s/", cfg.Username, cfg.Password, cfg.Host)
}
func Init() {
conn, err := amqp.Dial(getConnectionString())
if err != nil {
log.Fatalln("Failed to connect to RabbitMQ", err)
}
Conn = conn
ch, err := conn.Channel()
if err != nil {
log.Fatalln("Failed to open a channel", err)
}
defer ch.Close()
Channel = ch
log.Debug("RabbitMQ connected")
msgs, err := ch.Consume(
"pictures", // queue
"", // consumer
false, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
if err != nil {
log.Fatalln("Consume err", err)
}
forever := make(chan []byte)
go func() {
for d := range msgs {
log.Infoln("Received Message:", len(d.Body))
pictureMessage := structs.RabbitmqPictureMessage{}
if err := json.Unmarshal(d.Body, &pictureMessage); err != nil {
log.Fatal(err)
}
if err := picture.Save(pictureMessage); err == nil {
d.Ack(false)
}
}
}()
fmt.Println("Successfully Connected to our RabbitMQ Instance")
fmt.Println(" [*] - Waiting for messages")
<-forever
}

View File

@ -0,0 +1,7 @@
package structs
type RabbitmqPictureMessage struct {
Picture []byte
UserId string
Filename string
}