From bf91c7ef4f374a78bff46a42a85c0426b0abf7d0 Mon Sep 17 00:00:00 2001 From: Indrajith K L Date: Mon, 6 Nov 2023 06:12:28 +0530 Subject: 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 --- .editorconfig | 9 ++++++++ .gitignore | 5 +++++ LICENSE | 21 ++++++++++++++++++ README.md | 16 ++++++++++++++ shard.lock | 18 ++++++++++++++++ shard.yml | 17 +++++++++++++++ spec/spec_helper.cr | 2 ++ spec/wialt_listener_spec.cr | 9 ++++++++ src/wialt_listener.cr | 52 +++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 149 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 shard.lock create mode 100644 shard.yml create mode 100644 spec/spec_helper.cr create mode 100644 spec/wialt_listener_spec.cr create mode 100644 src/wialt_listener.cr diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..163eb75 --- /dev/null +++ b/.editorconfig @@ -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 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0bb75ea --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/docs/ +/lib/ +/bin/ +/.shards/ +*.dwarf diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..6f1ec5f --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Indrajith K L + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..abeadbc --- /dev/null +++ b/README.md @@ -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 diff --git a/shard.lock b/shard.lock new file mode 100644 index 0000000..82d8d02 --- /dev/null +++ b/shard.lock @@ -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 + diff --git a/shard.yml b/shard.yml new file mode 100644 index 0000000..5100ca1 --- /dev/null +++ b/shard.yml @@ -0,0 +1,17 @@ +name: wialt_listener +version: 0.1.0 + +authors: + - Indrajith K L + +dependencies: + kemal: + github: kemalcr/kemal + +targets: + wialt_listener: + main: src/wialt_listener.cr + +crystal: '>= 1.10.1' + +license: MIT diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr new file mode 100644 index 0000000..cb31982 --- /dev/null +++ b/spec/spec_helper.cr @@ -0,0 +1,2 @@ +require "spec" +require "../src/wialt_listener" diff --git a/spec/wialt_listener_spec.cr b/spec/wialt_listener_spec.cr new file mode 100644 index 0000000..bf9fa0c --- /dev/null +++ b/spec/wialt_listener_spec.cr @@ -0,0 +1,9 @@ +require "./spec_helper" + +describe WialtListener do + # TODO: Write tests + + it "works" do + false.should eq(true) + end +end diff --git a/src/wialt_listener.cr b/src/wialt_listener.cr new file mode 100644 index 0000000..7c4d01e --- /dev/null +++ b/src/wialt_listener.cr @@ -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 -- cgit v1.2.3