Merge pull request 'Rename backend to API' (#12) from rename-backend-to-api into main
All checks were successful
continuous-integration/drone/push Build is passing

Reviewed-on: #12
This commit is contained in:
Dominic Grimm 2022-01-20 20:22:21 +00:00
commit 41045bb78e
45 changed files with 68 additions and 70 deletions

View file

@ -16,19 +16,19 @@ steps:
---
kind: pipeline
type: docker
name: backend
name: api
steps:
- name: ameba
image: veelenga/ameba
commands:
- cd docker/backend/
- cd docker/api/
- ameba micrate/src/ src/
- name: pgsanity
image: boechat107/pgsanity
commands:
- cd docker/backend/db/
- cd docker/api/db/
- find -name "*.sql" | xargs pgsanity
- name: backend
- name: api
image: docker:dind
volumes:
- name: dockersock
@ -36,7 +36,7 @@ steps:
commands:
- cp .example.env .env
- cd docker/
- docker build --build-arg BUILD_ENV=development backend
- docker build --build-arg BUILD_ENV=development api
depends_on:
- ameba
- pgsanity

View file

@ -1,4 +1,4 @@
POSTGRES_USER="mw"
POSTGRES_PASSWORD="SUPERDUPERSECRET"
BACKEND_JWT_SECRET="SUPERDUPERSECRET"
API_JWT_SECRET="SUPERDUPERSECRET"

View file

@ -10,7 +10,7 @@ http {
# }
location /graphql {
proxy_pass http://backend;
proxy_pass http://api;
}
location /adminer {

View file

@ -7,7 +7,7 @@ services:
ports:
- 80:80
depends_on:
- backend
- api
db:
image: postgres:alpine
@ -25,17 +25,15 @@ services:
depends_on:
- db
backend:
api:
build:
context: ./docker/backend
context: ./docker/api
args:
BUILD_ENV: production
container_name: backend
container_name: api
environment:
BACKEND_SERVER_PORT: 8080
BACKEND_SERVER_HOST: 0.0.0.0
BACKEND_DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_USER}
BACKEND_JWT_SECRET: ${BACKEND_JWT_SECRET}
API_DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_USER}
API_JWT_SECRET: ${API_JWT_SECRET}
depends_on:
- db

View file

@ -17,14 +17,14 @@ RUN if [ "${BUILD_ENV}" = "development" ]; then \
fi
FROM ubuntu:latest as user
RUN useradd -u 10001 mw
RUN useradd -u 10001 api
FROM scratch as runner
WORKDIR /app
COPY --from=user /etc/passwd /etc/passwd
COPY --from=builder /app/bin/mw /bin/mw
COPY --from=builder /app/bin/api /bin/api
COPY --from=builder /app/bin/micrate /bin/micrate
COPY ./db ./db
USER mw
USER api
EXPOSE 80
ENTRYPOINT [ "mw" ]
ENTRYPOINT [ "api" ]

View file

@ -1,12 +1,12 @@
name: mw
name: api
version: 0.1.0
authors:
- Dominic Grimm <dominic.grimm@gmail.com>
targets:
mw:
main: src/mw.cr
api:
main: src/api.cr
micrate:
main: src/micrate.cr

1
docker/api/src/api.cr Normal file
View file

@ -0,0 +1 @@
require "./api/*"

View file

@ -1,7 +1,7 @@
require "crystal-argon2"
require "jwt"
module MW
module API
module Auth
extend self
@ -23,7 +23,7 @@ module MW
"exp" => expiration,
}
JWT.encode(payload.to_h, ENV_REQUESTER["BACKEND_JWT_SECRET"], JWT::Algorithm::HS256)
JWT.encode(payload.to_h, ENV_REQUESTER["API_JWT_SECRET"], JWT::Algorithm::HS256)
end
def create_user_jwt(user_id : Int, expiration : Int = (Time.utc + Time::Span.new(hours: 6)).to_unix) : String
@ -31,7 +31,7 @@ module MW
end
def decode_jwt(jwt : String) : JSON::Any
JWT.decode(jwt, ENV_REQUESTER["BACKEND_JWT_SECRET"], JWT::Algorithm::HS256)[0]
JWT.decode(jwt, ENV_REQUESTER["API_JWT_SECRET"], JWT::Algorithm::HS256)[0]
end
def decode_jwt?(jwt : String) : JSON::Any?

View file

@ -1,5 +1,5 @@
require "shard"
module MW
module API
AUTHORS = Shard.authors
end

View file

@ -4,7 +4,7 @@ require "fancyline"
require "./db"
module MW
module API
module CLI
extend self
@ -25,11 +25,11 @@ module MW
end
cli = Commander::Command.new do |cmd|
cmd.use = "mw"
cmd.long = "Mentorenwahl"
cmd.use = "api"
cmd.long = "Mentorenwahl API CLI"
cmd.run do
MW.run
API.run
end
cmd.commands.add do |c|
@ -37,7 +37,7 @@ module MW
c.long = "Prints the current version"
c.run do
puts MW::VERSION
puts API::VERSION
end
end
@ -46,7 +46,7 @@ module MW
c.long = "Prints the authors"
c.run do
puts MW::AUTHORS.join(",\n")
puts API::AUTHORS.join(",\n")
end
end

View file

@ -2,7 +2,7 @@ require "http/request"
require "graphql"
require "granite"
module MW
module API
class Context < GraphQL::Context
getter user : Db::User?
getter role : Schema::UserRole?

View file

@ -3,8 +3,8 @@ require "granite/adapter/pg"
require "./db/*"
module MW
module API
module Db
Granite::Connections << Granite::Adapter::Pg.new(name: "pg", url: ENV_REQUESTER["BACKEND_DATABASE_URL"])
Granite::Connections << Granite::Adapter::Pg.new(name: "pg", url: ENV_REQUESTER["API_DATABASE_URL"])
end
end

View file

@ -1,6 +1,6 @@
require "granite"
module MW
module API
module Db
class Admin < Granite::Base
table admins

View file

@ -1,6 +1,6 @@
require "granite"
module MW
module API
module Db
class Student < Granite::Base
table students

View file

@ -1,6 +1,6 @@
require "granite"
module MW
module API
module Db
class Teacher < Granite::Base
table teachers

View file

@ -1,6 +1,6 @@
require "granite"
module MW
module API
module Db
class TeacherVote < Granite::Base
table teacher_votes

View file

@ -1,7 +1,7 @@
require "CrystalEmail"
require "granite"
module MW
module API
module Db
class User < Granite::Base
table users

View file

@ -1,4 +1,4 @@
module MW
module API
module Db
enum UserRole
Admin

View file

@ -1,6 +1,6 @@
require "granite"
module MW
module API
module Db
class Vote < Granite::Base
table votes

View file

@ -1,4 +1,4 @@
module MW
module API
class EnvRequester
private property keys
@ -35,9 +35,9 @@ module MW
end
ENV_REQUESTER = EnvRequester.new([
"BACKEND_DATABASE_URL",
"BACKEND_ADMIN_EMAIL",
"BACKEND_ADMIN_PASSWORD",
"BACKEND_JWT_SECRET",
"API_DATABASE_URL",
"API_ADMIN_EMAIL",
"API_ADMIN_PASSWORD",
"API_JWT_SECRET",
])
end

View file

@ -1,6 +1,6 @@
require "http/server"
module MW
module API
extend self
def run : Nil

View file

@ -3,7 +3,7 @@ require "graphql"
require "./schema/helpers"
require "./schema/*"
module MW
module API
module Schema
SCHEMA = GraphQL::Schema.new(Query.new, Mutation.new)
end

View file

@ -1,6 +1,6 @@
require "graphql"
module MW
module API
module Schema
@[GraphQL::Object]
class Admin < GraphQL::BaseObject

View file

@ -1,6 +1,6 @@
require "graphql"
module MW
module API
module Schema
module Helpers
module ObjectMacros
@ -39,8 +39,8 @@ module MW
module DbObject
macro db_object(type)
include ::MW::Schema::Helpers::ObjectDbInit
include ::MW::Schema::Helpers::ObjectFinders
include ::API::Schema::Helpers::ObjectDbInit
include ::API::Schema::Helpers::ObjectFinders
db_init {{ type }}
finders {{ type }}

View file

@ -1,6 +1,6 @@
require "graphql"
module MW
module API
module Schema
@[GraphQL::Object]
class Mutation < GraphQL::BaseMutation

View file

@ -1,6 +1,6 @@
require "graphql"
module MW
module API
module Schema
@[GraphQL::Object]
class Query < GraphQL::BaseQuery

View file

@ -1,6 +1,6 @@
require "graphql"
module MW
module API
module Schema
@[GraphQL::Object]
class Student < GraphQL::BaseObject

View file

@ -1,6 +1,6 @@
require "graphql"
module MW
module API
module Schema
@[GraphQL::Object]
class Teacher < GraphQL::BaseObject

View file

@ -1,6 +1,6 @@
require "graphql"
module MW
module API
module Schema
@[GraphQL::Object]
class TeacherVote < GraphQL::BaseObject

View file

@ -1,6 +1,6 @@
require "graphql"
module MW
module API
module Schema
@[GraphQL::Object]
class User < GraphQL::BaseObject

View file

@ -1,6 +1,6 @@
require "graphql"
module MW
module API
module Schema
@[GraphQL::Enum]
enum UserRole

View file

@ -1,6 +1,6 @@
require "graphql"
module MW
module API
module Schema
@[GraphQL::Object]
class Vote < GraphQL::BaseObject

View file

@ -1,7 +1,7 @@
require "toro"
require "json"
module MW
module API
class Server < Toro::Router
private struct GraphQLData
include JSON::Serializable

View file

@ -1,5 +1,5 @@
require "shard"
module MW
module API
VERSION = Shard.version
end

View file

@ -0,0 +1,5 @@
require "micrate"
require "pg"
Micrate::DB.connection_url = ENV["API_DATABASE_URL"]?
Micrate::Cli.run

View file

@ -1,5 +0,0 @@
require "micrate"
require "pg"
Micrate::DB.connection_url = ENV["BACKEND_DATABASE_URL"]?
Micrate::Cli.run

View file

@ -1 +0,0 @@
require "./mw/*"

View file

@ -1,3 +1,3 @@
#!/usr/bin/env bash
docker-compose exec backend /micrate "$@"
docker-compose exec api micrate "$@"