// Run the application, start http and scp server. func Run(appConfig Config) { config = appConfig // Connect to DB db, err := lib.Connect() if err != nil { fmt.Printf("Failed to connect to db") return } // Logging log := logrus.New() log.Level = logrus.DebugLevel log.Out = os.Stdout log.Formatter = &logrus.TextFormatter{} // Websockets ws := ws.NewServer() // Shared dependencies between all controller deps := dependencies.Dependencies{ Fs: afero.NewOsFs(), Logger: log, DB: db, WS: ws, } ws.Dependencies = &deps go ws.Start() // // Start SCP // scp := scp.Server{} // scp.DB = deps.DB // scp.Logger = deps.Logger // scp.CertPath = "certs/scp.rsa" // scp.BindAddr = config.SCPBindAddr // go scp.ListenAndServe() if config.Secure { c := autocert.DirCache("certs") m := autocert.Manager{ Cache: c, Prompt: autocert.AcceptTOS, HostPolicy: autocert.HostWhitelist("x.zqz.ca"), } s := &http.Server{ Addr: config.HTTPBindAddr, TLSConfig: &tls.Config{GetCertificate: m.GetCertificate}, } deps.Info("Listening for HTTP1.1 Connections", "addr", ":3001") deps.Info("Listening for HTTP2 Connections", "addr", config.HTTPBindAddr) go http.ListenAndServe(":3001", secureRedirect()) s.ListenAndServeTLS("", "") } else { deps.Info("Listening for HTTP1.1 Connections", "addr", config.HTTPBindAddr) http.ListenAndServe(config.HTTPBindAddr, Routes(deps)) } }
// CompleteFile builds the file from chunks and then generates thumbnails func CompleteFile(deps dependencies.Dependencies, f *models.File) error { deps.Info("Processing File", "name", f.Name, "id", f.ID) tx, err := deps.DB.Begin() if err != nil { deps.Error("Failed to create transaction") return err } if err = f.Reload(tx); err != nil { tx.Rollback() return errors.Wrap(err, "Failed to reload the file") } if f.State == lib.FileProcessing { tx.Rollback() return errors.Wrap(err, "This file is already being processed") } // Delete all thumbnails err = models.Thumbnails(tx, qm.Where("file_id=?", f.ID)).DeleteAll() if err != nil { deps.Info("No previous thumnails") } f.State = lib.FileProcessing if err = f.Update(tx, "state"); err != nil { tx.Rollback() return errors.Wrap(err, "Failed to update state") } reader, err := BuildFile(deps, f) if err != nil { tx.Rollback() return errors.Wrap(err, "Failed to complete building file") } thumbHash, thumbSize, err := CreateThumbnail(deps, reader) if err != nil { tx.Rollback() return errors.Wrap(err, "Failed to create thumbnail") } if len(thumbHash) == 0 { tx.Rollback() return errors.Wrap(err, "No thumbnail created") } t := models.Thumbnail{ Hash: thumbHash, Size: thumbSize, FileID: f.ID, } if err = t.Insert(tx); err != nil { tx.Rollback() return errors.Wrap(err, "Failed to insert Thumbnail") } f.State = lib.FileFinished if err = f.Update(tx, "state"); err != nil { tx.Rollback() return errors.Wrap(err, "Failed to set state") } if err = tx.Commit(); err != nil { return errors.Wrap(err, "Failed to commit transaction") } if err = Cleanup(deps, f); err != nil { return errors.Wrap(err, "Failed to cleanup file") } deps.Info("Processed File", "name", f.Name, "id", f.ID) return nil }