npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redis-queue-golang-server

v0.0.1

Published

```bash sudo apt install redis sudo gedit /etc/redis/redis.conf # edit line # ... # from bind 127.0.0.1 ::1 # to bind 0.0.0.0 # ...

Downloads

3

Readme

Redis Queue Golang Server

sudo apt install redis
sudo gedit /etc/redis/redis.conf
# edit line
# ...
# from
bind 127.0.0.1 ::1
# to
bind 0.0.0.0
# ...

sudo service redis restart
sudo service redis-server restart
# OR
sudo systemctl restart redis.service
sudo systemctl restart redis-server.service

go file: main.go

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"time"

	"github.com/go-redis/redis"
)

var rdb = redis.NewClient(&redis.Options{
	Addr:     "127.0.0.1:6379", // use default Addr
	Password: "",               // no password set
	DB:       0,                // use default DB
})

type queue struct {
	Count int64  `json:"count"`
	Name  string `json:"name"`
}

var d = `[{"count":2,"name":"queueTest01"},{"count":2,"name":"queueTest02"}]`

func main() {
	var listQueue []queue
	_ = json.Unmarshal([]byte(d), &listQueue)
	for _, q := range listQueue {
		go listenQueue(q.Count, q.Name)
	}

	http.HandleFunc("/addQueue", addQueue)
	http.HandleFunc("/post", post)
	_ = http.ListenAndServe(":8080", nil)
}

func post(writer http.ResponseWriter, request *http.Request) {
	fmt.Println("RequestURI", request.RequestURI)
	fmt.Println("foo", request.FormValue("foo"))
	fmt.Println("bar", request.FormValue("bar"))
	fmt.Println(request.Header)
}

type postData struct {
	Api  string `json:"api"`
	Data string `json:"data"`
}

func addQueue(writer http.ResponseWriter, request *http.Request) {
	var p postData
	// p.Api = request.FormValue("api")
	// p.Data = request.FormValue("data")
	// var queueName = request.FormValue("name")

	p.Api = `http://localhost:8080/post`
	p.Data = `{"foo":"123","bar":"456"}`
	var queueName = `queueTest01`

	b, _ := json.Marshal(p)
	_, _ = rdb.RPush(queueName, string(b)).Result()
}

func postRequest(requestUrl, contentType string, form url.Values) ([]byte, error) {
	//form = url.Values{"key":{"value"}, "name":{"value"}}
	//ContentType = "application/x-www-form-urlencoded"
	buffer := bytes.NewBufferString(form.Encode())
	rsp, err := http.Post(requestUrl, contentType, buffer)
	if err != nil {
		log.Printf("http.Post False! %v", err)
	} else {
		defer rsp.Body.Close()
	}
	return ioutil.ReadAll(rsp.Body)
}

func rawRequest(RequestUrl, Method string, CollectionHeader map[string]string, Json []byte) (StatusCode int, result []byte, error error) {
	req, err := http.NewRequest(Method, RequestUrl, bytes.NewBuffer(Json))
	if err == nil {
		for key, value := range CollectionHeader {
			req.Header.Set(key, value)
		}
	}
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		log.Printf("http.Post False! %v", err)
		error = err
		return
	} else {
		defer resp.Body.Close()
	}
	StatusCode = resp.StatusCode
	result, error = ioutil.ReadAll(resp.Body)
	return
}

func listenQueue(n int64, name string) {
	for {
		time.Sleep(1 * time.Second)
		list, err := rdb.LRange(name, 0, n-1).Result()
		if err == nil {
			for _, s := range list {
				queueReqest(s)
			}
			rdb.LTrim(name, int64(len(list)), -1)
		}
	}
}

func queueReqest(s string) {
	var p postData
	_ = json.Unmarshal([]byte(s), &p)
	var d = make(map[string]string)
	_ = json.Unmarshal([]byte(p.Data), &d)

	var form = url.Values{}

	for k, v := range d {
		form[k] = []string{v}
	}

	var ContentType = "application/x-www-form-urlencoded"

	_, _ = postRequest(p.Api, ContentType, form)
}