- add documentation
- add more info to README.MD - splited into multiple files
This commit is contained in:
1 parent
d8155d86e0
commit
951f61fbd4
7 files changed
+687
-189
No files matched your search
@@ -1,2 +1,3 @@
|
|||||||
*.exe
|
*.exe
|
||||||
sounds/**
|
sounds/**
|
||||||
|
conf.json
|
||||||
@@ -1,2 +1,28 @@
|
|||||||
|

|
||||||
|
|
||||||
# Soundr
|
# Soundr
|
||||||
An opensource audio server meant for professional applications
|
Soundr is a simple, open-source, cross-platform audio playing server written in go.
|
||||||
|
It aims to be simple to setup and work in many envoriments. It is also designed to be
|
||||||
|
easy to use and maintain.
|
||||||
|
Soundr is able to play multiple audio files at the same time. It is able to intigrate well as it uses a REST endpoint.
|
||||||
|
Swagger Documentation for that endpoint is available in `apiDocs.yml`.
|
||||||
|
The software it self is written in go and uses the BEEP library. It is made to be shipped as a single executable.
|
||||||
|
Another target was a minimal dependency tree.
|
||||||
|
Initally it was written to be used with [Bitfocus Companion](https://bitfocus.io/companion) in a more professional envoriment. (A client for Companion is currently WiP)
|
||||||
|
|
||||||
|
# Installation
|
||||||
|
Installation is as simple as it gets as it is a single executable.
|
||||||
|
Download one of the releases, drop your sounds into the /sounds folder and run the executable.
|
||||||
|
|
||||||
|
# Configuration
|
||||||
|
TODO, no config yet. Soon ports and other settings will be added.
|
||||||
|
|
||||||
|
# Usage
|
||||||
|
Drop your sounds into the /sounds. You can play them by sending a GET request to the /v1/play endpoint.
|
||||||
|
You need to know the base64 encoded file name of the sound you want to play. You can get started by querying /v1/list. It will return a list of all sounds with their respective base64 encoded file name.
|
||||||
|
Use that base64 as the `file` parameter in the request.
|
||||||
|
|
||||||
|
**Note**: The sounds must be in the format `*.mp3` (more will be supported soon :tm:).
|
||||||
|
|
||||||
|
# ToDo
|
||||||
|
- [ ] Add support for other audio formats
|
||||||
+144
@@ -0,0 +1,144 @@
|
|||||||
|
openapi: '3.0.2'
|
||||||
|
info:
|
||||||
|
title: Soundr
|
||||||
|
version: '1.0'
|
||||||
|
servers:
|
||||||
|
- url: http://localhost:8082/v1/
|
||||||
|
paths:
|
||||||
|
/play:
|
||||||
|
get:
|
||||||
|
summary: Plays a sound by it's base64'd name. Will load it to buffer first if not already loaded.
|
||||||
|
parameters:
|
||||||
|
- in: query
|
||||||
|
name: file
|
||||||
|
description: A base64 encoded version of the file name
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
|
responses:
|
||||||
|
'200':
|
||||||
|
description: OK
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
id:
|
||||||
|
type: number
|
||||||
|
description: The ID of the playing sound
|
||||||
|
'400':
|
||||||
|
description: Bad Request
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
reason:
|
||||||
|
type: string
|
||||||
|
description: The error message, in this case probably "file not found"
|
||||||
|
/buffer:
|
||||||
|
get:
|
||||||
|
summary: Loads a sound into the buffer.
|
||||||
|
parameters:
|
||||||
|
- in: query
|
||||||
|
name: file
|
||||||
|
description: A base64 encoded version of the file name
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
|
responses:
|
||||||
|
'200':
|
||||||
|
description: OK
|
||||||
|
|
||||||
|
'400':
|
||||||
|
description: Bad Request
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
reason:
|
||||||
|
type: string
|
||||||
|
description: The error message, in this case probably "file not found"
|
||||||
|
/bufferAll:
|
||||||
|
get:
|
||||||
|
summary: Loads all sounds into the buffer.
|
||||||
|
responses:
|
||||||
|
'200':
|
||||||
|
description: OK
|
||||||
|
/stop:
|
||||||
|
get:
|
||||||
|
summary: Stops a given sound by it's ID.
|
||||||
|
parameters:
|
||||||
|
- in: query
|
||||||
|
name: id
|
||||||
|
description: The ID of the sound to stop
|
||||||
|
schema:
|
||||||
|
type: number
|
||||||
|
responses:
|
||||||
|
'200':
|
||||||
|
description: OK
|
||||||
|
'400':
|
||||||
|
description: Bad Request
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
reason:
|
||||||
|
type: string
|
||||||
|
description: The error message
|
||||||
|
/stopAll:
|
||||||
|
get:
|
||||||
|
summary: Stops all sounds.
|
||||||
|
responses:
|
||||||
|
'200':
|
||||||
|
description: OK
|
||||||
|
/current:
|
||||||
|
get:
|
||||||
|
summary: Gets the current playing sound(s).
|
||||||
|
responses:
|
||||||
|
'200':
|
||||||
|
description: OK
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
sounds:
|
||||||
|
type: array
|
||||||
|
items:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
id:
|
||||||
|
type: number
|
||||||
|
description: The ID of the sound
|
||||||
|
name:
|
||||||
|
type: string
|
||||||
|
description: The name of the sound
|
||||||
|
loaded:
|
||||||
|
type: boolean
|
||||||
|
description: Whether the sound is loaded into the buffer
|
||||||
|
/list:
|
||||||
|
get: # TODO REWORK!!!!!
|
||||||
|
summary: Lists all sounds in the buffer.
|
||||||
|
responses:
|
||||||
|
'200':
|
||||||
|
description: OK
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
sounds:
|
||||||
|
type: array
|
||||||
|
items:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
name:
|
||||||
|
type: string
|
||||||
|
description: The name of the sound
|
||||||
|
base64:
|
||||||
|
type: string
|
||||||
|
description: The base64 version of the name
|
||||||
|
url:
|
||||||
|
type: string
|
||||||
|
description: The URL to the sound
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/faiface/beep"
|
||||||
|
"github.com/faiface/beep/mp3"
|
||||||
|
"github.com/faiface/beep/speaker"
|
||||||
|
)
|
||||||
|
|
||||||
|
func BufferSound(file string) bool {
|
||||||
|
_, ok := streamMap[file]
|
||||||
|
if !ok {
|
||||||
|
fmt.Println("Not in memory, loading")
|
||||||
|
f, err := os.Open("./sounds/" + file)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Opened file")
|
||||||
|
streamer, format, _ := mp3.Decode(f)
|
||||||
|
speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
|
||||||
|
|
||||||
|
fmt.Println("Decoded file")
|
||||||
|
buffer := beep.NewBuffer(format)
|
||||||
|
buffer.Append(streamer)
|
||||||
|
streamer.Close()
|
||||||
|
fmt.Println("Bufferd file")
|
||||||
|
|
||||||
|
// Save to streamMap
|
||||||
|
streamMap[file] = streamBuf{
|
||||||
|
Streamer: streamer,
|
||||||
|
Format: format,
|
||||||
|
Buffer: buffer,
|
||||||
|
}
|
||||||
|
return (true)
|
||||||
|
} else {
|
||||||
|
return (false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func PlaySound(file string, index int) int {
|
||||||
|
playbacks[index] = playback{
|
||||||
|
File: file,
|
||||||
|
IsLoaded: false,
|
||||||
|
Streamer: nil,
|
||||||
|
Control: nil,
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Playing sound: " + file)
|
||||||
|
var buffer *beep.Buffer
|
||||||
|
BufferSound(file)
|
||||||
|
buffer = streamMap[file].Buffer
|
||||||
|
streamer := streamMap[file].Streamer
|
||||||
|
|
||||||
|
fmt.Println("Trying to play sound")
|
||||||
|
shot := buffer.Streamer(0, buffer.Len())
|
||||||
|
|
||||||
|
done := make(chan bool)
|
||||||
|
ctrl := &beep.Ctrl{Streamer: beep.Seq(shot, beep.Callback(func() {
|
||||||
|
done <- true
|
||||||
|
})), Paused: false}
|
||||||
|
|
||||||
|
playbacks[index] = playback{
|
||||||
|
File: file,
|
||||||
|
IsLoaded: true,
|
||||||
|
Streamer: streamer,
|
||||||
|
Control: ctrl,
|
||||||
|
}
|
||||||
|
speaker.Play(ctrl)
|
||||||
|
<-done
|
||||||
|
fmt.Println("Finished playing sound: " + file)
|
||||||
|
delete(playbacks, index)
|
||||||
|
return 1
|
||||||
|
}
|
||||||
@@ -0,0 +1,181 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||||
|
<svg
|
||||||
|
id="svg247771"
|
||||||
|
viewBox="0 0 440.63513 107.85945"
|
||||||
|
height="107.85945"
|
||||||
|
width="440.63513"
|
||||||
|
version="1.1"
|
||||||
|
sodipodi:docname="logo.svg"
|
||||||
|
inkscape:version="1.1 (c68e22c387, 2021-05-23)"
|
||||||
|
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
|
||||||
|
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
|
||||||
|
xmlns:xlink="http://www.w3.org/1999/xlink"
|
||||||
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
|
xmlns:svg="http://www.w3.org/2000/svg"
|
||||||
|
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
|
||||||
|
xmlns:cc="http://creativecommons.org/ns#"
|
||||||
|
xmlns:dc="http://purl.org/dc/elements/1.1/">
|
||||||
|
<sodipodi:namedview
|
||||||
|
id="namedview24"
|
||||||
|
pagecolor="#505050"
|
||||||
|
bordercolor="#eeeeee"
|
||||||
|
borderopacity="1"
|
||||||
|
inkscape:pageshadow="0"
|
||||||
|
inkscape:pageopacity="0"
|
||||||
|
inkscape:pagecheckerboard="0"
|
||||||
|
showgrid="false"
|
||||||
|
inkscape:zoom="1.0833333"
|
||||||
|
inkscape:cx="221.07692"
|
||||||
|
inkscape:cy="-79.846154"
|
||||||
|
inkscape:window-width="1920"
|
||||||
|
inkscape:window-height="1017"
|
||||||
|
inkscape:window-x="-8"
|
||||||
|
inkscape:window-y="-8"
|
||||||
|
inkscape:window-maximized="1"
|
||||||
|
inkscape:current-layer="svg247771" />
|
||||||
|
<metadata
|
||||||
|
id="metadata247777">
|
||||||
|
<rdf:rdf>
|
||||||
|
<cc:work
|
||||||
|
rdf:about="">
|
||||||
|
<dc:format>image/svg+xml</dc:format>
|
||||||
|
<dc:type
|
||||||
|
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
|
||||||
|
</cc:work>
|
||||||
|
</rdf:rdf>
|
||||||
|
</metadata>
|
||||||
|
<defs
|
||||||
|
id="defs247775" />
|
||||||
|
<linearGradient
|
||||||
|
spreadMethod="pad"
|
||||||
|
y2="0.30000001"
|
||||||
|
x2="-0.1"
|
||||||
|
y1="1.2"
|
||||||
|
x1="0.30000001"
|
||||||
|
id="3d_gradient2-logo-b36c4a67-d2ab-47f0-a348-b74b176fcd78">
|
||||||
|
<stop
|
||||||
|
id="stop247752"
|
||||||
|
stop-opacity="1"
|
||||||
|
stop-color="#ffffff"
|
||||||
|
offset="0%" />
|
||||||
|
<stop
|
||||||
|
id="stop247754"
|
||||||
|
stop-opacity="1"
|
||||||
|
stop-color="#000000"
|
||||||
|
offset="100%" />
|
||||||
|
</linearGradient>
|
||||||
|
<linearGradient
|
||||||
|
gradientTransform="rotate(-30)"
|
||||||
|
spreadMethod="pad"
|
||||||
|
y2="0.30000001"
|
||||||
|
x2="-0.1"
|
||||||
|
y1="1.2"
|
||||||
|
x1="0.30000001"
|
||||||
|
id="3d_gradient3-logo-b36c4a67-d2ab-47f0-a348-b74b176fcd78">
|
||||||
|
<stop
|
||||||
|
id="stop247757"
|
||||||
|
stop-opacity="1"
|
||||||
|
stop-color="#ffffff"
|
||||||
|
offset="0%" />
|
||||||
|
<stop
|
||||||
|
id="stop247759"
|
||||||
|
stop-opacity="1"
|
||||||
|
stop-color="#cccccc"
|
||||||
|
offset="50%" />
|
||||||
|
<stop
|
||||||
|
id="stop247761"
|
||||||
|
stop-opacity="1"
|
||||||
|
stop-color="#000000"
|
||||||
|
offset="100%" />
|
||||||
|
</linearGradient>
|
||||||
|
<g
|
||||||
|
id="logo-group"
|
||||||
|
transform="translate(-291.68243,-313.94615)">
|
||||||
|
<image
|
||||||
|
xlink:href=""
|
||||||
|
id="container"
|
||||||
|
style="display:none"
|
||||||
|
x="272"
|
||||||
|
y="144"
|
||||||
|
width="480"
|
||||||
|
height="480" />
|
||||||
|
<g
|
||||||
|
id="logo-center">
|
||||||
|
<image
|
||||||
|
xlink:href=""
|
||||||
|
id="icon_container"
|
||||||
|
style="display:none" />
|
||||||
|
<g
|
||||||
|
id="title"
|
||||||
|
style="font-style:normal;font-weight:200;font-size:72px;line-height:1;font-family:Montserrat;font-variant-ligatures:none;text-align:center;text-anchor:middle">
|
||||||
|
<path
|
||||||
|
id="path247924"
|
||||||
|
style="font-style:normal;font-weight:200;font-size:72px;line-height:1;font-family:Montserrat;font-variant-ligatures:none;text-align:center;text-anchor:middle"
|
||||||
|
d="m 420.69725,-45.792 q -2.448,-1.584 -5.904,-2.448 -3.384,-0.936 -6.768,-0.936 -6.624,0 -10.656,2.736 -3.96,2.664 -3.96,7.128 0,3.888 1.944,6.264 1.944,2.376 4.752,3.672 2.88,1.224 7.776,2.736 5.184,1.584 8.28,3.024 3.096,1.368 5.184,4.104 2.16,2.736 2.16,7.2 0,3.744 -2.088,6.624 -2.088,2.808 -5.976,4.392 -3.816,1.512 -9,1.512 -4.752,0 -9.432,-1.944 -4.608,-1.944 -7.776,-5.184 l 1.008,-1.008 q 2.88,3.096 7.272,4.896 4.392,1.8 8.928,1.8 7.128,0 11.376,-3.024 4.248,-3.024 4.248,-8.064 0,-3.96 -2.016,-6.336 -1.944,-2.448 -4.824,-3.744 -2.88,-1.368 -7.848,-2.88 -5.112,-1.584 -8.208,-2.952 -3.024,-1.44 -5.112,-4.104 -2.088,-2.664 -2.088,-7.056 0,-5.112 4.392,-8.136 4.392,-3.096 11.664,-3.096 3.528,0 7.128,1.008 3.672,1.008 6.264,2.664 z"
|
||||||
|
stroke-width="0"
|
||||||
|
stroke-linejoin="miter"
|
||||||
|
stroke-miterlimit="2"
|
||||||
|
fill="#b9311d"
|
||||||
|
stroke="#b9311d"
|
||||||
|
transform="matrix(1.67,0,0,1.67,-358.33709,403.16423)" />
|
||||||
|
<path
|
||||||
|
id="path247926"
|
||||||
|
style="font-style:normal;font-weight:200;font-size:72px;line-height:1;font-family:Montserrat;font-variant-ligatures:none;text-align:center;text-anchor:middle;display:none"
|
||||||
|
d="m 450.84725,-37.224 q 5.112,0 9.216,2.376 4.176,2.376 6.48,6.624 2.376,4.248 2.376,9.576 0,5.328 -2.376,9.576 -2.304,4.248 -6.48,6.696 -4.104,2.376 -9.216,2.376 -5.112,0 -9.288,-2.376 -4.104,-2.448 -6.48,-6.696 -2.304,-4.248 -2.304,-9.576 0,-5.328 2.304,-9.576 2.376,-4.248 6.48,-6.624 4.176,-2.376 9.288,-2.376 z m 0,1.44 q -4.752,0 -8.568,2.232 -3.816,2.16 -5.976,6.048 -2.088,3.888 -2.088,8.856 0,4.968 2.088,8.928 2.16,3.888 5.976,6.12 3.816,2.16 8.568,2.16 4.752,0 8.568,-2.16 3.816,-2.232 5.904,-6.12 2.16,-3.96 2.16,-8.928 0,-4.968 -2.16,-8.856 -2.088,-3.888 -5.904,-6.048 -3.816,-2.232 -8.568,-2.232 z"
|
||||||
|
stroke-width="0"
|
||||||
|
stroke-linejoin="miter"
|
||||||
|
stroke-miterlimit="2"
|
||||||
|
fill="#b9311d"
|
||||||
|
stroke="#b9311d" />
|
||||||
|
<path
|
||||||
|
id="path247928"
|
||||||
|
style="font-style:normal;font-weight:200;font-size:72px;line-height:1;font-family:Montserrat;font-variant-ligatures:none;text-align:center;text-anchor:middle"
|
||||||
|
d="M 510.569,-37.224 V 0 h -1.44 v -10.872 q -1.728,5.184 -5.688,7.992 -3.96,2.808 -9.792,2.88 -6.768,0 -10.584,-3.888 -3.816,-3.888 -3.816,-10.8 v -22.536 h 1.44 v 22.536 q 0,6.264 3.384,9.792 3.456,3.456 9.576,3.456 7.272,-0.144 11.376,-4.752 4.104,-4.68 4.104,-12.816 v -18.216 z"
|
||||||
|
stroke-width="0"
|
||||||
|
stroke-linejoin="miter"
|
||||||
|
stroke-miterlimit="2"
|
||||||
|
fill="#b9311d"
|
||||||
|
stroke="#b9311d"
|
||||||
|
transform="matrix(1.67,0,0,1.67,-346.51349,403.16423)" />
|
||||||
|
<path
|
||||||
|
id="path247930"
|
||||||
|
style="font-style:normal;font-weight:200;font-size:72px;line-height:1;font-family:Montserrat;font-variant-ligatures:none;text-align:center;text-anchor:middle"
|
||||||
|
d="m 543.52475,-37.224 q 6.912,0 10.728,3.888 3.888,3.888 3.888,10.8 V 0 h -1.44 v -22.536 q 0,-6.264 -3.528,-9.72 -3.456,-3.528 -9.648,-3.528 -6.912,0.144 -11.016,4.248 -4.104,4.104 -4.68,11.304 V 0 h -1.44 v -37.224 h 1.44 v 10.512 q 1.8,-4.968 5.832,-7.704 4.032,-2.736 9.864,-2.808 z"
|
||||||
|
stroke-width="0"
|
||||||
|
stroke-linejoin="miter"
|
||||||
|
stroke-miterlimit="2"
|
||||||
|
fill="#b9311d"
|
||||||
|
stroke="#b9311d"
|
||||||
|
transform="matrix(1.67,0,0,1.67,-341.50349,403.16423)" />
|
||||||
|
<path
|
||||||
|
id="path247932"
|
||||||
|
style="font-style:normal;font-weight:200;font-size:72px;line-height:1;font-family:Montserrat;font-variant-ligatures:none;text-align:center;text-anchor:middle"
|
||||||
|
d="M 605.051,-53.424 V 0 h -1.44 v -10.944 q -2.088,5.112 -6.552,8.136 -4.392,2.952 -10.224,2.952 -5.184,0 -9.36,-2.376 -4.176,-2.448 -6.552,-6.696 -2.304,-4.32 -2.304,-9.648 0,-5.328 2.304,-9.648 2.376,-4.32 6.552,-6.696 4.176,-2.448 9.36,-2.448 5.832,0 10.224,3.024 4.464,2.952 6.552,8.064 v -27.144 z m -18.216,52.128 q 4.824,0 8.64,-2.232 3.816,-2.232 5.976,-6.12 2.16,-3.96 2.16,-8.928 0,-4.968 -2.16,-8.928 -2.16,-3.96 -5.976,-6.192 -3.816,-2.232 -8.64,-2.232 -4.824,0 -8.64,2.232 -3.816,2.232 -5.976,6.192 -2.16,3.96 -2.16,8.928 0,4.968 2.16,8.928 2.16,3.888 5.976,6.12 3.816,2.232 8.64,2.232 z"
|
||||||
|
stroke-width="0"
|
||||||
|
stroke-linejoin="miter"
|
||||||
|
stroke-miterlimit="2"
|
||||||
|
fill="#b9311d"
|
||||||
|
stroke="#b9311d"
|
||||||
|
transform="matrix(1.67,0,0,1.67,-336.49349,403.16423)" />
|
||||||
|
<path
|
||||||
|
id="path247934"
|
||||||
|
style="font-style:normal;font-weight:200;font-size:72px;line-height:1;font-family:Montserrat;font-variant-ligatures:none;text-align:center;text-anchor:middle"
|
||||||
|
d="m 622.75063,-27.072 q 1.65599,-4.752 5.32799,-7.416 3.67201,-2.664 8.928,-2.736 v 1.44 q -6.336,0.144 -10.08,4.248 -3.67199,4.032 -4.17599,11.16 V 0 h -1.44001 v -37.224 h 1.44001 z"
|
||||||
|
stroke-width="0"
|
||||||
|
stroke-linejoin="miter"
|
||||||
|
stroke-miterlimit="2"
|
||||||
|
fill="#b9311d"
|
||||||
|
stroke="#b9311d"
|
||||||
|
transform="matrix(1.67,0,0,1.67,-331.48349,403.16423)" />
|
||||||
|
</g>
|
||||||
|
<image
|
||||||
|
xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAgAElEQVR4Xu1dB3wcxbmf2b2TXEU1JWDJjRIcCCpugLFlNZohEEyv5pG8vIQESH0EEvIICSSBQJKX9uglFCchlMSoGJvuJpnmgImNJdmEYmPjbt3pdt7/m7097e3N3u6diu90u7+fENbu7M58M/8pX/l/nAVXIIFAAq4S4IFsAgkEEnCXQACQYHQEEkgjgQAgwfAIJBAAJBgDgQSyk0CwgmQnt6BUgUggAEiBdHTQzOwkEAAkO7kF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EF3vhxEjP8T/3+w8+CunNUry24ThnFtfPB1SjuMYKPN0egOEoDqWuvMQeG4CC3ckDiLcXeQtFSUXRhjBq0inrEfbrLSNO3bda0dv7DfJwdH1AGKELUXMNf4rfWtnd/LWv57qGDeAaSxavznmBF5za2DVecOspBjED6dqRGQzgGYMnHANe6U9gUFSFLAAa3WkOHFNdGd0dH2M4lqJWmuKLsdxHTXWODQdF5N/x+LCRgW3UHSVFn2TbRHDlACRygUqomGtA+SD+6pILGDg7RMTNOuBqiud1uJXcck5KBzra62tWOh/Rnpt2WIO1XliC1FC4U+V7v0vXf30FjP6rP5B5DyLine truncated
|
||||||
|
id="icon"
|
||||||
|
x="422"
|
||||||
|
y="-47"
|
||||||
|
width="58.162498"
|
||||||
|
height="58.162498"
|
||||||
|
transform="matrix(1.67,0,0,1.67,-353.32709,403.16423)" />
|
||||||
|
</g>
|
||||||
|
</g>
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 29 KiB |
@@ -1,22 +1,14 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html"
|
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/faiface/beep"
|
"github.com/faiface/beep"
|
||||||
"github.com/faiface/beep/mp3"
|
|
||||||
"github.com/faiface/beep/speaker"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type playback struct {
|
type playback struct {
|
||||||
@@ -38,198 +30,66 @@ type streamBuf struct {
|
|||||||
Buffer *beep.Buffer
|
Buffer *beep.Buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Configuration struct {
|
||||||
|
Port int
|
||||||
|
}
|
||||||
|
|
||||||
var playbacks map[int]playback
|
var playbacks map[int]playback
|
||||||
var mapMutex = sync.Mutex{}
|
|
||||||
|
|
||||||
var streamMap map[string]streamBuf
|
var streamMap map[string]streamBuf
|
||||||
|
|
||||||
func BufferSound(file string) bool {
|
|
||||||
_, ok := streamMap[file]
|
|
||||||
if !ok {
|
|
||||||
fmt.Println("Not in memory, loading")
|
|
||||||
f, err := os.Open("./sounds/" + file)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println("Opened file")
|
|
||||||
streamer, format, err := mp3.Decode(f)
|
|
||||||
speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
|
|
||||||
|
|
||||||
fmt.Println("Decoded file")
|
|
||||||
buffer := beep.NewBuffer(format)
|
|
||||||
buffer.Append(streamer)
|
|
||||||
streamer.Close()
|
|
||||||
fmt.Println("Bufferd file")
|
|
||||||
|
|
||||||
// Save to streamMap
|
|
||||||
streamMap[file] = streamBuf{
|
|
||||||
Streamer: streamer,
|
|
||||||
Format: format,
|
|
||||||
Buffer: buffer,
|
|
||||||
}
|
|
||||||
return (true)
|
|
||||||
} else {
|
|
||||||
return (false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func PlaySound(file string, index int) int {
|
|
||||||
playbacks[index] = playback{
|
|
||||||
File: file,
|
|
||||||
IsLoaded: false,
|
|
||||||
Streamer: nil,
|
|
||||||
Control: nil,
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println("Playing sound: " + file)
|
|
||||||
var buffer *beep.Buffer
|
|
||||||
BufferSound(file)
|
|
||||||
buffer = streamMap[file].Buffer
|
|
||||||
streamer := streamMap[file].Streamer
|
|
||||||
|
|
||||||
fmt.Println("Trying to play sound")
|
|
||||||
shot := buffer.Streamer(0, buffer.Len())
|
|
||||||
|
|
||||||
done := make(chan bool)
|
|
||||||
ctrl := &beep.Ctrl{Streamer: beep.Seq(shot, beep.Callback(func() {
|
|
||||||
done <- true
|
|
||||||
})), Paused: false}
|
|
||||||
|
|
||||||
playbacks[index] = playback{
|
|
||||||
File: file,
|
|
||||||
IsLoaded: true,
|
|
||||||
Streamer: streamer,
|
|
||||||
Control: ctrl,
|
|
||||||
}
|
|
||||||
speaker.Play(ctrl)
|
|
||||||
<-done
|
|
||||||
fmt.Println("Finished playing sound: " + file)
|
|
||||||
delete(playbacks, index)
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
|
fmt.Println("Welcome to Soundr!")
|
||||||
|
|
||||||
playbacks = make(map[int]playback)
|
playbacks = make(map[int]playback)
|
||||||
streamMap = make(map[string]streamBuf)
|
streamMap = make(map[string]streamBuf)
|
||||||
|
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
// Create /sounds if not exists
|
||||||
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Query().Get("name")))
|
if _, err := os.Stat("./sounds"); os.IsNotExist(err) {
|
||||||
})
|
fmt.Println("Created /sounds folder")
|
||||||
|
os.Mkdir("./sounds", 0777)
|
||||||
|
}
|
||||||
|
|
||||||
http.HandleFunc("/v1/play", func(w http.ResponseWriter, r *http.Request) {
|
// Handle config
|
||||||
w.Header().Set("Content-Type", "application/json")
|
fmt.Println("Opening conf.json")
|
||||||
var cnt = r.URL.Query().Get("file")
|
file, fOpenError := os.Open("conf.json") // Try to open the file
|
||||||
bytArr, err := base64.StdEncoding.DecodeString(cnt)
|
|
||||||
if err != nil {
|
if errors.Is(fOpenError, os.ErrNotExist) { // If it does not exist, create it
|
||||||
log.Fatal(err)
|
fmt.Println("Creating conf.json")
|
||||||
|
file, fOpenError = os.Create("conf.json")
|
||||||
|
if fOpenError != nil {
|
||||||
|
log.Fatal(fOpenError)
|
||||||
}
|
}
|
||||||
fmt.Println(string(bytArr[:]))
|
defer file.Close()
|
||||||
t, err := os.Stat("./sounds/" + string(bytArr[:]))
|
fmt.Println("Writing to conf.json")
|
||||||
t = t
|
// Write the default config to the file
|
||||||
if !errors.Is(err, os.ErrNotExist) {
|
json.NewEncoder(file).Encode(Configuration{
|
||||||
var currIndex = len(playbacks)
|
Port: 8080,
|
||||||
fmt.Fprintf(w, "{\"status\":\"ok\", \"id\":%d}", currIndex)
|
})
|
||||||
|
fmt.Println("Wrote to conf.json")
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
// Decode the config
|
||||||
|
decoder := json.NewDecoder(file)
|
||||||
|
configuration := Configuration{}
|
||||||
|
err := decoder.Decode(&configuration)
|
||||||
|
|
||||||
go PlaySound(string(bytArr[:]), currIndex)
|
if err != nil {
|
||||||
|
fmt.Println("error:", err)
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
// Web server stuff
|
||||||
fmt.Fprintf(w, "{\"status\":\"fail\", \"reason\":\"file not found\"}")
|
// Play route, takes file as parameter, file is base64 encoded
|
||||||
}
|
http.HandleFunc("/v1/play", handlePlay)
|
||||||
|
// Buffer route, buffers file
|
||||||
})
|
http.HandleFunc("/v1/buffer", handleBuffer)
|
||||||
|
http.HandleFunc("/v1/bufferAll", handleBufferAll)
|
||||||
http.HandleFunc("/v1/buffer", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/v1/stop", handleStop)
|
||||||
w.Header().Set("Content-Type", "application/json")
|
http.HandleFunc("/v1/stopAll", handleStopAll)
|
||||||
var cnt = r.URL.Query().Get("file")
|
http.HandleFunc("/v1/current", handleCurrent)
|
||||||
bytArr, err := base64.StdEncoding.DecodeString(cnt)
|
http.HandleFunc("/v1/list", handleListing)
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
t, err := os.Stat("./sounds/" + string(bytArr[:]))
|
|
||||||
t = t
|
|
||||||
if !errors.Is(err, os.ErrNotExist) {
|
|
||||||
fmt.Fprintf(w, "{\"status\":\"ok\"}")
|
|
||||||
go BufferSound(string(bytArr[:]))
|
|
||||||
} else {
|
|
||||||
fmt.Fprintf(w, "{\"status\":\"fail\", \"reason\":\"file not found\"}")
|
|
||||||
}
|
|
||||||
|
|
||||||
})
|
|
||||||
|
|
||||||
http.HandleFunc("/v1/stopAll", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
playbacks = make(map[int]playback)
|
|
||||||
fmt.Fprintf(w, "{\"status\":\"ok\"}")
|
|
||||||
speaker.Clear()
|
|
||||||
//fmt.Fprintf(w, "{\"status\":\"ok\", \"id\":%d}", currIndex)
|
|
||||||
|
|
||||||
})
|
|
||||||
|
|
||||||
http.HandleFunc("/v1/stop", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
var cnt, err = strconv.Atoi(r.URL.Query().Get("id"))
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(w, "{\"status\":\"fail\", \"reason\":\"invalid id\"}")
|
|
||||||
}
|
|
||||||
|
|
||||||
value, ok := playbacks[cnt]
|
|
||||||
if !ok {
|
|
||||||
fmt.Fprintf(w, "{\"status\":\"fail\", \"reason\":\"audio not playing\"}")
|
|
||||||
} else {
|
|
||||||
fmt.Fprintf(w, "{\"status\":\"ok\", \"id\":%d}", value)
|
|
||||||
value.Control.Paused = true
|
|
||||||
value.Control.Streamer = nil
|
|
||||||
delete(playbacks, cnt)
|
|
||||||
}
|
|
||||||
//fmt.Fprintf(w, "{\"status\":\"ok\", \"id\":%d}", currIndex)
|
|
||||||
|
|
||||||
})
|
|
||||||
|
|
||||||
http.HandleFunc("/v1/current", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
|
|
||||||
var tempResultSet map[int]playbackWebReturn
|
|
||||||
tempResultSet = make(map[int]playbackWebReturn)
|
|
||||||
|
|
||||||
for index, element := range playbacks {
|
|
||||||
tempResultSet[index] = playbackWebReturn{File: element.File, IsLoaded: element.IsLoaded, Id: index}
|
|
||||||
}
|
|
||||||
|
|
||||||
j, err := json.Marshal(tempResultSet)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Error: %s", err.Error())
|
|
||||||
} else {
|
|
||||||
fmt.Println(string(j))
|
|
||||||
}
|
|
||||||
fmt.Fprintf(w, string(j))
|
|
||||||
})
|
|
||||||
|
|
||||||
http.HandleFunc("/v1/list", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
var temp [][3]string
|
|
||||||
files, err := ioutil.ReadDir("./sounds/")
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, f := range files {
|
|
||||||
var soundObj [3]string
|
|
||||||
soundObj[0] = f.Name()
|
|
||||||
soundObj[1] = base64.StdEncoding.EncodeToString([]byte(f.Name()))
|
|
||||||
soundObj[2] = r.URL.Host + "/v1/play?file=" + soundObj[1]
|
|
||||||
temp = append(temp, soundObj)
|
|
||||||
}
|
|
||||||
|
|
||||||
j, err := json.Marshal(temp)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Error: %s", err.Error())
|
|
||||||
} else {
|
|
||||||
fmt.Println(string(j))
|
|
||||||
}
|
|
||||||
fmt.Fprintf(w, string(j))
|
|
||||||
})
|
|
||||||
log.Fatal(http.ListenAndServe(":8081", nil))
|
|
||||||
|
|
||||||
|
fmt.Println("Listening on port " + fmt.Sprint(configuration.Port))
|
||||||
|
log.Fatal(http.ListenAndServe(":"+fmt.Sprint(configuration.Port), nil))
|
||||||
}
|
}
|
||||||
+208
@@ -0,0 +1,208 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/faiface/beep/speaker"
|
||||||
|
)
|
||||||
|
|
||||||
|
func handlePlay(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Rejct everything else then GET requests
|
||||||
|
if r.Method != "GET" {
|
||||||
|
http.Error(w, "Method is not supported.", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json") // Set the content type to json
|
||||||
|
var cnt = r.URL.Query().Get("file") // Retrieve the file name from the query string
|
||||||
|
bytArr, err := base64.StdEncoding.DecodeString(cnt) // Decode the base64 string
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
t, err := os.Stat("./sounds/" + string(bytArr[:])) // Check if the file exists
|
||||||
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
|
w.WriteHeader(400)
|
||||||
|
fmt.Fprintf(w, "{\"status\":\"fail\", \"reason\":\"file not found\"}")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if t.IsDir() { // Make sure it is not a folder we are trying to play
|
||||||
|
w.WriteHeader(400)
|
||||||
|
fmt.Fprintf(w, "{\"status\":\"fail\", \"reason\":\"target is folder\"}")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var currIndex = len(playbacks) // Create a new index for the playback
|
||||||
|
fmt.Fprintf(w, "{\"status\":\"ok\", \"id\":%d}", currIndex) // Return a JSON object to the user
|
||||||
|
|
||||||
|
go PlaySound(string(bytArr[:]), currIndex) // Play the sound
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle Buffering
|
||||||
|
|
||||||
|
func handleBufferAll(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Rejct everything else then GET requests
|
||||||
|
if r.Method != "GET" {
|
||||||
|
http.Error(w, "Method is not supported.", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json") // Set the content type to json
|
||||||
|
|
||||||
|
var temp []string
|
||||||
|
files, err := ioutil.ReadDir("./sounds/") // Read the directory
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
// Loop through the files and add the file name to the temp array
|
||||||
|
// Also triggers the buffer process for the file
|
||||||
|
for _, f := range files {
|
||||||
|
temp = append(temp, f.Name())
|
||||||
|
go BufferSound(f.Name())
|
||||||
|
}
|
||||||
|
// Return the amount of files buffered
|
||||||
|
fmt.Fprintf(w, "{\"status\":\"ok\", \"amount\":%d}", len(temp))
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleBuffer(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Rejct everything else then GET requests
|
||||||
|
if r.Method != "GET" {
|
||||||
|
http.Error(w, "Method is not supported.", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json") // Set the content type to json
|
||||||
|
var cnt = r.URL.Query().Get("file") // Retrieve the file name from the query string
|
||||||
|
bytArr, err := base64.StdEncoding.DecodeString(cnt) // Decode the base64 string
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
t, err := os.Stat("./sounds/" + string(bytArr[:])) // Check if the file exists
|
||||||
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
|
w.WriteHeader(400)
|
||||||
|
fmt.Fprintf(w, "{\"status\":\"fail\", \"reason\":\"file not found\"}")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if t.IsDir() { // Make sure it is not a folder we are trying to play
|
||||||
|
w.WriteHeader(400)
|
||||||
|
fmt.Fprintf(w, "{\"status\":\"fail\", \"reason\":\"target is folder\"}")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprintf(w, "{\"status\":\"ok\"}")
|
||||||
|
go BufferSound(string(bytArr[:]))
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handeling Stop
|
||||||
|
|
||||||
|
func handleStop(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Rejct everything else then GET requests
|
||||||
|
if r.Method != "GET" {
|
||||||
|
http.Error(w, "Method is not supported.", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json") // Set the content type to json
|
||||||
|
var cnt, err = strconv.Atoi(r.URL.Query().Get("id")) // Retrieve the id, first convert it to an int
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(w, "{\"status\":\"fail\", \"reason\":\"invalid id\"}")
|
||||||
|
}
|
||||||
|
|
||||||
|
value, ok := playbacks[cnt] // Get value from playbacks map
|
||||||
|
if !ok {
|
||||||
|
w.WriteHeader(400)
|
||||||
|
fmt.Fprintf(w, "{\"status\":\"fail\", \"reason\":\"audio not playing\"}")
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(w, "{\"status\":\"ok\"}")
|
||||||
|
// Stop by pausing first then, set the streamer to nil. Finally delete it from the map
|
||||||
|
value.Control.Paused = true
|
||||||
|
value.Control.Streamer = nil
|
||||||
|
delete(playbacks, cnt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleStopAll(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Rejct everything else then GET requests
|
||||||
|
if r.Method != "GET" {
|
||||||
|
http.Error(w, "Method is not supported.", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json") // Set the content type to json
|
||||||
|
|
||||||
|
// Pause and stop all playbacks
|
||||||
|
for _, v := range playbacks {
|
||||||
|
v.Control.Paused = true
|
||||||
|
v.Control.Streamer = nil
|
||||||
|
}
|
||||||
|
speaker.Clear() // Clear the speaker and make it shut up
|
||||||
|
|
||||||
|
// Reset the map
|
||||||
|
playbacks = make(map[int]playback)
|
||||||
|
|
||||||
|
fmt.Fprintf(w, "{\"status\":\"ok\"}")
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleCurrent(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Rejct everything else then GET requests
|
||||||
|
if r.Method != "GET" {
|
||||||
|
http.Error(w, "Method is not supported.", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json") // Set the content type to json
|
||||||
|
|
||||||
|
var tempResultSet map[int]playbackWebReturn = make(map[int]playbackWebReturn) // Create a new map to store the results
|
||||||
|
// Iterate through the playbacks map and add important information to the tempResultSet map
|
||||||
|
for index, element := range playbacks {
|
||||||
|
tempResultSet[index] = playbackWebReturn{File: element.File, IsLoaded: element.IsLoaded, Id: index}
|
||||||
|
}
|
||||||
|
// Convert the map to a JSON object and return it to the user
|
||||||
|
j, err := json.Marshal(tempResultSet)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error: %s", err.Error())
|
||||||
|
} else {
|
||||||
|
fmt.Println(string(j))
|
||||||
|
fmt.Fprintf(w, string(j))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleListing(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Rejct everything else then GET requests
|
||||||
|
if r.Method != "GET" {
|
||||||
|
http.Error(w, "Method is not supported.", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json") // Set the content type to json
|
||||||
|
|
||||||
|
var temp [][3]string
|
||||||
|
files, err := ioutil.ReadDir("./sounds/") // Find all files in the sounds directory
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the file data to the temp array
|
||||||
|
for _, f := range files {
|
||||||
|
var soundObj [3]string
|
||||||
|
soundObj[0] = f.Name()
|
||||||
|
soundObj[1] = base64.StdEncoding.EncodeToString([]byte(f.Name()))
|
||||||
|
soundObj[2] = r.URL.Host + "/v1/play?file=" + soundObj[1]
|
||||||
|
temp = append(temp, soundObj)
|
||||||
|
}
|
||||||
|
// Convert the array to a JSON object and return it to the user
|
||||||
|
j, err := json.Marshal(temp)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error: %s", err.Error())
|
||||||
|
} else {
|
||||||
|
fmt.Println(string(j))
|
||||||
|
fmt.Fprintf(w, string(j))
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user