Skip to content
This repository has been archived by the owner on May 12, 2024. It is now read-only.
/ sharexhandler Public archive

A simple Golang web handler which can be used as a custom uploader endpoint for a ShareX client.

License

Notifications You must be signed in to change notification settings

michivip/sharexhandler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ShareX custom upload handler

This is a simple library which can be used to create your own custom Golang upload handler for your ShareX client. It is based on the http mux library which I really do recommend for working with http handlers in Golang.

Introduction

In ShareX you can choose between a bunch of different default built-in upload servers - but according to my expirience it was nearly impossible to manage all my uploaded files. Thats why I decided to write my own upload server (library). I chose Golang because it is a young and really powerful programming language which in my opinion has much potencial.

Tutorial

I will not write an own Wiki for this repository because in my opinion the usage is really simple and by explaining you some examples you should be able to use the code.

Design

The complete code is based on interfaces which can be used to code an own implementation. Moreover you can customize all paths/uris which should be used to handle processes. If thats still not enough you can just edit the code directly because of the really flexible MIT license.

Examples

The tutorial for itself only contains a basic example which is explained with comments. The complete design code (interfaces, basic methods/structs) is commented so that you can find your way through the code.

Basic example with SSL and some custom headers

package main

import (
	"github.com/michivip/sharexhandler"
	"net/http"
	"github.com/gorilla/mux"
	"crypto/tls"
	"strings"
	"log"
	"os"
	"github.com/michivip/sharexhandler/storages"
	"gopkg.in/mgo.v2"
	"bufio"
)

func main() {
	log.Println("Initializing storage...")
	fileFolder := "files/" // The folder where our uploaded files will be stored at.
	if err := os.Mkdir(fileFolder, os.ModePerm); err != nil { // Creating the folder
		panic(err)
	} else {
		// This example goes with the default built-in implemented MongoStorage but in your case you can use a different one.
		storage := &storages.MongoStorage{
			DialInfo: &mgo.DialInfo{ // The dial info. More information: https://godoc.org/labix.org/v2/mgo#DialInfo
				Addrs: []string{"localhost"},
			},
			Configuration: &storages.MongoStorageConfiguration{ // MongoDB configuration
				DatabaseName:         "sharex",   // Database name where collections are created in.
				UploadCollectionName: "uploads",  // Collection where the upload file information (not the file data) is stored at.
				FileFolderPath:       fileFolder, // The folder where the file data is stored at - no information.
			},
		}
		if err := storage.Initialize(); err != nil { // Initializing the storage - in our case connecting to the database.
			panic(err)
		} else {
			log.Println("Initialized!")
			log.Println("Set up custom upload server. Running it in background...")
			srv := setupHttpsServer(storage)
			go srv.ListenAndServeTLS("cert.pem", "private.pem") // Our cert file and our key file which are laying in our directory.
			// Now just a stop hook - nothing special.
			log.Println("Done! Enter \"stop\" to stop the webservers.")
			reader := bufio.NewReader(os.Stdin)
			var text string
			for ; strings.TrimSuffix(text, "\n") != "stop"; text, _ =