iris icon indicating copy to clipboard operation
iris copied to clipboard

Load templates from S3

Open spolischook opened this issue 2 years ago • 8 comments

Is your feature request related to a problem? Please describe. As far as I understand there are two ways of getting templates works:

  • put templates at the same location near executable as they were near main.go
  • pack all templates into an executable with go-bindata

Describe the solution you'd like I would like to use S3 storage for templates

Describe alternatives you've considered It seems that locales are able to load from function, e.g. LoadAssets, but not a templates

Additional context I don't think it's a good idea to have a big executable with all of my HTML templates (email templates - there are a lot) Also, don't like the idea to carry along the HTML directory.

spolischook avatar Jan 18 '22 10:01 spolischook

So, I've been able to make HTMLEngine work with S3 Actually the issue with view.HTMLEngine, Load() method, that is able to load templates only from the filesystem (virtual or actual) I've passed my own FileGetter interface:

type FileGetter interface {
	GetFileContent(path string) ([]byte, error)
	ListFiles() ([]string, error)
}

embedded HTMLEngine and change Load() method. Also, I've implemented LocalFs and S3 structs of the FileGetter interface Now it can get templates whatever you want.

I could make these changes to get the ability to load templates from anywhere, but it will have BC breaks. The FileGetter should be added to the HTMLEngine constructor, or to Load() function of interface.

Maybe that change is applicable to the Iris contrib repo. @kataras let me know what do you think.

P.S.: another way - is to make a new function, same as LoadAssets, let's call it LoadTemplates - in such a way we can take care of BC

spolischook avatar Jan 24 '22 05:01 spolischook

something like this would be useful for my use case where our users would like to dynamically provide templates which can change sporadically

gf3 avatar Feb 08 '22 20:02 gf3

I will wait comment from @kataras Will be happy to make that changes to iris

spolischook avatar Feb 09 '22 10:02 spolischook

Hello @spolischook and @gf3,

If the addition requires the amazon s3 go dependency, then it's better to live as its own repository that will work as an "extension" of the HTMLEngine. I have invited you to the https://github.com/iris-contrib/s3, that repository will contain everything amazon s3-related and Iris. You can initialize the root project and then create a sub-package of 'view/html' and put your work there. Please let me know your thoughts.

kataras avatar Mar 01 '22 23:03 kataras

@kataras sorry for late response. I'm ukrainian and I have a lot of issues to resolve right now, so this one is not in a priority. I'll look at it a bit later. Actually, what I remember, we can have LoadTemplates method, same as LoadAssets and the get templates from S3 (actualy from any storega, e.g. database) will be few lines of user code

spolischook avatar Mar 05 '22 09:03 spolischook

Yes @spolischook but Aws S3 is a heavy dependency we really don't need in the main repository, that's why we must put it on iris-contrib/s3. And about Ukraine I am really sorry for what happens there - stay strong! I have a lot of ukrainian friends!

kataras avatar Mar 12 '22 10:03 kataras

Zero dependency. It just gave me ability to inject my own AwsS3 io.Reader

spolischook avatar Mar 12 '22 10:03 spolischook

Hello @spolischook, how are you?

You can customize how directories are loaded to view engine by: iris.Patches().Context().ResolveFS method. Currently we support: string, fs.FS, http.FileSystem and embed.FS (code here). The fs.FS type is useful on your case, because you can use s3-to-fs package to convert your amazon s3 objects to go fs.FS value.

If you found other solution, could you please share the code on PR or here or the repository we've created for S3?

kataras avatar Sep 22 '22 21:09 kataras