# FHIR 教育訓練相關資源
[TOC]
## FHIR 概述
### 文件
+ FHIR 官方網站:https://www.hl7.org/fhir/
+ FHIR 中文簡介:http://www.tpms.org.tw/2018/01/29/fhir/
### 快速掃描-請搭配簡報服用
+ FHIR 快速掃描:https://www.slideshare.net/lorexyang/fhir-153713142
## FHIR Server 安裝與使用
### 搭配簡報服用
https://www.slideshare.net/lorexyang/fhir-server
### 測試伺服器列表
+ UHN 公開伺服器(目前最多人用): http://fhirtest.uhn.ca
+ 臺灣公開測試伺服器(較推薦): https://hapi.fhir.tw
+ 其他伺服器列表: http://wiki.hl7.org/index.php?title=Publicly_Available_FHIR_Servers_for_testing
### HAPI FHIR Repository
+ 原 Github Repo:https://github.com/jamesagnew/hapi-fhir
+ MySQL 版 Repo:http://gitlab.sita.tech/medic/hapifhir-mysql
### MariaDB 建置與設定
```bash=
$ sudo yum install mariadb mariadb-server
$ sudo systemctl start mariadb
$ sudo systemctl enable mariadb
$ sudo systemctl status mariadb
$ mysql -h <hostname> -u <username> -p
```
```sql=
CREATE DATABASE <DB_NAME> CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE USER '<USERNAME>'@'<FHIR_SERVER_IP>' IDENTIFIED BY '<PASSWORD>';
GRANT ALL PRIVILEGES ON <DB_NAME>.* TO '<USERNAME>'@'<FHIR_SERVER_IP>';
FLUSH PRIVILEGES;
exit;
```
### Docker CE 建置
```bash=
$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2
$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
$ sudo yum update
$ sudo yum install docker-ce
$ sudo systemctl start docker
$ sudo systemctl enable docker
```
測試
```bash=
$ sudo docker run hello-world
```
### FHIR 建置步驟
安裝必要的工具
```bash=
$ sudo yum install epel-release
$ sudo yum update
$ sudo yum install wget
$ sudo wget http://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo -O /etc/yum.repos.d/epel-apache-maven.repo
$ sudo yum install git vim apache-maven
```
從 Gitlab 抓取 Repository 到本機
```bash=
$ git clone http://gitlab.sita.tech/lorex/hapifhir-mysql.git
$ cd hapifhir-mysql
```
### 設定伺服器連線資訊
```bash=
$ vim ./configuration.properties
```
```yaml=
sqlUrl=<MySQL IP>
sqlPort=<MySQL Port, defaults to 3306>
sqlDb=<MySQL DB name>
sqlUser=<MySQL username>
sqlPass=<MySQL password>
```
### 建置 docker image 並跑服務
複製設定檔
```bash=
$ cp ./configuration.properties ./src/main/resources/configuration.properties;
```
編譯檔案
```bash=
$ mvn clean package;
```
建置 Docker image
```bash=
$ docker build -t hapi-fhir/hapi-fhir-mysql
```
把服務跑起來並把 Port 映射出來
```bash=
$ docker run -d -p 8080:8080 hapi-fhir/hapi-fhir-mysql
```
### 建置 nginx reverse proxy
```bash=
$ sudo yum install nginx
$ systemctl start nginx
$ systemctl enable nginx
$ emacs /etc/nginx/conf.d/fhir.conf
```
設定檔內容
```bson=
server {
listen 80;
listen [::]:80;
server_name <YOUR DOMAIN>; # 設定你的 domain name
rewrite ^(.*) https://$host$1 permanent; # 將 HTTP 連線導向到 HTTPS
}
server {
listen 443 ssl;
listen [::]:443 ssl; # 監聽 HTTPS Port
server_name <YOUR DOMAIN>;
location / {
proxy_pass http://path.to.server:port;
proxy_redirect off;
proxy_set_header Host $host;
}
ssl_certificate /path/to/ssl.crt; # 填入 SSL Certificate 路徑
ssl_certificate_key /path/to/ssl.key; # 填入 SSL Key 路徑
}
```
## REST API 應用
### 請搭配簡報服用
https://www.slideshare.net/lorexyang/fhir-rest-api-123996154
### 文件參考
+ JSON 規範(RFC7159文件):https://tools.ietf.org/html/rfc7159
+ XML 規範(RFC4825文件):https://tools.ietf.org/html/rfc4825
+ Postman:https://www.getpostman.com/
+ REST API 文件:https://www.restapitutorial.com/
### 範例請求
取得所有 Patient 資料:GET https://hapi.fhir.tw/baseDstu3/Patient
取得單一 Patient 資料:GET https://hapi.fhir.tw/baseDstu3/Patient/1
新增一筆 Patient 資料:POST https://hapi.fhir.tw/baseDstu3/Patient
+ Payload

編輯剛剛的 Patient 資料:PUT https://hapi.fhir.tw/baseDstu3/Patient
+ Payload

刪除剛剛新增的 Patient 資料:DELETE https://hapi.fhir.tw/baseDstu3/Patient/265
### 範例病人資料(同時也是 payload.json 內容)
```json=
{
"resourceType": "Patient",
"name": {
"use": "official",
"text": "王大明",
"family": "王",
"given": "大明"
},
"gender": "male",
"telecom": {
"system": "phone",
"value": "0912345678",
"use": "mobile"
},
"address": {
"use": "home",
"type": "physical",
"text": "高雄市小港區大馬路999號",
"line": "大馬路999號",
"city": "高雄市",
"district": "小港區",
"postalCode": "812",
"country": "TW"
},
"active": true,
"birthDate": "1995-01-01"
}
```
### 使用 node.js 存取 FHIR Server
+ 創建空資料夾,然後 npm init
```bash=
$ npm init
```
+ 安裝 dependencies
```bash=
$ npm i -S axios
```
+ 新增 payload.json,先把 payload 寫進去,省 code 空間
+ 新增 main.js,開始寫
+ 執行程式
```bash=
$ node main.js
```
### main.js 範例程式碼
```javascript=
const axios = require('axios'); // 引入 axios
const util = require('util');
const payload = require('./payload.json'); // 引入 payload
const baseURL = "https://hapi.fhir.tw/baseDstu3"; // 設定 Base URL
axios.post(`${baseURL}/Patient`, payload) // 傳送 POST 請求
.then(res => {
console.log("傳送成功" + util.inspect(res.data, {
depth: null
}));
}).catch(err => {
console.log("發生錯誤" + util.inspect(err, {
depth: null
}));
});
```