summaryrefslogtreecommitdiffhomepage
path: root/rails-backend/app/controllers/api
diff options
context:
space:
mode:
Diffstat (limited to 'rails-backend/app/controllers/api')
-rw-r--r--rails-backend/app/controllers/api/v1/auth_controller.rb109
-rw-r--r--rails-backend/app/controllers/api/v1/games_controller.rb167
-rw-r--r--rails-backend/app/controllers/api/v1/tags_controller.rb15
-rw-r--r--rails-backend/app/controllers/api/v1/users_controller.rb10
4 files changed, 301 insertions, 0 deletions
diff --git a/rails-backend/app/controllers/api/v1/auth_controller.rb b/rails-backend/app/controllers/api/v1/auth_controller.rb
new file mode 100644
index 0000000..590be3b
--- /dev/null
+++ b/rails-backend/app/controllers/api/v1/auth_controller.rb
@@ -0,0 +1,109 @@
+require 'net/http'
+require 'bcrypt'
+
+class Api::V1::AuthController < ApplicationController
+ class << self
+ end
+
+ def data
+ if !cookies[:session].nil?
+ puts cookies[:session]
+ #render json: Api::V1::AuthController.user_table[cookies[:session]]
+ result = User.find_by(access_token_digest: cookies[:session])
+ puts "--- RESULT: ---"
+ puts result
+ render json: result
+ else
+ puts "Not logged in"
+ render json: { info: "Not logged in" }, status: 401
+ end
+ end
+ def callback
+ # user logs in through github
+ # github redirects them to this endpoint with the token in the url as query params
+ # we need to use this token to exchange with github for user info(i.e username)
+ #puts "Code: #{params[:code]}" # this is the github token
+ #puts ENV["GITHUB_CLIENT_SECRET"]
+ #puts ENV["GITHUB_CLIENT_ID"]
+ access_token = get_access_token(params[:code])
+ user_data = JSON.parse(get_github_user_data(access_token))
+ #puts "------------------------- USER DATA: ------------------------- "
+ #pp user_data
+ id = user_data['id'].to_s
+ #puts "id: #{id}, at: #{access_token}"
+ access_token_digest = BCrypt::Password.create(access_token)
+ #cookies[:session] = access_token_digest
+ cookies[:session] = {
+ value: access_token_digest,
+ #domain: :all,
+ #same_site: :none,
+ secure: true
+ }
+ #user_params = {
+ # # access_token_digest: hashed_token,
+ # user_data: user_data
+ #}
+ #puts "USER DATA HERE NERD"
+ #puts user_data.class
+ user = User.find_or_create_by(identifier: id)
+ user.user_data = user_data
+ user.access_token_digest = access_token_digest
+ user.user_name = user_data["login"]
+ user.save
+ #redirect_to 'http://localhost:5173/', allow_other_host: true
+ redirect_to "#{ENV['ROOT_DOMAIN']}/closewindow", allow_other_host: true
+ end
+
+ private
+
+ def get_github_user_data(access_token)
+ uri = URI("https://api.github.com/user")
+ headers = { Authorization: "Bearer #{access_token}" }
+ response = Net::HTTP.get(
+ uri,
+ headers
+ )
+ puts "Response Body"
+ puts response
+ #if response.is_a?(Net::HTTPSuccess)
+ #if response.body.nil?
+ result = response
+ if !result["error"].nil?
+ puts "Error: #{result["error"]}"
+ puts response
+ # we had an error
+ # TODO
+ else
+ puts "huh?" if result.nil?
+ return result
+ end
+ #else
+ # puts "Error(body nil)"
+ # something went wrong?
+ # TODO
+ #end
+ end
+
+ def get_access_token(github_user_code)
+ uri = URI("https://github.com/login/oauth/access_token?client_id=#{ENV["GITHUB_CLIENT_ID"]}&client_secret=#{ENV["GITHUB_CLIENT_SECRET"]}&code=#{github_user_code}")
+ #uri = URI('https://github.com/login/oauth/access_token')
+ headers = {Accept: 'application/json'}
+ response = Net::HTTP.post(
+ uri,
+ nil,
+ headers
+ )
+ if response.is_a?(Net::HTTPSuccess)
+ result = JSON.parse(response.body)
+ if !result["error"].nil?
+ # we had an error
+ else
+ return result["access_token"]
+ end
+ else
+ # something went wrong?
+ # TODO
+ end
+ end
+end
+
diff --git a/rails-backend/app/controllers/api/v1/games_controller.rb b/rails-backend/app/controllers/api/v1/games_controller.rb
new file mode 100644
index 0000000..4346244
--- /dev/null
+++ b/rails-backend/app/controllers/api/v1/games_controller.rb
@@ -0,0 +1,167 @@
+require "zip"
+
+class Api::V1::GamesController < ApplicationController
+ #skip_before_action :verify_authenticity_token
+ before_action :allow_iframe, only: [:show_file]
+ def create
+ puts "----- PARAMS PLATFORM TAG ----------"
+ pp params["game"]["platform_tag"]
+ user = User.find_by(access_token_digest: cookies[:session])
+ #user = User.first # temporary for debug
+ if(!user)
+ render json: {session: cookies[:session]}, status: 401
+ else
+ pp params
+
+ @game = user.games.new(game_params.except(:status, :platform_tag))
+ @game.titleSlug = game_params[:title].parameterize
+ @game.status = game_params[:status].to_i
+ if !params["game"]["platform_tag"].nil?
+ params["game"]["platform_tag"].each do |tag|
+ tag_obj = Tag.find_by(tag_type: "platform", name: tag)
+ if tag_obj
+ @game.tags << tag_obj
+ end
+ end
+ end
+
+ @game.save_zip(params[:game][:zip])
+
+ if @game.save
+ render json: @game, status: :created
+ else
+ render json: @game.errors, status: :unprocessable_entity
+ end
+ end
+ end
+
+
+ # list of all games
+ def index
+ game = Game.all.order(created_at: :desc)
+ #render json: game
+ render json: game.to_json(include: [:game_files, :card_img, :char_img, :title_img, :tags])
+ end
+
+ # single game or list of user's games
+ #get 'games/:user/:game', to: 'games#show'
+ #get 'games/:user', to: 'games#show'
+ def show
+ user = User.find_by! user_name: params[:user]
+ if params[:game].nil?
+ # get list of user games
+ games = Game.where(user_id: user.id).order(created_at: :desc)
+ render json: games.to_json(include: [:tags])
+ else
+ game = Game.find_by! user_id: user.id, titleSlug: params[:game]
+ render json: game.to_json(include: [:tags])
+ # get game
+ end
+ end
+
+ # :user/:game/*path/:file
+ def show_file
+ user = User.find_by user_name: params[:user]
+
+ # if no user given then just show all games
+ if(user.nil?)
+ game = Game.all.order(created_at: :desc)
+ render json: game
+ return
+ end
+
+ game = Game.find_by user_id: user.id, titleSlug: params[:game]
+
+ # if no game given then just show all games from that user
+ if(game.nil?)
+ game = Game.all.order(created_at: :desc)
+ render json: game
+ return
+ end
+
+ # format and file is seperated in rails
+ filename = params[:file]
+ if !params[:format].nil?
+ filename = "#{filename}.#{params[:format]}"
+ end
+
+ # if we have no path, make it a blank string
+ # this lets us later match with files that are in the root
+ params[:path] ||= ""
+
+ result = game.game_files.blobs.find_by(filename: filename, filepath: params[:path].delete_suffix('/').delete_prefix('/')) # TODO check if we need to do the prefix/suffix deletion at all
+
+ # we shouldnt need this
+ #result ||= game.game_files.blobs.find_by(filename: filename)
+ if(result.nil?)
+ game = Game.all.order(created_at: :desc)
+ render json: { filename: filename, filepath: params[:path] }
+ #render json: game
+ return
+ end
+
+ format = filename.rpartition('.').last
+ if format == "html"
+ render html: result.download.html_safe
+ elsif format == "js"
+ render js: result.download.html_safe
+ #else
+ # redirect_to url_for(result)
+ #end
+ elsif format == "gz"
+ response.headers['Content-Encoding'] = 'gzip'
+ second_ext = filename.rpartition('.').first.rpartition('.').last
+ if second_ext == 'js'
+ send_data result.download.html_safe, filename: filename, disposition: "inline", type: "application/javascript"
+ elsif second_ext == 'wasm'
+ send_data result.download.html_safe, filename: filename, disposition: "inline", type: "application/wasm"
+ elsif second_ext == 'data'
+ send_data result.download.html_safe, filename: filename, disposition: "inline", type: "application/octet-stream"
+ else
+ send_data result.download.html_safe, filename: filename, disposition: "inline"
+ end
+ else
+ send_data result.download.html_safe, filename: filename, disposition: "inline"
+ end
+ end
+
+ #get 'imggames/:user/:game?type=___', to: 'games#show_img'
+ def show_img
+ user = User.find_by! user_name: params[:user]
+ game = Game.find_by! user_id: user.id, titleSlug: params[:game]
+
+ result = nil;
+ if params[:type] == "char"
+ result = game.char_img.download
+ elsif params[:type] == "title"
+ result = game.title_img.download
+ elsif params[:type] == "card"
+ result = game.card_img.download
+ end
+
+ send_data result, type: 'image/png', disposition: 'inline'
+ end
+
+ private
+
+ def game_params
+ params.require(:game).permit(
+ :title,
+ :description,
+ :github_link,
+ :img_rendering,
+ :status,
+ :order,
+ :card_img,
+ :char_img,
+ :title_img,
+ :zip,
+ :platform_tag
+ #game_files: []
+ )
+ end
+
+ def allow_iframe
+ response.headers.delete('X-Frame-Options')
+ end
+end
diff --git a/rails-backend/app/controllers/api/v1/tags_controller.rb b/rails-backend/app/controllers/api/v1/tags_controller.rb
new file mode 100644
index 0000000..4b31de8
--- /dev/null
+++ b/rails-backend/app/controllers/api/v1/tags_controller.rb
@@ -0,0 +1,15 @@
+class Api::V1::TagsController < ApplicationController
+
+ def index
+ if !params[:tag_type].nil?
+ tag = Tag.where(tag_type: params[:tag_type]).order(name: :asc)
+
+ render json: tag.to_json
+ else
+ tag = Tag.all.order(tag_type: :desc, name: :asc)
+ #render json: tag
+ render json: tag.to_json
+ end
+ end
+
+end
diff --git a/rails-backend/app/controllers/api/v1/users_controller.rb b/rails-backend/app/controllers/api/v1/users_controller.rb
new file mode 100644
index 0000000..ad27ad6
--- /dev/null
+++ b/rails-backend/app/controllers/api/v1/users_controller.rb
@@ -0,0 +1,10 @@
+class Api::V1::UsersController < ApplicationController
+ def index
+ # return list of all users
+ users = User.all.order(created_at: :desc)
+ #render json: users.to_json(only: [:name])
+ #render json: users.to_json(only: [:user])
+ #render json: users.to_json(only: { only: [:name] })
+ render json: users.to_json(include: [games: { only: [:title, :titleSlug] }])
+ end
+end