一个练习项目,好玩的bbs-ruby-ramaze
代码:
#!ruby #coding=utf-8 require "mysql" require "json" require 'ramaze' 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.to_s.sub(" +0800", "") myinfo['sessionId'] = sessionId end return myinfo end class MyIndex < Ramaze::Controller map '/' def index response.header['Server'] = "ruby-ramaze" return "此站接口使用ruby-ramaze实现,<a href='api.html' target='_blank'>接口列表</a>" end end class MyUser < Ramaze::Controller map '/user' def register response.header['Server'] = "ruby-ramaze" conn = Mysql.connect('mysql://root:123456@127.0.0.1:3306/my_bbs?charset=utf8mb4') username = request.params['username'] nickname = request.params['nickname'] nickname = nickname.force_encoding("UTF-8") password = request.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 return retdata.to_json end def login response.header['Server'] = "ruby-ramaze" conn = Mysql.connect('mysql://root:123456@127.0.0.1:3306/my_bbs?charset=utf8mb4') secretKey = 'saacac3423@21212' username = request.params['username'] password = request.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.to_s.sub(" +0800", "") 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 return retdata.to_json end def logout response.header['Server'] = "ruby-ramaze" conn = Mysql.connect('mysql://root:123456@127.0.0.1:3306/my_bbs?charset=utf8mb4') sessionId = request.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 return retdata.to_json end def getuserinfo response.header['Server'] = "ruby-ramaze" conn = Mysql.connect('mysql://root:123456@127.0.0.1:3306/my_bbs?charset=utf8mb4') sessionId = request.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 return retdata.to_json end end class MyPost < Ramaze::Controller map '/post' def list response.header['Server'] = "ruby-ramaze" conn = Mysql.connect('mysql://root:123456@127.0.0.1:3306/my_bbs?charset=utf8mb4') pagesize = 20 page = request.params['page'].to_i keyword = request.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.to_s.sub(" +0800", "") mylist.append(myinfo) end mydata['data'] = mylist retdata = {} retdata['code'] = 0 retdata['msg'] = '' retdata['data'] = mydata return retdata.to_json end def detail response.header['Server'] = "ruby-ramaze" conn = Mysql.connect('mysql://root:123456@127.0.0.1:3306/my_bbs?charset=utf8mb4') id = request.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 return retdata.to_json end def add response.header['Server'] = "ruby-ramaze" conn = Mysql.connect('mysql://root:123456@127.0.0.1:3306/my_bbs?charset=utf8mb4') title = request.params['title'] content = request.params['content'] sessionId = request.params['sessionId'] userinfo = getloginuserinfo(sessionId) retdata = {} if userinfo.empty? retdata['code'] = 1 retdata['msg'] = '请先登录' retdata['data'] = '' else title = title.force_encoding("UTF-8") content = content.force_encoding("UTF-8") 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 return retdata.to_json end def edit response.header['Server'] = "ruby-ramaze" conn = Mysql.connect('mysql://root:123456@127.0.0.1:3306/my_bbs?charset=utf8mb4') id = request.params['id'].to_i title = request.params['title'] content = request.params['content'] sessionId = request.params['sessionId'] userinfo = getloginuserinfo(sessionId) retdata = {} if userinfo.empty? retdata['code'] = 1 retdata['msg'] = '请先登录' retdata['data'] = '' else title = title.force_encoding("UTF-8") content = content.force_encoding("UTF-8") 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 return retdata.to_json end def delete response.header['Server'] = "ruby-ramaze" conn = Mysql.connect('mysql://root:123456@127.0.0.1:3306/my_bbs?charset=utf8mb4') id = request.params['id'].to_i sessionId = request.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 return retdata.to_json end end class MyReply < Ramaze::Controller map '/reply' def list response.header['Server'] = "ruby-ramaze" conn = Mysql.connect('mysql://root:123456@127.0.0.1:3306/my_bbs?charset=utf8mb4') pagesize = 20 page = request.params['page'].to_i contentId = request.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 mylist.append(myinfo) end mydata['data'] = mylist retdata = {} retdata['code'] = 0 retdata['msg'] = '' retdata['data'] = mydata return retdata.to_json end def detail response.header['Server'] = "ruby-ramaze" conn = Mysql.connect('mysql://root:123456@127.0.0.1:3306/my_bbs?charset=utf8mb4') id = request.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 return retdata.to_json end def add response.header['Server'] = "ruby-ramaze" conn = Mysql.connect('mysql://root:123456@127.0.0.1:3306/my_bbs?charset=utf8mb4') contentId = request.params['contentId'].to_i content = request.params['content'] sessionId = request.params['sessionId'] userinfo = getloginuserinfo(sessionId) retdata = {} if userinfo.empty? retdata['code'] = 1 retdata['msg'] = '请先登录' retdata['data'] = '' else content = content.force_encoding("UTF-8") 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 return retdata.to_json end def edit response.header['Server'] = "ruby-ramaze" conn = Mysql.connect('mysql://root:123456@127.0.0.1:3306/my_bbs?charset=utf8mb4') id = request.params['id'].to_i content = request.params['content'] sessionId = request.params['sessionId'] userinfo = getloginuserinfo(sessionId) retdata = {} if userinfo.empty? retdata['code'] = 1 retdata['msg'] = '请先登录' retdata['data'] = '' else content = content.force_encoding("UTF-8") 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 return retdata.to_json end def delete response.header['Server'] = "ruby-ramaze" conn = Mysql.connect('mysql://root:123456@127.0.0.1:3306/my_bbs?charset=utf8mb4') id = request.params['id'].to_i sessionId = request.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 return retdata.to_json end end Ramaze.start port:1105
输出:
W, [2024-06-26T11:21:52.900397 #8316] WARN -- : No explicit root folder found, assuming it is D:/workspace/studys/study_bbs D, [2024-06-26T11:21:52.900916 #8316] DEBUG -- : Using webrick I, [2024-06-26T11:21:53.094192 #8316] INFO -- : WEBrick 1.8.1 I, [2024-06-26T11:21:53.094274 #8316] INFO -- : ruby 2.7.6 (2022-04-12) [x64-mingw32] D, [2024-06-26T11:21:53.098437 #8316] DEBUG -- : Rackup::Handler::WEBrick is mounted on /. I, [2024-06-26T11:21:53.098628 #8316] INFO -- : WEBrick::HTTPServer#start: pid=8316 port=1105
本文来自博客园,作者:河北大学-徐小波,转载请注明原文链接:https://www.cnblogs.com/xuxiaobo/p/18268653