Giter VIP home page Giter VIP logo

multisender's Introduction

MultiSender - Go Module for Simultaneous Writing to Multiple io.Writer Instances

MultiSender is a Go module that enables simultaneous writing to multiple io.Writer instances. It provides a MultiSenderWriter type and a MultiSender type to facilitate efficient and parallelized writing.

Installation

To use this module in your Go project, you can run:

go get github.com/NIR3X/multisender

Usage

package main

import (
	"io"
	"net/http"
	"path/filepath"
	"time"

	"github.com/NIR3X/filecache"
	"github.com/NIR3X/filewatcher"
	"github.com/NIR3X/logger"
)

const (
	fileCacheMaxSize          = 2 * 1024 * 1024
	fileWatcherUpdateInterval = 1 * time.Second
	rootDir                   = "www"
)

func main() {
	fileCache := filecache.NewFileCache(fileCacheMaxSize)
	multiSender := NewMultiSender(fileCache)
	fileWatcher := filewatcher.NewFileWatcher(fileWatcherUpdateInterval, func(path string, isDir bool) { // created
		if isDir {
			return
		}
		err := fileCache.Update(path)
		if err != nil {
			logger.Eprintln(err)
		}
	}, func(path string, isDir bool) { // removed
		if isDir {
			return
		}
		fileCache.Delete(path)
	}, func(path string, isDir bool) { // modified
		if isDir {
			return
		}
		err := fileCache.Update(path)
		if err != nil {
			logger.Eprintln(err)
		}
	})
	defer fileWatcher.Close()
	err := fileWatcher.Watch(rootDir)
	if err != nil {
		logger.Eprintln(err)
		return
	}
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		path := r.URL.Path
		if path == "/" {
			path = "/index.html"
		}
		path = filepath.Join(rootDir, path)
		reader, ident := fileCache.GetCached(path)
		switch ident {
		case filecache.Cached:
			_, err = io.Copy(w, reader)
			if err != nil {
				logger.Eprintln(err)
			}
		case filecache.Piped:
			multiSenderWriter := multiSender.Add(path, w)
			multiSenderWriter.Wait()
		}
	})
	logger.Println("Listening on port 8000...")
	err = http.ListenAndServe(":8000", nil)
	if err != nil {
		logger.Eprintln(err)
		return
	}
}

License

GNU AGPLv3 Image

This program is Free Software: You can use, study share and improve it at your will. Specifically you can redistribute and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

multisender's People

Contributors

nir3x avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.