Peterfei

上主是我的牧者,我实在一无所缺


  • 首页

  • 归档

  • 标签

rails grape 用户登陆API

发表于 2015-10-20   |   分类于 ROR   |  

##新建apiUser表:
1.access_token
2.expires_at
3.user_id
4.active
rails g model api_key access_token:string expires_at:datetime user_id:integer active:boolean

##在建好的migrate 文件中加入索引:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class CreateApiUserKeys < ActiveRecord::Migration
def change
create_table :api_user_keys do |t|
t.string :access_token
t.datetime :expires_at
t.integer :user_id
t.boolean :active
t.timestamps null: false
end
add_index :api_user_keys, ["user_id"], name: "index_api_keys_on_user_id", unique: false
add_index :api_user_keys, ["access_token"], name: "index_api_keys_on_access_token", unique: true
end
end

执行db:migrate rake db:migrate

##在model中生成token

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class ApiUserKey < ActiveRecord::Base
attr_accessible :access_token, :expires_at, :user_id, :active, :application
before_create :generate_access_token
before_create :set_expiration
belongs_to :user
def expired?
DateTime.now >= self.expires_at
end
private
def generate_access_token
begin
self.access_token = SecureRandom.hex
end while self.class.exists?(access_token: access_token)
end
def set_expiration
self.expires_at = DateTime.now+30
end
end

##在Grape中加入:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
helpers do
def authenticate!
error!('Unauthorized. Invalid or expired token.', 401) unless current_user
end
def current_user
# find token. Check if valid.
token = ApiUserKey.where(access_token: params[:token]).first
if token && !token.expired?
@current_user = User.find(token.user_id)
else
false
end
end
end

##Post 和Get :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
resource :auth do
desc "Creates and returns access_token if valid login"
params do
requires :login, type: String, desc: "Username or email address"
requires :password, type: String, desc: "Password"
end
post :login do
if params[:login]
user = User.find_by_col_user_phone params[:login].downcase
end
if user && user.authenticate(params[:password])
key = ApiUserKey.create(user_id: user.id)
{token: key.access_token}
else
error!('用户名或密码错误', 401)
end
end
desc "Returns pong if logged in correctly"
params do
requires :token, type: String, desc: "Access token."
end
get :ping do
authenticate!
{ message: "pong" }
end
end
end

rails mina+puma 部署

发表于 2015-10-19   |   分类于 ROR   |  

出于安全考虑,不要使用 root 帐号运行 web 应用。这里新建一个专门用于部署的用户,例如 deploy 或者其它你喜欢的名字。运行以下命令创建用户:

# useradd -m -s /bin/bash deploy

将用户加入 sudo 群组,以便使用 sudo 命令:
# adduser deploy sudo
为 deploy 用户设置密码:
# passwd deploy
给Linux deploy用户 加入rvm 权限
usermod -a -G rvm deploy
退出当前 SSH 链接,用 deploy 帐号重新登陆。

##加入gem mina
mina init, 生成config/deploy.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require 'mina/rails'
require 'mina/git'
require 'mina/bundler'
require 'mina/rvm'
#服务器地址,是使用ssh的方式登录服务器
set :domain, 'deploy@yourIPServer'
#服务器中项目部署位置
set :deploy_to, '/home/deploy/api'
#git代码仓库
set :repository, 'git@XXX.git'
#git分支
set :branch, 'master'
set :rvm_path, '/usr/local/rvm/bin/rvm'
set :bundle_gemfile, "app/Gemfile"
# 中括号里的文件 会出现在服务器项目附录的shared文件夹中,这里加入了secrets.yml,环境密钥无需跟开发计算机一样
set :shared_paths, ['config/database.yml', 'log', 'config/secrets.yml']
set :rails_env, 'development'
task :environment do
# 如果使用的是rbenv,这么设置,但需确保.rbenv-version(rbenv local 1.9.3-p374)已经存在于你的项目中
invoke :'rvm:use[ruby-2.1.2@default]'
# 如果使用rvm,可以这样加载一个RVM version@gemset
# invoke :'rvm:use[ruby-1.9.3-p374@default]'
end
# 这个块里面的代码表示运行 mina setup时运行的命令
task :setup => :environment do
# 在服务器项目目录的shared中创建log文件夹
queue! %[mkdir -p "#{deploy_to}/#{shared_path}/log"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/log"]
# 在服务器项目目录的shared中创建config文件夹 下同
queue! %[mkdir -p "#{deploy_to}/#{shared_path}/config"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/config"]
queue! %[touch "#{deploy_to}/#{shared_path}/config/database.yml"]
queue! %[touch "#{deploy_to}/#{shared_path}/config/secrets.yml"]
# puma.rb 配置puma必须得文件夹及文件
queue! %[mkdir -p "#{deploy_to}/shared/tmp/pids"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/tmp/pids"]
queue! %[mkdir -p "#{deploy_to}/shared/tmp/sockets"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/tmp/sockets"]
queue! %[touch "#{deploy_to}/shared/config/puma.rb"]
queue %[echo "-----> Be sure to edit 'shared/config/puma.rb'."]
# tmp/sockets/puma.state
queue! %[touch "#{deploy_to}/shared/tmp/sockets/puma.state"]
queue %[echo "-----> Be sure to edit 'shared/tmp/sockets/puma.state'."]
# log/puma.stdout.log
queue! %[touch "#{deploy_to}/shared/log/puma.stdout.log"]
queue %[echo "-----> Be sure to edit 'shared/log/puma.stdout.log'."]
# log/puma.stdout.log
queue! %[touch "#{deploy_to}/shared/log/puma.stderr.log"]
queue %[echo "-----> Be sure to edit 'shared/log/puma.stderr.log'."]
queue %[echo "-----> Be sure to edit '#{deploy_to}/#{shared_path}/config/database.yml'."]
end
#这个代码块表示运行 mina deploy时执行的命令
desc "Deploys the current version to the server."
task :deploy => :environment do
to :before_hook do
end
deploy do
#重新拉git服务器上的最新版本,即使没有改变
invoke :'git:clone'
#重新设定shared_path位置
invoke :'deploy:link_shared_paths'
invoke :'bundle:install'
# invoke :'rails:db_migrate'
invoke :'rails:assets_precompile'
invoke :'deploy:cleanup'
to :launch do
queue "mkdir -p #{deploy_to}/#{current_path}/tmp/"
# queue "chown -R www-data #{deploy_to}"
queue "touch #{deploy_to}/#{current_path}/tmp/restart.txt"
end
end
end

这样一来mina的基本配置就完成,接下来只要将你开发环境的项目上传到git服务器,然后运行下面的命令就完成了

mina deploy

##puma配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env puma
#rails的运行环境
# environment 'production'
environment 'development'
threads 2, 64
workers 4
#项目名
app_name = "api"
#项目路径
application_path = "/home/deploy/#{app_name}"
#这里一定要配置为项目路径下地current
directory "#{application_path}/current"
#下面都是 puma的配置项
pidfile "#{application_path}/shared/tmp/pids/puma.pid"
state_path "#{application_path}/shared/tmp/sockets/puma.state"
stdout_redirect "#{application_path}/shared/log/puma.stdout.log", "#{application_path}/shared/log/puma.stderr.log"
bind "unix://#{application_path}/shared/tmp/sockets/#{app_name}.sock"
activate_control_app "unix://#{application_path}/shared/tmp/sockets/pumactl.sock"
#后台运行
daemonize true
on_restart do
puts 'On restart...'
end
preload_app!

##启动puma
进入/yourprj/config
pumactl -F puma.rb start

##Nginx 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
#include /etc/nginx/conf.d/*.conf;
upstream deploy {
server unix:///var/www/ruby_sample/shared/tmp/sockets/ruby_sample.sock;
}
server {
listen 80;
server_name your.server.domain.ip; # change to match your URL
root /var/www/ruby_sample/current/public; # I assume your app is located at this location
location / {
proxy_pass http://deploy; # match the name of upstream directive which is defined above
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~* ^/assets/ {
# Per RFC2616 - 1 year maximum expiry
expires 1y;
add_header Cache-Control public;
# Some browsers still send conditional-GET requests if there's a
# Last-Modified header or an ETag header even if they haven't
# reached the expiry date sent in the Expires header.
add_header Last-Modified "";
add_header ETag "";
break;
}
}
}

接下里只需要重启nginx服务器,整个rails的环境就搭建完成了
nginx -s reload

ionic android 打包

发表于 2015-08-05   |   分类于 前端   |  

生成证书

`keytool -genkey -v -keystore **-release-key.keystore -alias alias_name -keyalg RSA -keysize 2048 -validity 10000`

生成release 版本:

cordova build --release android

jarsinger:

jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore ezu110-release-key.keystore /Users/liufei/ionic/yongche/platforms/android/build/outputs/apk/android-release-unsigned.apk  alias_name

生成apk:

/Library/Android/android-sdk-macosx/build-tools/22.0.1/zipalign -v 4 /Users/liufei/ionic/yongche/platforms/android/build/outputs/apk/android-release-unsigned.apk ezu110.apk

ionic 发送POST请求

发表于 2015-07-22   |   分类于 Ionic   |  

今天写一个POST请求给远程API,FORM数据一直不能发送,最终用如下factory解决问题:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
.factory('LoginService', ['$http', function($http) {
var users = [];
return {
getUsers: function(loginData) {
var username = loginData.username,
password = loginData.password;
return $http({
method: 'POST',
url: 'http://172.16.100.222/zf/app/login.php?act=login',
data: $.param(loginData),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).success(function(response) {
// debugger
users = response;
return users;
});
}
}
}])

未命名

发表于 2015-07-15   |   分类于 Linux   |  

1、复制远程服务器的文件到本地:
scp -P888 root@120.18.50.33:/data/ha97.zip /home/
2、复制远程服务器的目录到本地:
scp -vrp -P888 root@120.18.50.33:/data/ha97/ /home/
3、复制本地的文件到远程服务器:
scp -P888 /home/ha97.zip root@120.18.50.33:/data/
4、复制本地的目录到远程服务器:
scp -vrp -P888 /home/ root@120.18.50.33:/data/rp

1…111213…16
peterfei

peterfei

peterfei|技术|上主是我的牧者

79 日志
14 分类
62 标签
RSS
github
© 2025 peterfei
由 Hexo 强力驱动
主题 - NexT.Mist