Skip to content

markfront/SPFH_RestApi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

This repository contains code to run SinglePageFullHtml (SPFH) (https://github.com/markfront/SinglePageFullHtml) as a restful service.

Getting Started

Clone/Compile/Package

$ git clone https://github.com/markfront/SPFH_RestApi.git
$ cd SPFH_RestApi
$ ./mvnw clean package

Start the Service

$ ./mvnw spring-boot:run

The service will be run on http://localhost:8080

Call the Service

Enter the following in browser address bar or using GET method in Postman:

http://localhost:8080/fget?url=<url>

For example, http://localhost:8080/fget?url=https://www.wonderslist.com/10-most-amazing-places-on-earth

After about 10-30 seconds, you should see the result returned as JSON.

Run the API As Docker Container

If you like to run the api as a docker, I have included the dockerfile to build a ubuntu docker image that includes everything. Here is how:

Install Docker and Docker.io (if you haven't)

sudo apt-get install docker docker.io

Build Docker Image

If this is the first time you build docker, you might want to first read this page: https://www.howtoforge.com/tutorial/how-to-create-docker-images-with-dockerfile

$ cd ~/git/SPFH_RestApi
$ docker build -f my-custom-ubuntu.dockerfile -t my-custom-ubuntu .

It will take a few minutes to build. When it succeeds, you should see something like:

Successfully built 73f1bc6fda8c
Successfully tagged my-custom-ubuntu:latest

Also you should see the built image by "docker image ls", something like this:

$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
my-custom-ubuntu    latest              73f1bc6fda8c        21 minutes ago      1.28GB

Start the Container

docker run -d -v /tmp:/tmp -p 8282:8080 --name my-container my-custom-ubuntu

It will start the docker container named "my-custom-ubuntu" in the backend (as deamon by the option "-d"), hook the folder /tmp of your host machine to the folder /tmp of the docker (to store temporary file generated by the rest api which is running inside the docker). It will also hook the port 8282 of your host machine to the port 8080 exposed by the docker.

You can also enter the container as root (to debug or monitor) by:

docker exec -it my-container /bin/bash

Note: as the service is trying to run firefox inside the docker container, it might not work (which you would get error like "Error: no DISPLAY environment variable specified").

It happened to me when I tested the docker container in my Ubuntu laptop. Did some research, found the idea described in the following article seems easier to follow: http://fabiorehm.com/blog/2014/09/11/running-gui-apps-with-docker/

It basically tries to share the firefox in the host with the docker container, so that when you try to open firefox inside the docker container, it actually opens the one installed in the host. Not a perfect solution, but works. You will need to install the following:

$ sudo apt-get install dbus-x11 
$ sudo apt-get install x11-xserver-utils
$ xhost +local:docker

Then start the docker container like this:

$ sudo docker run -v /tmp:/tmp -v /tmp/.X11-unix/:/tmp/.X11-unix/ -p 8282:8080 -e DISPLAY=$DISPLAY --name my-container my-custom-ubuntu

Test the Api

Open a browser and enter the following in the address bar:

http://localhost:8282/fget?url=https://www.wonderslist.com/10-most-amazing-places-on-earth

Note the port number is 8282, which is what you specify when start the docker.

Shutdown the Container

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
4ef2c1533bb6        my-custom-ubuntu    "/bin/sh -c 'cd git/…"   5 seconds ago       Up 4 seconds        0.0.0.0:8282->8080/tcp   eager_kirch

$ docker kill 4ef2c1533bb6

Enjoy!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages