Set Hooks Programmatically?

Heya folks, Really enjoying Tus so far.

Is there a way to set HttpHooks in tusd programmatically? I’ve dug through the docs and source for a few hours now and can only seem to references to setting web hooks via the CLI. This isn’t ideal for my specific use case, so I was really hoping I could set it programmatically when I initialize the handler.
I’d like to be able to do something like this:

handler, err := tusd.NewHandler(tusd.Config { 
	    BasePath:              "/files/",
		StoreComposer:         composer,
		NotifyCompleteUploads: true,
	})
handler.CompletedUploads = tusd.NewHookEvent(<endpoint Info>)

And sort of just have it do the post-upload completion hooks automatically. Am I missing something obvious here?

I got it working but frankly the solution I ended up with should be considered a warcrime. Essentially what I did was overwrite the Flag object and reinitialize it by calling all the constructor functions(SetupPreHooks, SetupPostHooks, etc) in flags.go before returning a handler to the webserver.

Would love to know if anyone has a cleaner solution.

Hi there,

thanks for the kind words that you are enjoying tus so far :slight_smile:

Unfortunately, the entire hooks system (including the HTTP hooks, file hooks, gRPC hooks, …) is currently designed to be only used by the tusd CLI. It is not intended to be a public API and therefore it is not covered in any API documentation or guarantees about backwards compatibility. So, I do not think there is any better way to programmatically set the HTTP hooks. To my knowledge, you are the first person wanting to use the HTTP hooks programmatically, so this is new terrain for us as well.

That being said, I would be open to moving the hooks system into the tusd library itself. It would make reusability (as in your case) possible and the components easier to test. Let me know if you are interested in making a PR for this :slight_smile:

Hey Marius,

Thanks for the prompt reply! I’m definitely interested in helping get the Hooks system into the package library, however I’m really slammed right now trying to get this project out the door. Perhaps I’ll attempt it in a few weeks when things are less hectic.

1 Like