Finished the States

This commit is contained in:
2021-04-06 09:59:38 +02:00
parent b64158732c
commit 0727e912af
20 changed files with 949 additions and 860 deletions

View File

@@ -4,7 +4,8 @@
"./commands",
"@adonisjs/core/build/commands",
"@adonisjs/repl/build/commands",
"@adonisjs/lucid/build/commands"
"@adonisjs/lucid/build/commands",
"@adonisjs/mail/build/commands"
],
"exceptionHandlerNamespace": "App/Exceptions/Handler",
"aliases": {
@@ -23,7 +24,8 @@
"@adonisjs/redis",
"@adonisjs/session",
"@adonisjs/auth",
"@adonisjs/lucid"
"@adonisjs/lucid",
"@adonisjs/mail"
],
"aceProviders": [
"@adonisjs/repl"

View File

@@ -21,3 +21,8 @@ REDIS_HOST=
REDIS_PASSWORD=
GITHUB_TOKEN=
SMTP_HOST=
SMTP_PORT=
SMTP_USERNAME=
SMTP_PASSWORD=

File diff suppressed because one or more lines are too long

View File

@@ -6,10 +6,10 @@ import Logger from "@ioc:Adonis/Core/Logger";
export default class StatesController {
public async get ({response}: HttpContextContract) {
const is_sleeping = await Redis.get('artapi/states/sleeping')
const is_listening_music = await Redis.get('artapi/states/listening')
const is_developing = await Redis.get('artapi/states/developing')
const is_learning = await Redis.get('artapi/states/learning')
const is_sleeping = await Redis.get('states:sleeping')
const is_listening_music = await Redis.get('states:listening')
const is_developing = await Redis.get('states:developing')
const is_learning = await Redis.get('states:learning')
return response.status(200).send({
is_learning: this.getStatus(is_learning),
@@ -19,31 +19,32 @@ export default class StatesController {
})
}
public async set ({request, response}: HttpContextContract) {
const state = await request.param('state')
public async set ({request, response, params}: HttpContextContract) {
const state = params.state
const value = await request.input('value')
Logger.debug("MESSAGE RECEIVE")
if (state && value) {
await Redis.set(`artapi/states/${state}`, value)
await Redis.set(`states:${state}`, value)
switch (state) {
case 'learning':
await Redis.set(`artapi/states/developing`, 'false')
await Redis.set(`artapi/states/sleeping`, 'false')
break
case 'developing':
await Redis.set(`artapi/states/learning`, 'false')
await Redis.set(`artapi/states/sleeping`, 'false')
break
case 'listening':
await Redis.set(`artapi/states/sleeping`, 'false')
break
case 'sleeping':
await Redis.set(`artapi/states/developing`, 'false')
await Redis.set(`artapi/states/listening`, 'false')
await Redis.set(`artapi/states/learning`, 'false')
break
if (value === 'true') {
switch (state) {
case 'learning':
await Redis.set(`states:developing`, 'false')
await Redis.set(`states:sleeping`, 'false')
break
case 'developing':
await Redis.set(`states:learning`, 'false')
await Redis.set(`states:sleeping`, 'false')
break
case 'listening':
await Redis.set(`states:sleeping`, 'false')
break
case 'sleeping':
await Redis.set(`states:developing`, 'false')
await Redis.set(`states:listening`, 'false')
await Redis.set(`states:learning`, 'false')
break
}
}
await UpdateGitHubReadme()
@@ -51,10 +52,12 @@ export default class StatesController {
message: 'State successfully updated !'
})
}
Logger.info("Finish")
}
public getStatus(state: string | null): string {
return state === 'true' || state !== null ? "Yes" : "No"
if (state === null) return "No"
return state === 'true' ? "Yes" : "No"
}
}

View File

@@ -12,7 +12,7 @@ export default class AuthMiddleware {
/**
* The URL to redirect to when request is Unauthorized
*/
protected redirectTo = '/login'
protected redirectTo = '/'
/**
* Authenticates the current HTTP request against a custom set of defined

View File

@@ -1,3 +0,0 @@
export async function isListeningMusic(): Promise<void> {
}

View File

@@ -3,10 +3,10 @@ import axios from 'axios'
import Env from "@ioc:Adonis/Core/Env";
export async function UpdateGitHubReadme(): Promise<void> {
const sleeping = await Redis.get('artapi/states/sleeping')
const learning = await Redis.get('artapi/states/learning')
const developing = await Redis.get('artapi/states/developing')
const listening_music = await Redis.get('artapi/states/listening')
const sleeping = await Redis.get('states:sleeping')
const learning = await Redis.get('states:learning')
const developing = await Redis.get('states:developing')
const listening_music = await Redis.get('states:listening')
const infos_table = `| Informations | State |
| ---------------------------: | ------: |
@@ -50,5 +50,6 @@ export async function UpdateGitHubReadme(): Promise<void> {
}
function getStatus(state): string {
if (state === null) return "No"
return state === "true" ? "Yes" : "No"
}

View File

@@ -126,7 +126,7 @@ export const http: ServerConfig = {
| to set the header explicitly.
|
*/
forceContentNegotiationTo: 'true',
forceContentNegotiationToJSON: true,
}
/*

View File

@@ -98,17 +98,23 @@ const authConfig: AuthConfig = {
/*
|--------------------------------------------------------------------------
| Tokens provider
| Redis provider for managing tokens
|--------------------------------------------------------------------------
|
| Uses SQL database config for managing tokens. The foreignKey column is used
| to make the relationship between the user and the token. You are free to
| use any column name here.
| Uses Redis for managing tokens. We recommend using the "redis" driver
| over the "database" driver when the tokens based auth is the
| primary authentication mode.
|
| Redis ensure that all the expired tokens gets cleaned up automatically.
| Whereas with SQL, you have to cleanup expired tokens manually.
|
| The foreignKey column is used to make the relationship between the user
| and the token. You are free to use any column name here.
|
*/
tokenProvider: {
driver: 'database',
table: 'api_tokens',
driver: 'redis',
redisConnection: 'local',
foreignKey: 'user_id',
},

View File

@@ -6,9 +6,10 @@
*/
import Env from '@ioc:Adonis/Core/Env'
import { OrmConfig } from '@ioc:Adonis/Lucid/Orm'
import { DatabaseConfig } from '@ioc:Adonis/Lucid/Database'
const databaseConfig: DatabaseConfig = {
const databaseConfig: DatabaseConfig & { orm: Partial<OrmConfig> } = {
/*
|--------------------------------------------------------------------------
| Connection
@@ -42,14 +43,26 @@ const databaseConfig: DatabaseConfig = {
password: Env.get('MYSQL_PASSWORD', ''),
database: Env.get('MYSQL_DB_NAME'),
},
migrations: {
naturalSort: true,
},
healthCheck: false,
debug: false,
debug: false,
},
}
},
/*
|--------------------------------------------------------------------------
| ORM Configuration
|--------------------------------------------------------------------------
|
| Following are some of the configuration options to tweak the conventional
| settings of the ORM. For example:
|
| - Define a custom function to compute the default table name for a given model.
| - Or define a custom function to compute the primary key for a given model.
|
*/
orm: {
},
}
export default databaseConfig

59
config/mail.ts Normal file
View File

@@ -0,0 +1,59 @@
/**
* Config source: https://git.io/JvgAf
*
* Feel free to let us know via PR, if you find something broken in this contract
* file.
*/
import Env from '@ioc:Adonis/Core/Env'
import { MailConfig } from '@ioc:Adonis/Addons/Mail'
const mailConfig: MailConfig = {
/*
|--------------------------------------------------------------------------
| Default mailer
|--------------------------------------------------------------------------
|
| The following mailer will be used to send emails, when you don't specify
| a mailer
|
*/
mailer: 'smtp',
/*
|--------------------------------------------------------------------------
| Mailers
|--------------------------------------------------------------------------
|
| You can define or more mailers to send emails from your application. A
| single `driver` can be used to define multiple mailers with different
| config.
|
| For example: Postmark driver can be used to have different mailers for
| sending transactional and promotional emails
|
*/
mailers: {
/*
|--------------------------------------------------------------------------
| Smtp
|--------------------------------------------------------------------------
|
| Uses SMTP protocol for sending email
|
*/
smtp: {
driver: 'smtp',
host: Env.get('SMTP_HOST'),
port: Env.get('SMTP_PORT'),
auth: {
user: Env.get('SMTP_USERNAME'),
pass: Env.get('SMTP_PASSWORD'),
type: 'login',
}
},
},
}
export default mailConfig

View File

@@ -40,7 +40,7 @@ const redisConfig: RedisConfig = {
port: Env.get('REDIS_PORT'),
password: Env.get('REDIS_PASSWORD', ''),
db: Env.get('REDIS_DB', 0),
keyPrefix: '',
keyPrefix: 'artapi:',
healthCheck: true
},
},

View File

@@ -10,7 +10,6 @@ import { SessionConfig } from '@ioc:Adonis/Addons/Session'
const sessionConfig: SessionConfig = {
enabled: true,
/*
|--------------------------------------------------------------------------
| Driver

14
contracts/mail.ts Normal file
View File

@@ -0,0 +1,14 @@
/**
* Contract source: https://git.io/JvgAT
*
* Feel free to let us know via PR, if you find something broken in this contract
* file.
*/
declare module '@ioc:Adonis/Addons/Mail' {
import { MailDrivers } from '@ioc:Adonis/Addons/Mail'
interface MailersList {
smtp: MailDrivers['smtp'],
}
}

View File

@@ -11,7 +11,7 @@
"lr": "node ace list:routes"
},
"devDependencies": {
"@adonisjs/assembler": "^5.0.1",
"@adonisjs/assembler": "^3.0.0",
"adonis-preset-ts": "^2.1.0",
"pino-pretty": "^4.7.1",
"typescript": "^4.2.3",
@@ -19,12 +19,13 @@
"youch-terminal": "^1.1.0"
},
"dependencies": {
"@adonisjs/auth": "^7.0.0",
"@adonisjs/core": "^5.1.4",
"@adonisjs/lucid": "^12.0.0",
"@adonisjs/redis": "^7.0.0",
"@adonisjs/repl": "^3.0.0",
"@adonisjs/session": "^6.0.1",
"@adonisjs/auth": "^5.1.1",
"@adonisjs/core": "~5.0.4-preview-rc-2.1",
"@adonisjs/lucid": "^10.0.0",
"@adonisjs/mail": "^5.2.3",
"@adonisjs/redis": "^5.0.9",
"@adonisjs/repl": "^1.0.0",
"@adonisjs/session": "^4.0.5",
"axios": "^0.21.1",
"luxon": "^1.26.0",
"mysql": "^2.18.1",

View File

@@ -21,7 +21,7 @@ import Server from "@ioc:Adonis/Core/Server";
*/
Server.middleware.register([
//'Adonis/Core/BodyParserMiddleware',
'Adonis/Core/BodyParserMiddleware',
'App/Middleware/SilentAuth',
])

View File

@@ -27,7 +27,9 @@ Route.get('/source', async ({response}: HttpContextContract) => {
Route.get('health', async ({response}: HttpContextContract) => {
const report = await HealthCheck.getReport()
return report.healthy ? response.ok(report) : response.badRequest(report)
const isLive = await HealthCheck.isLive()
const isReady = await HealthCheck.isReady()
return report.healthy ? response.ok({ isLive, isReady, report: report.report }) : response.badRequest({ isLive, isReady, report: report.report })
})
// ArtAPI
@@ -50,12 +52,15 @@ Route.group(() => {
Route.resource('users', 'UsersController').only(['store', 'update', 'destroy'])
Route.resource('subscribers', 'SubscribersController').only(['update', 'destroy'])
Route.resource('files', 'FileController').only(['store', 'destroy'])
Route.post('/states', 'StatesController.set')
Route.post('/stats/build', 'StatesController.incrementBuild')
Route.post('/stats/command', 'StatesController.incrementCommand')
Route.post('/locations', 'LocationsController.add')
Route.post('/projects', 'ProjectsController.add')
}).middleware('auth')
}).middleware('auth:web')
Route.group(() => {
Route.post('/states/:state', 'StatesController.set')
Route.post('/stats/build', 'StatesController.incrementBuild')
Route.post('/stats/command', 'StatesController.incrementCommand')
}).middleware('auth:api')
Route.group(() => {
Route.get('/me', 'AuthController.user').middleware('auth')

View File

@@ -31,7 +31,8 @@
"@adonisjs/redis",
"@adonisjs/session",
"@adonisjs/auth",
"@adonisjs/lucid"
"@adonisjs/lucid",
"@adonisjs/mail"
]
}
}

1305
yarn.lock

File diff suppressed because it is too large Load Diff