A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
6
import VSearch from './page/search.js';7
import VFavorites from './page/favorites.js';8
import VAccount from './page/account.js';9
import VAuth from './page/auth.js';10
import VDetail from './component/detail.js';12
export const VERSION = '{{version}}';
21
devices: null,22
token: null,23
station: null,23
authenticated: false,24
authenticated: null,25
open: false,26
version: VERSION27
};
40
VSearch,41
VFavorites,42
VAccount,43
VAuth,44
VPlayer,45
VPopup,46
VDetail
57
state.devices = await http`get::/api/devices`();58
state.token = localStorage.getItem(TOKEN_NAME);59
}58
catch(error) {59
// ignore60
catch(err) {61
state.account = null;62
state.devices = null;63
state.token = null;64
}65
state.tab = 'search';66
state.open = false;67
state.station = null;68
if(state.account === null) {62
this.$refs.popup.open({63
title: $lang('global.brand'),64
description: $lang('global.slogan') + $lang('global.upcoming')65
});69
state.authenticated = false;70
return;71
}72
state.authenticated = true;
156
},157
template: `158
<div159
v-if="state.authenticated !== null"160
@touchstart="onTouchStart"161
@touchend="onTouchEnd"162
class="px-4 sm:px-10 max-w-[1200px] mx-auto flex flex-col min-h-[100vh] max-h-[100vh] overflow-hidden"163
>159
<v-menu :state="state"></v-menu>164
<v-auth v-if="!state.authenticated" :state="state"></v-auth>165
<v-menu v-if="state.authenticated" :state="state"></v-menu>166
<div class="flex flex-1 gap-[32px] sm:gap-[5rem] overflow-y-clip transition-transform md:transition-none" :style="styles">167
<v-search v-if="state.authenticated" ref="search" :state="state"></v-search>168
<v-favorites v-if="state.authenticated" ref="favorites" :state="state"></v-favorites>
37
const {name, language} = this.state.account;38
this.name = name;39
this.language = language;40
},41
logout() {42
localStorage.removeItem('verbal-token');43
this.state.app.init();44
}45
},46
template: `
100
class="mt-4 mx-auto"101
></v-button>102
</div>103
<div class="flex flex-col">104
<button105
class="w-full h-[52px] px-4 text-md bg-zinc-900 hover:bg-zinc-800 text-zinc-100 rounded-md outline-none"106
@click="logout"107
>108
{{$lang('global.logout')}}109
</button>110
</div>111
<div>112
<p class="text-sm text-gray-400 text-center">Verbal - {{state.version}}</p>113
<p class="text-sm text-gray-400 text-center pt-1">
1
import {$lang, http} from '../utils.js';2
import VTab from '../component/tab.js';3
import VSelect from '../component/select.js';4
import VButton from '../element/button.js';6
export default {7
props: ['state'],8
data() {9
return {10
name: '',11
language: 'en',12
token: '',13
method: null14
};15
},16
components: {17
VTab,18
VSelect,19
VButton20
},21
methods: {22
$lang,23
async register() {24
try {25
localStorage.removeItem('verbal-token');26
const device = await http`post::/api/account`({27
name: this.name,28
language: this.language29
});30
localStorage.setItem('verbal-token', device.uid);31
this.state.app.init();32
}33
catch(err) {34
console.error(err);35
}36
},37
async login() {38
try {39
localStorage.setItem('verbal-token', this.token);40
await http`get::/api/account`();41
this.state.app.init();42
}43
catch(err) {44
localStorage.removeItem('verbal-token');45
console.error(err);46
}47
}48
},49
template: `50
<v-tab id="account" :tab="state.tab">51
<div class="flex flex-col items-center justify-center h-screen min-h-screen">52
<div v-if="method === null">53
<h3 class="text-white text-[34px] font-bold pb-4 -mt-14 text-center">{{$lang('global.brand')}}</h3>54
<p class="text-md text-gray-400 text-center pb-12 px-4">{{$lang('global.slogan')}}</p>55
<div class="flex flex-col gap-5 w-full px-4">56
<button57
class="w-full h-[52px] px-4 text-md bg-zinc-100 hover:bg-zinc-300 text-zinc-900 rounded-md outline-none"58
@click="method = 'register'"59
>60
Create an account61
</button>62
<button63
class="w-full h-[52px] px-4 text-md bg-zinc-900 hover:bg-zinc-800 text-zinc-100 rounded-md outline-none"64
@click="method = 'login'"65
>66
Log In67
</button>68
</div>69
</div>70
<div v-if="method === 'register'" class="w-full flex flex-col gap-5 -mt-12">71
<v-button72
icon="arrow-left"73
title="Back"74
@click="method = null"75
class="self-start"76
>77
</v-button>78
<h3 class="text-white text-[24px] font-bold pb-4 px-4 sm:pt-0">Create an account</h3>79
<div class="flex flex-col gap-1 w-full px-4">80
<label for="username" class="text-white text-lg">Choose your username</label>81
<input82
v-model="name"83
id="username"84
type="text"85
maxlength="20"86
spellcheck="false"87
autocomplete="off"88
class="rounded-md w-full h-[52px] px-4 mt-2 text-md bg-zinc-900 text-white placeholder:text-gray-500 focus:placeholder:text-gray-600 outline-none"89
>90
</div>91
<div class="flex flex-col gap-1 w-full px-4">92
<label for="language" class="text-white text-lg">Select language</label>93
<v-select94
id="language"95
v-model="language"96
class="mt-2"97
>98
<option value="en">English</option>99
<option value="de">Deutsch</option>100
</v-select>101
</div>102
<button103
class="h-[52px] px-4 mx-4 text-md bg-zinc-100 hover:bg-zinc-300 text-zinc-900 rounded-md outline-none mt-8"104
@click="register"105
>106
Sign up now107
</button>108
</div>109
<div v-if="method === 'login'" class="w-full flex flex-col gap-5 -mt-12">110
<v-button111
icon="arrow-left"112
title="Back"113
@click="method = null"114
class="self-start"115
>116
</v-button>117
<h3 class="text-white text-[24px] font-bold pb-4 px-4 sm:pt-0">Log In</h3>118
<div class="flex flex-col gap-1 w-full px-4">119
<label for="token" class="text-white text-lg">Insert account token</label>120
<input121
v-model="token"122
id="token"123
type="text"124
spellcheck="false"125
autocomplete="off"126
class="rounded-md w-full h-[52px] px-4 mt-2 text-md bg-zinc-900 text-white placeholder:text-gray-500 focus:placeholder:text-gray-600 outline-none"127
>128
</div>129
<button130
class="h-[52px] px-4 mx-4 text-md bg-zinc-100 hover:bg-zinc-300 text-zinc-900 rounded-md outline-none mt-8"131
@click="login"132
>133
Sign In134
</button>135
</div>136
</div>137
</v-tab>138
`139
}
1
#!/bin/bash3
declare -A config4
config=(5
["port"]=42006
["uri"]="/api"7
["method"]="GET"8
["args"]=""9
["token"]="test-token"10
)12
while read argument; do13
key=$(echo "$argument" | awk '{split($0, a, "="); print a[1]}')14
if [ -z "$key" ]; then15
continue16
fi17
value=$(echo "$argument" | awk '{st = index($0, "="); print substr($0, st + 1)}')18
config["$key"]=$value19
done <<< $(echo "$@" | tr " " "\n")21
# while read key; do22
# echo "$key: ${config[$key]}"23
# done <<< $(echo "${!config[@]}" | tr " " "\n")25
# echo ""27
url="http://localhost:${config["port"]}${config["uri"]}?${config["args"]}"29
echo "Request"30
echo "------------------------------------------------------------"32
echo "${config["method"]} $url"33
echo "verbal-token: ${config["token"]}"34
echo ""36
echo "Response"37
echo "------------------------------------------------------------"39
curl "$url" -I -g41
curl "$url" -s -g -X "${config["method"]}" -H "verbal-token: ${config["token"]}" | jq -r "."
1
#!/bin/bash3
declare -A config4
config=(5
["port"]=""6
["uri"]="/api"7
["method"]="GET"8
["args"]=""9
["token"]="test-token"10
)12
while read argument; do13
key=$(echo "$argument" | awk '{split($0, a, "="); print a[1]}')14
if [ -z "$key" ]; then15
continue16
fi17
value=$(echo "$argument" | awk '{st = index($0, "="); print substr($0, st + 1)}')18
config["$key"]=$value19
done <<< $(echo "$@" | tr " " "\n")21
# while read key; do22
# echo "$key: ${config[$key]}"23
# done <<< $(echo "${!config[@]}" | tr " " "\n")25
# echo ""27
url="http://localhost:${config["port"]}${config["uri"]}?${config["args"]}"29
echo ""30
echo "Request"31
echo "------------------------------------------------------------"33
echo "${config["method"]} $url"34
echo "verbal-token: ${config["token"]}"35
echo ""37
echo "Response"38
echo "------------------------------------------------------------"40
curl "$url" -I -g42
curl "$url" -s -g -X "${config["method"]}" -H "verbal-token: ${config["token"]}" | jq -r "."
1
use sqlx::postgres::Postgres;2
use sqlx::Acquire;3
use tide::http::Method;4
use tide_sqlx::SQLxRequestExt;6
use crate::model;7
use crate::unwrap_option_or_throw;8
use crate::unwrap_result_or_throw;9
use crate::Response;11
#[derive(Default)]12
pub struct Auth;
18
mut req: tide::Request<State>,19
next: tide::Next<'_, State>,20
) -> tide::Result {19
if req.url().path().starts_with("/api") {20
let token =21
unwrap_option_or_throw!(req.header("verbal-token"), "no token was provided!")22
.as_str();23
let query = format!(24
"25
SELECT account.*26
FROM account27
INNER JOIN device28
ON account.id = device.account_id29
WHERE device.uid = '{}'30
",31
&token,32
);33
let option_account;34
{35
let mut conn = req.sqlx_conn::<Postgres>().await;36
option_account = unwrap_result_or_throw!(37
sqlx::query_as::<_, model::Account>(&query)38
.fetch_optional(unwrap_result_or_throw!(39
conn.acquire().await,40
"cannot acquire connection to database!"41
))42
.await,43
"cannot acquire account for given device!"44
);21
let path_url = req.url().path();22
if path_url.starts_with("/api") {23
match req.header("verbal-token") {24
Some(header) => {25
let token = header.as_str();26
let option_account;27
{28
let query = format!(29
"30
SELECT account.*31
FROM account32
INNER JOIN device33
ON account.id = device.account_id34
WHERE device.uid = '{}'35
",36
&token,37
);38
let mut conn = req.sqlx_conn::<Postgres>().await;39
option_account = unwrap_result_or_throw!(40
sqlx::query_as::<_, model::Account>(&query)41
.fetch_optional(unwrap_result_or_throw!(42
conn.acquire().await,43
"cannot acquire connection to database!"44
))45
.await,46
"cannot acquire account for given device!"47
);48
}49
req.set_ext(unwrap_option_or_throw!(50
option_account,51
"no account found for given token!"52
));53
}54
None => {55
if !(path_url.eq("/api/account") && req.method() == Method::Post) {56
return Response::throw("no token was provided!");57
}58
}59
}46
req.set_ext(unwrap_option_or_throw!(47
option_account,48
"no account found for given token!"49
));60
}61
let res = next.run(req).await;62
Ok(res)
104
#[derive(Debug, Clone, Serialize, FromRow)]105
pub struct Value(pub String);107
#[derive(Debug, Clone, Deserialize)]108
pub struct AddAccount {109
pub name: String,110
pub language: String,111
}
20
Response::with(account)21
}23
pub async fn add_account(mut req: tide::Request<()>) -> tide::Result {24
if req.ext::<model::Account>().is_some() {25
return Response::throw("already logged in with another account!");26
}27
let add_account = req.body_json::<model::AddAccount>().await?;28
info!("{:#?}", add_account);29
let mut conn = req.sqlx_conn::<Postgres>().await;30
let sql = format!(31
r#"32
INSERT INTO33
account34
(name, language)35
VALUES36
('{name}', '{language}')37
RETURNING account.*38
"#,39
name = add_account.name,40
language = add_account.language,41
);42
let account = sqlx::query_as::<_, model::Account>(&sql)43
.fetch_one(conn.acquire().await?)44
.await?;45
info!("{:#?}", account);46
let sql = format!(47
r#"48
INSERT INTO49
device50
(uid, account_id)51
VALUES52
(uuid_generate_v4(), {account_id})53
RETURNING device.*54
"#,55
account_id = account.id,56
);57
let device = sqlx::query_as::<_, model::Device>(&sql)58
.fetch_one(conn.acquire().await?)59
.await?;60
info!("{:#?}", device);61
Response::with(device)62
}64
pub async fn do_search(req: tide::Request<()>) -> tide::Result {65
let query = req.query::<model::Query>()?;66
let account =
51
/* handle api requests */52
app.at("/api/account").get(route::get_account);53
app.at("/api/account").put(route::update_account);54
app.at("/api/account").post(route::add_account);55
app.at("/api/search").get(route::do_search);56
app.at("/api/song").get(route::get_song);57
app.at("/api/favorite").get(route::get_favorites);
24
"global.brand": "Verbal",25
"global.slogan": "A mobile app for listening to radio stations from around the globe.",26
"global.upcoming": "<br><br>Comming soon!",27
"global.logout": "Log Out",28
"player.loading": "Loading...",29
"player.no-song-title": "- No song title available -",30
"search.input.all": "All",
72
"global.brand": "Verbal",73
"global.slogan": "Eine mobile App zum Hören von Radiosendern aus aller Welt.",74
"global.upcoming": "<br><br>Kommt bald!",75
"global.logout": "Abmelden",76
"player.loading": "Laden...",77
"player.no-song-title": "- Song Titel nicht verfügbar -",78
"search.input.all": "Alle",