Skip to content

Commit

Permalink
converted to web app, added docker, makefile, and run.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
Jmainguy committed Apr 16, 2018
1 parent 4d29bf5 commit 02fbca5
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 23 deletions.
10 changes: 10 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM centos:latest
ADD Sohre.repo /etc/yum.repos.d/
RUN yum install -y patient_csv_to_xml-0.1-1.el7.centos && \
yum clean all
RUN chgrp -R 0 /opt/patient_csv_to_xml \
&& chmod -R g+rwX /opt/patient_csv_to_xml
WORKDIR /opt/patient_csv_to_xml
USER patient_csv_to_xml
EXPOSE 8000
CMD ["/opt/patient_csv_to_xml/patient_csv_to_xml"]
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This expects a few requirements
# one, that https://github.com/Jmainguy/docker_rpmbuild is cloned into ~/Github/docker_rpmbuild
# two, that docker is installed and running
# three, that ~/Github/docker_rpmbuild/dockerbuild/build.sh centos7 has been run
rpm:
@tar -czvf ~/Github/docker_rpmbuild/rpmbuild/SOURCES/patient_csv_to_xml.tar.gz ../patient_csv_to_xml
@cp patient_csv_to_xml.spec ~/Github/docker_rpmbuild/rpmbuild/SPECS/patient_csv_to_xml.spec
@cd ~/Github/docker_rpmbuild/; ./run.sh centos7 patient_csv_to_xml
@ls -ltrh ~/Github/docker_rpmbuild/rpmbuild/RPMS/x86_64/patient_csv_to_xml*
4 changes: 4 additions & 0 deletions Sohre.repo
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[sohre]
name=Sohre-$releasever
gpgcheck=0
baseurl=https://pulp.soh.re/pulp/repos/sohre/el$releasever/
51 changes: 28 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,52 @@ package main

import (
"encoding/csv"
"os"
"encoding/xml"
"fmt"
"io"
"net/http"
"mime/multipart"
"bytes"
"fmt"
"encoding/xml"
)

func main() {
func hello(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello world!")
}

// Csv to read from
filename := "/tmp/export.csv"
// XML to write to
writefilename := "/tmp/patients.xml"
func convertCsvToXml(f multipart.File) (buffer *bytes.Buffer){
// Root of our xml doc
v := &Import{}

// Open CSV file
f, err := os.Open(filename)
if err != nil {
panic(err)
}
defer f.Close()

// Read File into a Variable
// Read CSV File into a Variable
lines, err := csv.NewReader(f).ReadAll()
if err != nil {
panic(err)
}

// Loop through lines & turn into object
// Loop through CSV lines & turn into object
for _, line := range lines {
addPatient(v, line)
}

file, _ := os.Create(writefilename)

xmlWriter := io.Writer(file)

enc := xml.NewEncoder(xmlWriter)
var buf bytes.Buffer
enc := xml.NewEncoder(&buf)
enc.Indent("", " ")
if err := enc.Encode(v); err != nil {

err = enc.Encode(&v)
if err != nil {
fmt.Printf("error: %v\n", err)
}

f.Close()
buffer = &buf
return buffer
}


func main() {

http.HandleFunc("/", upload)

http.ListenAndServe(":8000", nil)

}
43 changes: 43 additions & 0 deletions patient_csv_to_xml.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
%define _unpackaged_files_terminate_build 0
%define debug_package %{nil}
Name: patient_csv_to_xml
Version: 0.1
Release: 1%{?dist}
Summary: A golang http server to convert generations csv to ez claim xml

License: GPLv2
URL: https://github.com/Jmainguy/patient_csv_to_xml
Source0: patient_csv_to_xml.tar.gz

%description
A golang http server to convert generations csv to ez claim xml

%prep
%setup -q -n patient_csv_to_xml
%build
export GOPATH=/usr/src/go
/usr/bin/go build
%install
mkdir -p $RPM_BUILD_ROOT/opt/patient_csv_to_xml
install -m 0755 $RPM_BUILD_DIR/patient_csv_to_xml/patient_csv_to_xml %{buildroot}/opt/patient_csv_to_xml/
install -m 0755 $RPM_BUILD_DIR/patient_csv_to_xml/upload.gtpl %{buildroot}/opt/patient_csv_to_xml/

%files
/opt/patient_csv_to_xml/patient_csv_to_xml
/opt/patient_csv_to_xml/upload.gtpl
%dir /opt/patient_csv_to_xml
%doc

%pre
getent group patient_csv_to_xml >/dev/null || groupadd -r patient_csv_to_xml
getent passwd patient_csv_to_xml >/dev/null || \
useradd -r -g patient_csv_to_xml -d /opt/patient_csv_to_xml -s /sbin/nologin \
-c "User to run patient_csv_to_xml" patient_csv_to_xml
exit 0
%post
chown -R patient_csv_to_xml:patient_csv_to_xml /opt/patient_csv_to_xml

%changelog
* Mon Apr 16 2018 Jonathan Mainguy <jon@soh.re> - 0.1-1
- Initial release

2 changes: 2 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
docker run --name patient_csv_to_xml --restart always -p 8000:8000 -d patient_csv_to_xml
16 changes: 16 additions & 0 deletions upload.gtpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<html>
<center>
<head>
<title>Upload CSV, Download XML</title>
</head>
<body>
<h1>Upload a CSV file, exported from generations</h1>
<h2>You will get a XML file back, import that into EZ Claim<h2>
<form enctype="multipart/form-data" action="http://localhost:8000/upload" method="post">
<input type="file" name="uploadfile" />
<input type="hidden" name="token" value="{{.}}"/>
<input type="submit" value="upload" />
</form>
</body>
</center>
</html>
36 changes: 36 additions & 0 deletions webFuncs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"fmt"
"net/http"
"io"
"crypto/md5"
"time"
"strconv"
"html/template"
)

func upload(w http.ResponseWriter, r *http.Request) {
fmt.Println("method:", r.Method)
if r.Method == "GET" {
crutime := time.Now().Unix()
h := md5.New()
io.WriteString(h, strconv.FormatInt(crutime, 10))
token := fmt.Sprintf("%x", h.Sum(nil))

t, _ := template.ParseFiles("upload.gtpl")
t.Execute(w, token)
} else {
r.ParseMultipartForm(32 << 20)
file, _, err := r.FormFile("uploadfile")
if err != nil {
fmt.Println(err)
return
}
buffer := convertCsvToXml(file)

w.Header().Set("Content-Disposition", "attachment; filename=patient.xml")
w.Header().Set("Content-Type", r.Header.Get("Content-Type"))
io.Copy(w, buffer)
}
}

0 comments on commit 02fbca5

Please sign in to comment.