Initial Commit

* Listener Implementation - In-Progress
* Initial API Endpoints and Websocket implementation
  * API End Points Implemented -> /music_changed, /state_change
  * Websocket End Points implemented -> /i_am_listening_to
* Socket Implementation - In-Progress
This commit is contained in:
2023-11-06 06:12:28 +05:30
commit bf91c7ef4f
9 changed files with 149 additions and 0 deletions

9
.editorconfig Normal file
View File

@@ -0,0 +1,9 @@
root = true
[*.cr]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true

5
.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
/docs/
/lib/
/bin/
/.shards/
*.dwarf

21
LICENSE Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2023 Indrajith K L <indrajith@indrajith.dev>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

16
README.md Normal file
View File

@@ -0,0 +1,16 @@
# [W]hat [I] [A]m [L]istening [T]o Listener
A Web Server written in Crystal Lang which is part of WIALT Project. This server is a broker server which act as a bridge between Local MPC Music Player and indrajith.dev website. Whose main purpose is to display music title I'am listening to.
## Installation
* Install Crystal
* `shards install`
## Usage
* `crystal run src/wialt_listener.cr`
## Contributors
- [Indrajith K L](https://github.com/cooljith91112) - creator and maintainer

18
shard.lock Normal file
View File

@@ -0,0 +1,18 @@
version: 2.0
shards:
backtracer:
git: https://github.com/sija/backtracer.cr.git
version: 1.2.2
exception_page:
git: https://github.com/crystal-loot/exception_page.git
version: 0.3.1
kemal:
git: https://github.com/kemalcr/kemal.git
version: 1.4.0
radix:
git: https://github.com/luislavena/radix.git
version: 0.4.1

17
shard.yml Normal file
View File

@@ -0,0 +1,17 @@
name: wialt_listener
version: 0.1.0
authors:
- Indrajith K L <indrajith@indrajith.dev>
dependencies:
kemal:
github: kemalcr/kemal
targets:
wialt_listener:
main: src/wialt_listener.cr
crystal: '>= 1.10.1'
license: MIT

2
spec/spec_helper.cr Normal file
View File

@@ -0,0 +1,2 @@
require "spec"
require "../src/wialt_listener"

View File

@@ -0,0 +1,9 @@
require "./spec_helper"
describe WialtListener do
# TODO: Write tests
it "works" do
false.should eq(true)
end
end

52
src/wialt_listener.cr Normal file
View File

@@ -0,0 +1,52 @@
# TODO: Write documentation for `WialtListener`
require "kemal"
class MusicChange
include JSON::Serializable
property title : String
end
module WialtListener
VERSION = "0.1.0"
sockets = [] of HTTP::WebSocket
static_headers do |response|
response.headers.add("Access-Control-Allow-Origin", "")
end
post "/music_changed" do |env|
response = MusicChange.from_json env.request.body.not_nil!
puts "#{response.title}"
send_message_to_clients(sockets, response.title)
{status: "OK"}
end
post "/state_change" do |env|
puts "#{env.request.body}"
{status: "OK"}
end
ws "/i_am_listening_to" do |socket|
sockets.push socket
socket.on_message do |message|
send_message_to_clients(sockets, message)
end
socket.on_close do |_|
sockets.delete(socket)
puts "Closing Socket: #{socket}"
end
end
def self.send_message_to_clients(sockets, title)
sockets.each do |a_socket|
a_socket.send title
end
end
Kemal.run
end