Commit c003d969 authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Initial commit

FROM python:3.9-buster
COPY requirements.txt ./
RUN pip install -r requirements.txt --extra-index-url
COPY index.html script.js ./
ENTRYPOINT ["python", ""]
# Coords Service
Toy Python server using [Coords Library]( to demonstrate GitLab CI features.

1022 Bytes

<title>Coordinates calculator</title>
<!-- Bootstrap CSS -->
<link href="" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous" />
<!-- Fetch API polyfill, for supporting Internet Explorer -->
<script src=""></script>
.hide { display: none; }
.loading {
position: fixed;
top: 0;
bottom: 0;
right: 0;
left: 0;
background-color: rgba(255, 255, 255, 0.7);
z-index: 100000;
.spinner-wrapper {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
width: 100%;
.spinner-border {
width: 3rem; height: 3rem;
<div class="container">
<div class="row mt-5">
<div class="col text-center">
<h2>Coordinates for #LOCATION_NAME#</h2>
<p class="fw-light">Using coords-library #LIB_VERSION#</p>
<div class="row">
<div class="col-6 offset-3">
<div class="input-group mt-3 mb-3">
<input type="text" class="form-control" id="object_name" placeholder="Object name" aria-label="Object name" aria-describedby="get_coordinates">
<button class="btn btn-primary" type="button" id="get_coordinates">Get coordinates</button>
<div class="alert alert-danger hide" role="alert" id="error">
<div class="row hide" id="result">
<div class="col">
<ul id="result-list">
<!-- results go here -->
<div id="loading" class="loading hide">
<div class="spinner-wrapper">
<div class="spinner-border text-info" role="status">
<span class="visually-hidden">Loading...</span>
<script src="script.js"></script>
\ No newline at end of file
function getCoordinates() {
// Previous results cleanup
document.getElementById('result-list').innerHTML = '';
// Retrieve object name
let objectName = document.getElementById('object_name').value;
// Call the server
fetch('coords?object=' + objectName)
.then(response => response.json())
.then(data => {
if (typeof data['error'] === 'undefined') {
// Build results list
let resultsHTML = '';
for (let key in data) {
resultsHTML += '<li><strong>' + key + '</strong>: ' + data[key] + '</li>';
document.getElementById('result-list').innerHTML = resultsHTML;
} else {
// Show error
let errorElement = document.getElementById('error');
errorElement.innerText = data['error'];
.finally(() => {
// Event handlers
document.getElementById('get_coordinates').onclick = getCoordinates;
document.getElementById('object_name').onkeydown = function(event) {
// Enter key event
if(event.keyCode === 13) {
import http.server
import socketserver
import urllib.parse as urlparse
from urllib.parse import parse_qs
from importlib.metadata import version
import json
import sys
import traceback
import argparse
import coords
class CoordsHttpRequestHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
if self.path == '/':
self.send_header('Content-type', 'text/html')
# Open HTML page and replace placeholders
content = open('index.html', 'r').read()
location_name = coords.get_location_name(location_id)
content = content.replace('#LOCATION_NAME#', location_name)
content = content.replace('#LIB_VERSION#', version('coordslib'))
self.wfile.write(bytes(content, 'utf8'))
elif self.path.startswith('/coords'):
# extract 'object' query parameter from URL
parsed = urlparse.urlparse(self.path)
object_name = parse_qs(parsed.query)['object'][0]
# call coords library
result = coords.get_coords(object_name, location_id)
except Exception as ex:
print(traceback.format_exc(), file=sys.stderr)
# send error response
self.send_json_response({'error': str(ex)})
# default server GET implementation (returns file or 404)
return http.server.SimpleHTTPRequestHandler.do_GET(self)
def send_json_response(self, data):
self.send_header('Content-type', 'application/json')
# write JSON data in HTTP response body
self.wfile.write(bytes(json.dumps(data) + '\n', 'utf8'))
parser = argparse.ArgumentParser(description='Simple HTTP server that displays a form '
'to get astronomical coordinates for a given location')
parser.add_argument('port', type=int, nargs=1, help='server port')
parser.add_argument('location_id', type=str, nargs=1, help='astropy observatory/site identifier')
args = parser.parse_args()
port = args.port[0]
location_id = args.location_id[0]
# Create an object of the above class
handler_object = CoordsHttpRequestHandler
# Avoid 'address already in use' error on server restart
socketserver.TCPServer.allow_reuse_address = True
coords_server = socketserver.TCPServer(("", port), handler_object)
# Start the server
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment