Skip to content

Commit 86123de

Browse files
authored
feat: add improvements to the http server (#65)
* chore(main): Add graceful shutdown to the http service * build(compose): add a health check to the compose file * build(main): rename the temporary path environment variable * build(make): add changes to the makefile * fix(templates): fix zip files preview * fix(docker): add non-root user to the dockerfile * fix(templates): shorten the modal title * build(make): set the build target as prerequisite * feat(main): add api router * refactor(main): refactor the functions and handlers that are responsible to convert input files * docs(README): add documentation on how to use the api
1 parent 1d52b49 commit 86123de

File tree

9 files changed

+327
-122
lines changed

9 files changed

+327
-122
lines changed

Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ COPY --from=builder /usr/share/fonts /usr/share/fonts
3535

3636
ENV FONTCONFIG_PATH /usr/share/fonts
3737

38+
# Use morphos as user
39+
RUN useradd -m morphos
40+
USER morphos
41+
3842
EXPOSE 8080
3943

4044
ENTRYPOINT ["/bin/morphos"]

Makefile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
HTMX_VERSION=1.9.6
22
RESPONSE_TARGETS_VERSION=1.9.11
33
BOOTSTRAP_VERSION=5.3.2
4-
GO_VERSION=1.21.5
54

65
.PHONY: run
76
## run: Runs the air command.
@@ -27,11 +26,11 @@ download-bootstrap:
2726
.PHONY: docker-build
2827
## docker-build: Builds the container image
2928
docker-build:
30-
docker build --build-arg="GO_VERSION=${GO_VERSION}" -t morphos .
29+
docker build -t morphos .
3130

3231
.PHONY: docker-run
3332
## docker-run: Runs the container
34-
docker-run:
33+
docker-run: docker-build
3534
docker run --rm -p 8080:8080 -v /tmp:/tmp morphos
3635

3736
.PHONY: help

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ docker run --rm -p 8080:8080 -v /tmp:/tmp ghcr.io/danvergara/morphos-server:late
4444

4545
## Usage
4646

47+
### HTML form
48+
4749
Run the server as mentioned above and open up your favorite browser. You'll see something like this:
4850

4951
<img src="screenshots/morphos.png"/>
@@ -68,6 +70,43 @@ A modal will pop up with a preview of the converted image.
6870

6971
<img src="screenshots/modal_morphos.png"/>
7072

73+
### API
74+
75+
You can consume morphos through an API, so other systems can integrate with it.
76+
77+
##### Endpoints
78+
79+
`GET /api/v1/formats`
80+
81+
This returns a JSON that shows the supported formats at the moment.
82+
83+
e.g.
84+
85+
```
86+
{"documents": ["docx", "xls"], "image": ["png", "jpeg"]}
87+
```
88+
89+
`POST /api/v1/upload`
90+
91+
This is the endpoint that converts files to a desired format. It is basically a multipart form data in a POST request. The API simply writes the converted files to the response body.
92+
93+
e.g.
94+
95+
```
96+
curl -F 'targetFormat=epub' -F 'uploadFile=@/path/to/file/foo.pdf' localhost:8080/api/v1/upload --output foo.epub
97+
```
98+
The form fields are:
99+
100+
* targetFormat: the target format the file will be converted to
101+
* uploadFile: The path to the file that is going to be converted
102+
103+
### Configuration
104+
105+
The configuration is only done by the environment varibles shown below.
106+
107+
* `MORPHOS_PORT` changes the port the server will listen to (default is `8080`)
108+
* `MORPHOS_UPLOAD_PATH` defines the temporary path the files will be stored on disk (default is `/tmp`)
109+
71110
## Supported Files And Convert Matrix
72111

73112
### Images X Images

docker-compose.yml

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
name: morphos
22
services:
3-
morphos-server:
4-
ports:
5-
- 8080:8080
6-
volumes:
7-
- /tmp:/tmp
8-
image: ghcr.io/danvergara/morphos-server:latest
3+
morphos-server:
4+
image: ghcr.io/danvergara/morphos-server:latest
5+
# uncomment this if you want to build the container yourself.
6+
# build:
7+
# context: .
8+
# target: release
9+
ports:
10+
- 8080:8080
11+
volumes:
12+
- /tmp:/tmp
13+
healthcheck:
14+
test: timeout 10s bash -c ':> /dev/tcp/127.0.0.1/8080' || exit 1
15+
interval: 60s
16+
retries: 3
17+
start_period: 20s
18+
timeout: 30s

0 commit comments

Comments
 (0)