Skip to content

Practice of using GO to make simple Restful API service

License

Notifications You must be signed in to change notification settings

GMfatcat/GO_Restful_api

Repository files navigation

Static Badge Static Badge Static Badge

Table of Contents
  1. About The Project
  2. Getting Started
  3. Architecture and Usage
  4. Roadmap
  5. Contributing
  6. License
  7. Contact
  8. Acknowledgments

About The Project

Main Page

This is the simple fullstack project using Go,Python,SQL,HTML,CSS and Javascript.

User can query dates to get & save json data, aslo leaflet maps.

Functions of the project 😄

  • Generating fake geo-data in json format --> Python
  • Store geo-data in sql table --> Go, PostgreSQL
  • Lightweight HTTP Server --> Go
  • Interactive Web Page --> HTML,CSS and Javascript

Developer Report

Getting Started

Prerequisites

  • Language Version
    Python >= 3.7
    Go >= 1.12 (using go mod)
    
  • Package pq : Go PostgreSQL Driver
    go get github.com/lib/pq
    

Installation

Git clone the project or download the whole ZIP file.

  1. Git
    git clone https://github.com/GMfatcat/GO_Restful_api.git
    
  2. Download the zip file with the bellow instruction
    1. Click <Code> (green button)
    2. Download ZIP
    

(back to top)

Architecture and Usage

Architecture

Architecture:

  • Frontend (User + Homepage + Result page)
  • Backend (HTTP Server + SQL Server + File Server)
  • Data Provider (Generate json data)
  • Middleware Logger (save as "log.txt")

Data Provider:

  • Argparse script for generating data, see arguments info below

Run whitout arguments

python generate_data.py

Run with arguments

usage: generate_data.py [-h] [--max_position MAX_POSITION] [--max_num MAX_NUM] [--days DAYS] [--start_date START_DATE]

options:
-h, --help            show this help message and exit
--max_position MAX_POSITION Maximum number of positions to generate.
--max_num MAX_NUM     Maximum number of objects to generate.
--days DAYS           Number of days between to generate.
--start_date START_DATE Start date in the format YYYY-MM-DD.

Example: python generate_data.py --max_position 30 --max_num 30 --days 20 --start_date 2023-01-05
  • Modify coordinates range if necessary
    edit in generate_data.py line 24 & 25
    
  • Run the script without any arguments is fine but those data has already been generated in "./data" folder
  • Save json in "./data"

Backend Usage:

  • Configure your PostgreSQL database user / dbname / password in "./action/connector/Connector.go"
  • Add json data to SQL database
    // change the *addNewData* to true in add_data.go, default to be false
    go run add_data.go
    
  • Query dates for sql data
    // v1 & v2 differences see test_backend.go
    go run test_backend.go
    
  • Test Communication between frontend and backend
    // After start the server , Open browser, type "http://localhost:8080" in url section.
    go run test_frontend.go
    
  • Run Http Server - Must run before ANY Frontend Usage!!!
    // After start the server , Open browser, type "http://localhost:9487" in url section.
    go run server.go
    

Server Log example after Server started Server Log

Frontend Usage (example video):

  • Connect to Homepage
    Open browser, type "http://localhost:9487" in url section.
    You can change the port in "server.go"
    
  • Query dates for json data (Date format:XXXX-XX-XX)
    Available Name same as the filename in ./data
    Current Available : <2022-01-01> to <2022-01-30> 
    
  • Auto-display json data (if data exists)
  • Save json & Generate Leaflet map by button

(back to top)

Roadmap

I am trying to combine this project with my another geohash project.
That project can use geohash to find several groups of what3words. Combination will be in the develop branch.

  • Data Generator
  • SQL Server for Data
  • Go HTTP Server
  • Backend Functions
    • JSON to SQL Server
    • Query SQL TABLE
    • Middleware Logger
    • Update / Delete TABLE
    • TBD
  • Frontend Functions
    • Webpage
    • Query data
    • Save JSON data
    • Interactive Map (Leaflet)
    • Direct "curl / get" to access data
    • TBD

(back to top)

Contributing

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch
  3. Commit your Changes
  4. Push to the Branch
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE.txt for more information.

Contact

GMfatcat : github

Acknowledgments

I've included all of free resources to kick things off!

(back to top)