5秒鐘搭建一個簡單版的restful資源服務器

利用Mins能夠在五秒鐘內(不包括下載文件時間....)搭建一個簡單的restful資源服務器。css

準備

首先安裝Minshtml

brew tap chenhg5/tap && brew install mins

因爲我在mac系統下,因此使用brew安裝。對應系統能夠下載對應二進制執行文件,下載連接爲:https://github.com/chenhg5/mi...mysql

圖片描述

下載好,你能夠選擇將二進制文件放進環境路徑中。須要給mins執行權限:git

chmod +x mins
mv mins /usr/local/bin/

接着須要編寫一個配置文件 config.ini, 內容以下:github

[server]
port = 4006

[database]
addr = localhost
port = 3306
user = root
password = root
database = example

example是對應的mysql資源數據庫,裏面有一個users表。golang

啓動

接着啓動Mins,就完成了。sql

./mins -c ./config.ini

圖片描述

體驗

而後咱們新增一個數據進example的users表:數據庫

curl -X POST \
  http://localhost:4006/users \
  -F name=jack \
  -F sex=0

能夠看到數據庫就新增了一條數據:服務器

圖片描述

而後能夠查出這條數據:restful

curl -X GET http://localhost:4006/users/1
{"code":200, "msg":"ok", "data": {"id":1,"name":"jack","sex":0}}

修改數據

curl -X PUT http://localhost:4006/users/1 -F name=Mick

刪除數據

curl -X DELETE http://localhost:4006/users/1

靜態文件

除了資源的增刪改查,mins還構建了一個靜態文件服務器,經過mins能夠訪問當前路徑下的靜態文件,如html,css,image文件等。

性能

採用的是golang的fasthttp網絡庫開發,性能天然是很棒的。

相關文章
相關標籤/搜索