Skip to content

Commit

Permalink
0.9.0 NEW Random Data API
Browse files Browse the repository at this point in the history
  • Loading branch information
jolav committed Mar 7, 2023
1 parent eb2eea1 commit 4f2e37c
Show file tree
Hide file tree
Showing 11 changed files with 358 additions and 113 deletions.
42 changes: 40 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

![Version](https://img.shields.io/badge/version-0.8.5-orange.svg)
![Version](https://img.shields.io/badge/version-0.9.0-orange.svg)
![Maintained YES](https://img.shields.io/badge/Maintained%3F-yes-green.svg)
![Ask Me Anything !](https://img.shields.io/badge/Ask%20me-anything-1abc9c.svg)

# ![logo](https://github.com/jolav/codetabs/blob/master/www/_public/icons/ct/ct64r.png?raw=true) **ONLINE TOOLS ([codetabs.com](https://codetabs.com))**

**version 0.8.5**
**version 0.9.0**

1. [Count LOC (lines of code) online from github/gitlab repos or zipped uploaded folder](#count-loc-online)
2. [CORS proxy](#cors-proxy)
Expand All @@ -15,6 +15,7 @@
6. [HTTP Headers](#headers)
7. [API weather temp](#weather)
8. [Video To Gif](#video2gif)
9. [Random Data API](#random-data-api)


[To Do List](#to-do)
Expand Down Expand Up @@ -233,6 +234,43 @@ Scale : Set width:height , if one parameter is -1 it will automatically determin
<hr>

![logo](https://github.com/jolav/codetabs/blob/master/www/_public/icons/random48.png?raw=true)

## **RANDOM DATA API**

### **[Demo and Docs online](https://codetabs.com/random-data/random-data.html)**

- Api to generate random data
- Only suppports GET request.
- Limit : 10 request per seconds. Once reached subsequent requests will result in error 429 (too many requests) until your quota is cleared.


### **Endpoints**

- Get Random Integers
```
http Request :
GET https://api.codetabs.com/v1/random/integer?range=X-Y
```

Examples
Get random number between 1-10 both inclusive
https://api.codetabs.com/v1/random/integer?min=1&max=10
You can also specify how many times you want the result with the parameter times.
Default is 1 and there is no need to specify it. Max times = 10.000
https://api.codetabs.com/v1/random/integer?min=1&max=10&times=50

- List with randomized order
```
http Request :
GET https://api.codetabs.com/v1/random/list?len=X
```
Max list elements : 10.000
Example: Get random order numbers for a list of 1000 elements
https://api.codetabs.com/v1/random/list?len=1000

<hr>

## TO DO

- [ ] **WWW** clean unused parts, css, etc
Expand Down
20 changes: 20 additions & 0 deletions _utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ func SliceContainsString(str string, slice []string) bool {
return false
}

// SliceContainsInteger ... returns true/false
func SliceContainsInteger(num int, slice []int) bool {
for _, v := range slice {
if v == num {
return true
}
}
return false
}

// GenericCommandSH ...
func GenericCommandSH(comm string) (chunk []byte, err error) {
chunk, err = exec.Command("sh", "-c", comm).CombinedOutput()
Expand All @@ -35,6 +45,16 @@ func GenericCommandSH(comm string) (chunk []byte, err error) {
return chunk, err
}

// RemoveElementFromSliceString
func RemoveElementFromSliceString(index int, slice []string) []string {
return append(slice[:index], slice[index+1:]...)
}

// RemoveElementFromSliceInt
func RemoveElementFromSliceInt(index int, slice []int) []int {
return append(slice[:index], slice[index+1:]...)
}

// GenericCommand ...
func GenericCommand(args []string) (err error) {
_, err = exec.Command(args[0], args[1:len(args)]...).CombinedOutput()
Expand Down
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"flag"
"fmt"
"log"
"math/rand"
"net/http"
"os"
"os/user"
Expand All @@ -20,13 +21,14 @@ import (
"github.com/jolav/codetabs/headers"
"github.com/jolav/codetabs/loc"
"github.com/jolav/codetabs/proxy"
"github.com/jolav/codetabs/random"
"github.com/jolav/codetabs/stars"
"github.com/jolav/codetabs/store"
"github.com/jolav/codetabs/video2gif"
"github.com/jolav/codetabs/weather"
)

var version = "0.8.5"
var version = "0.9.0"
var when = "undefined"

type Conf struct {
Expand All @@ -42,6 +44,8 @@ type Conf struct {
}

func main() {

rand.Seed(time.Now().UnixNano())
checkFlags()

var c Conf
Expand Down Expand Up @@ -91,6 +95,7 @@ func main() {
mux.HandleFunc("/v1/headers/", mw(headers.Router, "headers", c))
mux.HandleFunc("/v1/weather/", mw(weather.Router, "weather", c))
mux.HandleFunc("/v1/video2gif/", mw(video2gif.Router, "video2gif", c))
mux.HandleFunc("/v1/random/", mw(random.Router, "random", c))
mux.HandleFunc("/v1/stars/", mw(stars.Router, "stars", c))
mux.HandleFunc("/v1/proxy/", mw(proxy.Router, "proxy", c))
mux.HandleFunc("/v1/tmp/", mw(proxy.Router, "proxy", c))
Expand Down
113 changes: 113 additions & 0 deletions random/random.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/* */

package random

import (
"fmt"
"net/http"
"strconv"
"strings"

u "github.com/jolav/codetabs/_utils"
)

type random struct {
Quest string `json:"quest"`
Result []int `json:"data"`
}

func Router(w http.ResponseWriter, r *http.Request) {
params := strings.Split(strings.ToLower(r.URL.Path), "/")
path := params[1:len(params)]
if path[len(path)-1] == "" { // remove last empty slot after /
path = path[:len(path)-1]
}
//log.Printf("Going ....%s %s %d", path, r.Method, len(path))
if len(path) < 2 || path[0] != "v1" {
u.BadRequest(w, r)
return
}
if len(path) != 3 {
u.BadRequest(w, r)
return
}

if r.Method == "POST" {
u.BadRequest(w, r)
return
}

rd := newRandom()
randomType := path[2]
switch randomType {
case "integer":
rd.randomInteger(w, r)
case "list":
rd.randomList(w, r)
default:
u.BadRequest(w, r)
return
}
}

func (rd random) randomInteger(w http.ResponseWriter, r *http.Request) {
times := 1

r.ParseForm()
min, err := strconv.Atoi(r.Form.Get("min"))
if err != nil {
u.BadRequest(w, r)
return
}
max, err := strconv.Atoi(r.Form.Get("max"))
if err != nil || min > max || min < 0 || max > 10000000000 {
u.BadRequest(w, r)
return
}
if r.Form.Get("times") != "" {
times, err = strconv.Atoi(r.Form.Get("times"))
if err != nil || times > 10000 {
u.BadRequest(w, r)
return
}
}
if times == 1 {
rd.Quest = fmt.Sprintf("Random Integer between %d-%d", min, max)
} else {
rd.Quest = fmt.Sprintf("%d Random Integers between %d-%d", times, min, max)
}

for time := 1; time <= times; time++ {
rd.Result = append(rd.Result, u.GetRandomInt(min, max))
}

u.SendJSONToClient(w, rd, 200)
}

func (rd random) randomList(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
elements, err := strconv.Atoi(r.Form.Get("len"))
if err != nil || elements < 2 || elements > 10000 {
u.BadRequest(w, r)
return
}

rd.Quest = fmt.Sprintf("Randomized order list with %d elements", elements)
element := 0
for element < elements {
number := u.GetRandomInt(1, elements)
if !u.SliceContainsInteger(number, rd.Result) {
rd.Result = append(rd.Result, number)
element++
}
}
u.SendJSONToClient(w, rd, 200)
}

func newRandom() random {
rd := random{
Quest: "",
Result: []int{},
}
return rd
}
Binary file added www/_public/icons/random48.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified www/_public/images/sprite.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added www/_public/sprites/random32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion www/_public/templates/version.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<h2 class="centerText">
API doc
<small>
<small>(version 0.8.5)</small>
<small>(version 0.9.0)</small>
</small>
</h2>
<!-- End -->
113 changes: 21 additions & 92 deletions www/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -229,95 +229,24 @@ hr {
}

/* Generated by http://css.spritegen.com CSS Sprite Generator */

.ct64r, .ct48r, .alexa32, .ct32r, .gif32, .headers32, .ip32, .jolav32, .loc32, .notes32, .proxy32, .stars32, .weather32, .check {
display: inline-block;
background: url('/_public/images/sprite.png') no-repeat;
overflow: hidden;
text-indent: -9999px;
text-align: left;
}

.ct64r {
background-position: -0px -0px;
width: 64px;
height: 64px;
}

.ct48r {
background-position: -0px -64px;
width: 48px;
height: 48px;
}

.alexa32 {
background-position: -0px -112px;
width: 32px;
height: 32px;
}

.ct32r {
background-position: -32px -112px;
width: 32px;
height: 32px;
}

.gif32 {
background-position: -0px -144px;
width: 32px;
height: 32px;
}

.headers32 {
background-position: -32px -144px;
width: 32px;
height: 32px;
}

.ip32 {
background-position: -0px -176px;
width: 32px;
height: 32px;
}

.jolav32 {
background-position: -32px -176px;
width: 32px;
height: 32px;
}

.loc32 {
background-position: -0px -208px;
width: 32px;
height: 32px;
}

.notes32 {
background-position: -32px -208px;
width: 32px;
height: 32px;
}

.proxy32 {
background-position: -0px -240px;
width: 32px;
height: 32px;
}

.stars32 {
background-position: -32px -240px;
width: 32px;
height: 32px;
}

.weather32 {
background-position: -0px -272px;
width: 32px;
height: 32px;
}

.check {
background-position: -48px -64px;
width: 16px;
height: 16px;
}

.ct64r, .ct48r, .alexa32, .ct32r, .gif32,
.headers32, .ip32, .jolav32, .loc32, .notes32,
.proxy32, .random32, .stars32, .weather32, .check
{ display: inline-block; background: url('_public/images/sprite.png') no-repeat; overflow: hidden; text-indent: -9999px; text-align: left; }

.ct64r { background-position: -0px -0px; width: 64px; height: 64px; }
.ct48r { background-position: -0px -64px; width: 48px; height: 48px; }
.alexa32 { background-position: -0px -112px; width: 32px; height: 32px; }
.ct32r { background-position: -32px -112px; width: 32px; height: 32px; }
.gif32 { background-position: -0px -144px; width: 32px; height: 32px; }
.headers32 { background-position: -32px -144px; width: 32px; height: 32px; }
.ip32 { background-position: -0px -176px; width: 32px; height: 32px; }
.jolav32 { background-position: -32px -176px; width: 32px; height: 32px; }
.loc32 { background-position: -0px -208px; width: 32px; height: 32px; }
.notes32 { background-position: -32px -208px; width: 32px; height: 32px; }
.proxy32 { background-position: -0px -240px; width: 32px; height: 32px; }
.random32 { background-position: -32px -240px; width: 32px; height: 32px; }
.stars32 { background-position: -0px -272px; width: 32px; height: 32px; }
.weather32 { background-position: -32px -272px; width: 32px; height: 32px; }
.check { background-position: -48px -64px; width: 16px; height: 16px; }
Loading

0 comments on commit 4f2e37c

Please sign in to comment.