一个练习项目,好玩的bbs-ruby-sinatra
代码:
#!ruby #coding=utf-8 require "sinatra" require "sinatra/base" require "mysql" require "json" =begin gem sources -l gem sources --remove https://rubygems.org/ gem sources --remove https://mirrors.ustc.edu.cn/rubygems/ gem sources --remove https://gems.ruby-china.com/ gem sources -a https://rubygems.org/ gem sources -a https://mirrors.ustc.edu.cn/rubygems/ gem sources -a https://gems.ruby-china.com/ gem install ruby-mysql =end def getloginuserinfo(sessionId = '') conn = Mysql.connect('mysql://root:123456@127.0.0.1:3306/my_bbs?charset=utf8mb4') myinfo = {} items = conn.query("select id,username,nickname,addTime,sessionId from user where sessionId='#{sessionId}'") items.each do |id,username,nickname,addTime,sessionId| myinfo['id'] = id myinfo['username'] = username myinfo['nickname'] = nickname myinfo['addTime'] = addTime myinfo['sessionId'] = sessionId end return myinfo end class MyApp < Sinatra::Base enable :sessions set :port, 1084 pagesize = 20 secretKey = 'saacac3423@21212' conn = Mysql.connect('mysql://root:123456@127.0.0.1:3306/my_bbs?charset=utf8mb4') get '/' do headers 'Server' => 'ruby-sinatra' str = "此站接口使用ruby-sinatra实现,<a href='api.html' target='_blank'>接口列表</a>" end get '/user/register' do headers 'Server' => 'ruby-sinatra' username = params['username'] nickname = params['nickname'] password = params['password'] passwordMd5 = Digest::MD5.hexdigest(password) myinfo = {} items = conn.query("select id,username,nickname,addTime from user where username='#{username}'") items.each do |id,username,nickname,addTime| myinfo['id'] = id myinfo['username'] = username myinfo['nickname'] = nickname myinfo['addTime'] = addTime end retdata = {} if myinfo.empty? conn.query("insert into user(username, password, nickname) value('#{username}', '#{passwordMd5}', '#{nickname}')") retdata['code'] = 0 retdata['msg'] = '' retdata['data'] = conn.insert_id else retdata['code'] = 1 retdata['msg'] = '用户名已经存在' retdata['data'] = '' end retdata.to_json end get '/user/login' do headers 'Server' => 'ruby-sinatra' username = params['username'] password = params['password'] passwordMd5 = Digest::MD5.hexdigest(password) myinfo = {} items = conn.query("select id,username,nickname,addTime from user where username='#{username}' and password='#{passwordMd5}'") items.each do |id,username,nickname,addTime| myinfo['id'] = id myinfo['username'] = username myinfo['nickname'] = nickname myinfo['addTime'] = addTime end retdata = {} if myinfo.empty? retdata['code'] = 1 retdata['msg'] = '用户名或者密码错误' retdata['data'] = '' else sessionId = Digest::MD5.hexdigest(secretKey + myinfo['id'].to_s + myinfo['addTime']) conn.query("update user set sessionId='#{sessionId}' where id=#{myinfo['id']}") myinfo['sessionId'] = sessionId retdata['code'] = 0 retdata['msg'] = '' retdata['data'] = myinfo end retdata.to_json end get '/user/logout' do headers 'Server' => 'ruby-sinatra' sessionId = params['sessionId'] myinfo = {} items = conn.query("select id,username,nickname,addTime,sessionId from user where sessionId='#{sessionId}'") items.each do |id,username,nickname,addTime,sessionId| myinfo['id'] = id myinfo['username'] = username myinfo['nickname'] = nickname myinfo['addTime'] = addTime myinfo['sessionId'] = sessionId end if myinfo conn.query("update user set sessionId='' where sessionId='#{sessionId}'") myinfo['sessionId'] = '' end retdata = {} retdata['code'] = 0 retdata['msg'] = '' retdata['data'] = myinfo retdata.to_json end get '/user/getuserinfo' do headers 'Server' => 'ruby-sinatra' sessionId = params['sessionId'] myinfo = {} items = conn.query("select id,username,nickname,addTime,sessionId from user where sessionId='#{sessionId}'") items.each do |id,username,nickname,addTime,sessionId| myinfo['id'] = id myinfo['username'] = username myinfo['nickname'] = nickname myinfo['addTime'] = addTime myinfo['sessionId'] = sessionId end retdata = {} retdata['code'] = 0 retdata['msg'] = '' retdata['data'] = myinfo retdata.to_json end get '/post/list' do headers 'Server' => 'ruby-sinatra' content_type :json page = params['page'].to_i keyword = params['keyword'] if page <= 0 page = 1 end addsql = " isDel=0 " if keyword != "" addsql = " isDel=0 and title like '%#{keyword}%' " end start = (page - 1) * pagesize mydata = {} items = conn.query("select count(1) as count from content where #{addsql}") items.each do |count| #mydata['count'] = count[0].to_i mydata['totalpage'] = (count[0].to_f / pagesize).ceil() end mylist = [] items = conn.query("select id,title,userId,userNickename,replyNum,updateTime from content where #{addsql} order by updateTime desc limit #{start},#{pagesize}") items.each do |id,title,userId,userNickename,replyNum,updateTime| myinfo = {} myinfo['id'] = id myinfo['title'] = title myinfo['userId'] = userId myinfo['userNickename'] = userNickename myinfo['replyNum'] = replyNum myinfo['updateTime'] = updateTime.gsub(" +0800", "") mylist.append(myinfo) end mydata['data'] = mylist retdata = {} retdata['code'] = 0 retdata['msg'] = '' retdata['data'] = mydata retdata.to_json end get '/post/detail' do headers 'Server' => 'ruby-sinatra' id = params['id'].to_i myinfo = {} items = conn.query("select id,title,content,userId,userNickename,replyNum,updateTime from content where isDel=0 and id=#{id}") items.each do |id,title,content,userId,userNickename,replyNum,updateTime| myinfo['id'] = id myinfo['title'] = title myinfo['content'] = content myinfo['userId'] = userId myinfo['userNickename'] = userNickename myinfo['replyNum'] = replyNum myinfo['updateTime'] = updateTime end retdata = {} retdata['code'] = 0 retdata['msg'] = '' retdata['data'] = myinfo retdata.to_json end get '/post/add' do headers 'Server' => 'ruby-sinatra' title = params['title'] content = params['content'] sessionId = params['sessionId'] userinfo = getloginuserinfo(sessionId) retdata = {} if userinfo.empty? retdata['code'] = 1 retdata['msg'] = '请先登录' retdata['data'] = '' else sql = "insert into content(title, content, userId, userNickename) value('#{title}', '#{content}', #{userinfo['id']}, '#{userinfo['nickname']}')" conn.query(sql) retdata['code'] = 0 retdata['msg'] = '' retdata['data'] = conn.insert_id end retdata.to_json end get '/post/edit' do headers 'Server' => 'ruby-sinatra' id = params['id'].to_i title = params['title'] content = params['content'] sessionId = params['sessionId'] userinfo = getloginuserinfo(sessionId) retdata = {} if userinfo.empty? retdata['code'] = 1 retdata['msg'] = '请先登录' retdata['data'] = '' else conn.query("update content set title='#{title}',content='#{content}',userId=#{userinfo['id']},userNickename='#{userinfo['nickname']}' where id=#{id} and userId=#{userinfo['id']}") retdata['code'] = 0 retdata['msg'] = '' retdata['data'] = '' end retdata.to_json end get '/post/delete' do headers 'Server' => 'ruby-sinatra' id = params['id'].to_i sessionId = params['sessionId'] userinfo = getloginuserinfo(sessionId) retdata = {} if userinfo.empty? retdata['code'] = 1 retdata['msg'] = '请先登录' retdata['data'] = '' else conn.query("update content set isDel=1 where id=#{id} and userId=#{userinfo['id']}") retdata['code'] = 0 retdata['msg'] = '' retdata['data'] = '' end retdata.to_json end get '/reply/list' do headers 'Server' => 'ruby-sinatra' content_type :json page = params['page'].to_i contentId = params['contentId'].to_i if page <= 0 page = 1 end start = (page - 1) * pagesize mydata = {} items = conn.query("select count(1) as count from reply where isDel=0 and contentId=#{contentId}") items.each do |count| #mydata['count'] = count[0].to_i mydata['totalpage'] = (count[0].to_f / pagesize).ceil() end mylist = [] items = conn.query("select id,content,replyUserId,replyUserNickename,addTime from reply where isDel=0 and contentId=#{contentId} order by id asc limit #{start},#{pagesize}") items.each do |id,content,replyUserId,replyUserNickename,addTime| myinfo = {} myinfo['id'] = id myinfo['content'] = content myinfo['replyUserId'] = replyUserId myinfo['replyUserNickename'] = replyUserNickename myinfo['addTime'] = addTime.gsub(" +0800", "") mylist.append(myinfo) end mydata['data'] = mylist retdata = {} retdata['code'] = 0 retdata['msg'] = '' retdata['data'] = mydata retdata.to_json end get '/reply/detail' do headers 'Server' => 'ruby-sinatra' id = params['id'].to_i myinfo = {} items = conn.query("select id,content,replyUserId,replyUserNickename,addTime from reply where isDel=0 and id=#{id}") items.each do |id,content,replyUserId,replyUserNickename,addTime| myinfo['id'] = id myinfo['content'] = content myinfo['replyUserId'] = replyUserId myinfo['replyUserNickename'] = replyUserNickename myinfo['addTime'] = addTime end retdata = {} retdata['code'] = 0 retdata['msg'] = '' retdata['data'] = myinfo retdata.to_json end get '/reply/add' do headers 'Server' => 'ruby-sinatra' contentId = params['contentId'].to_i content = params['content'] sessionId = params['sessionId'] userinfo = getloginuserinfo(sessionId) retdata = {} if userinfo.empty? retdata['code'] = 1 retdata['msg'] = '请先登录' retdata['data'] = '' else conn.query("update content set replyNum=replyNum+1 where id=#{contentId}") conn.query("insert into reply(contentId, content, replyUserId, replyUserNickename) value(#{contentId}, '#{content}', #{userinfo['id']}, '#{userinfo['nickname']}')") retdata['code'] = 0 retdata['msg'] = '' retdata['data'] = conn.insert_id end retdata.to_json end get '/reply/edit' do headers 'Server' => 'ruby-sinatra' id = params['id'].to_i content = params['content'] sessionId = params['sessionId'] userinfo = getloginuserinfo(sessionId) retdata = {} if userinfo.empty? retdata['code'] = 1 retdata['msg'] = '请先登录' retdata['data'] = '' else conn.query("update reply set content='#{content}',replyUserId=#{userinfo['id']},replyUserNickename='#{userinfo['nickname']}' where id=#{id} and replyUserId=#{userinfo['id']}") retdata['code'] = 0 retdata['msg'] = '' retdata['data'] = '' end retdata.to_json end get '/reply/delete' do headers 'Server' => 'ruby-sinatra' id = params['id'].to_i sessionId = params['sessionId'] userinfo = getloginuserinfo(sessionId) retdata = {} if userinfo.empty? retdata['code'] = 1 retdata['msg'] = '请先登录' retdata['data'] = '' else myinfo = {} items = conn.query("select id,content,replyUserId,replyUserNickename,addTime,contentId from reply where isDel=0 and id=#{id}") items.each do |id,content,replyUserId,replyUserNickename,addTime,contentId| myinfo['id'] = id myinfo['content'] = content myinfo['replyUserId'] = replyUserId myinfo['replyUserNickename'] = replyUserNickename myinfo['addTime'] = addTime myinfo['contentId'] = contentId end if myinfo.empty? retdata['code'] = 1 retdata['msg'] = '回复不存在' retdata['data'] = '' else conn.query("update content set replyNum=replyNum-1 where id=#{myinfo['contentId']}") conn.query("update reply set isDel=1 where id=#{id} and replyUserId=#{userinfo['id']}") retdata['code'] = 0 retdata['msg'] = '' retdata['data'] = '' end end retdata.to_json end end # 启动应用 MyApp.run!
运行输出:
== Sinatra (v3.0.1) has taken the stage on 1084 for development with backup from Puma *** SIGUSR2 not implemented, signal based restart unavailable! *** SIGUSR1 not implemented, signal based restart unavailable! *** SIGHUP not implemented, signal based logs reopening unavailable! Puma starting in single mode... * Puma version: 6.0.0 (ruby 2.7.6-p219) ("Sunflower") * Min threads: 0 * Max threads: 5 * Environment: development * PID: 6808 * Listening on http://[::1]:1084 * Listening on http://127.0.0.1:1084 Use Ctrl-C to stop
本文来自博客园,作者:河北大学-徐小波,转载请注明原文链接:https://www.cnblogs.com/xuxiaobo/p/18268649