{"id":18965711,"url":"https://github.com/shahincsejnu/httpapiserver","last_synced_at":"2025-08-23T02:37:12.051Z","repository":{"id":144535216,"uuid":"323364359","full_name":"shahincsejnu/httpAPIserver","owner":"shahincsejnu","description":"A basic RESTful API server, build with Golang.","archived":false,"fork":false,"pushed_at":"2021-05-23T05:10:02.000Z","size":37221,"stargazers_count":2,"open_issues_count":2,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-01T06:12:35.348Z","etag":null,"topics":["dockerfile","jwt-authentication"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shahincsejnu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-12-21T14:47:08.000Z","updated_at":"2023-12-17T12:59:14.000Z","dependencies_parsed_at":null,"dependency_job_id":"0865f571-5425-4adb-888e-f801ed22fdee","html_url":"https://github.com/shahincsejnu/httpAPIserver","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shahincsejnu%2FhttpAPIserver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shahincsejnu%2FhttpAPIserver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shahincsejnu%2FhttpAPIserver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shahincsejnu%2FhttpAPIserver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shahincsejnu","download_url":"https://codeload.github.com/shahincsejnu/httpAPIserver/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239964504,"owners_count":19725951,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dockerfile","jwt-authentication"],"created_at":"2024-11-08T14:31:03.924Z","updated_at":"2025-02-21T05:41:54.126Z","avatar_url":"https://github.com/shahincsejnu.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# httpAPIserver\n\n## RESTful HTTP API server using [Go](https://github.com/golang), [Cobra CLI](https://github.com/spf13/cobra), [gorilla mux](https://github.com/gorilla/mux)\n\n### Description\n\nThis is a basic RESTful API server, build with Golang. In this API server I have implemented Cobra CLI for running the API from the CLI and also used gorilla mux instead of Go net/http.\n\n\n[![Go Report Card](https://goreportcard.com/badge/github.com/shahincsejnu/httpAPIserver)](https://goreportcard.com/report/github.com/shahincsejnu/httpAPIserver)\n\n------------ \n\n### Installation\n\n- `git clone https://github.com/shahincsejnu/httpAPIserver.git`\n- `cd httpAPIserver`\n- `go install httpAPIserver`\n\n---------------\n\n### Run by CLI Commands\n\n- start the API in default port : 8080 by `httpAPIserver start`\n- start the API in your given port by `httpAPIserver start -p=8088`, give your port number in the place of 8088\n\n--------------\n\n### Run the API server in docker container using dockerfile\n\n#### Create docker image from the dockerfile\n\n- `docker build -t \u003cimage_name\u003e .`\n- or `docker build -t \u003cdocker_hub_username\u003e/\u003cimage_name\u003e:\u003ctag\u003e .` (if your do this then don't need to give tag before dockerhub push)\n\n#### Run the API server from the docker image in docker container\n\n- `docker run -p 8088:8080 \u003cimage_name\u003e` (valid when used `CMD [\"start\", \"-p\", \"8080\"]` in Dockerfile)\n- `docker run -p 8081:8088 \u003cimage_name\u003e start -p \"8088\"` (valid for current version of Dockerfile, when did not used CMD in Dockerfile)\n\n--------------\n\n#### upload the image to [Docker Hub](https://hub.docker.com/)\n\n- `docker login --username=\u003cdocker_hub_username\u003e`\n- `docker tag \u003cid_of_the_created_image\u003e \u003cdocker_hub_username\u003e/\u003cname_of_the_image\u003e:\u003ctag\u003e`\n- `docker push \u003cdocker_hub_username\u003e/\u003cname_of_the_image\u003e:\u003ctag\u003e`\n\n--------------\n\n#### run using volume (valid for current version where did not gave .env file in docker image)\n\n\n- `docker run -v \u003cabsolute_host_path/.env\u003e:\u003ccontainer_path/.env\u003e -p 8088:8089 \u003cimage_name\u003e start -p 8089`\n\n\n--------------\n\n### The Endpoints of this REST API\n\n|Endpoint | Function | Method | StatusCode | Authentication|\n|-----|-----|-----|-----|-----|\n|`/api/login`| logIn | GET | StatusOK, StatusUnauthorized | Basic\n|`/api/articles` | getAllArticles | GET | StatusOK, StatusUnauthorized | JWT|\n|`/api/article` | addNewArticle | POST | StatusCreated, StatusUnauthorized | JWT|\n|`/api/article/{id}` | deleteArticle | DELETE | StatusOK, StatusNoContent, StatusUnauthorized | JWT|\n|`/api/article/{id}` | updateArticle | PUT | StatusCreated, StatusNoContent, StatusUnauthorized | JWT|\n|`/api/article/{id}` | getSingleArticle | GET | StatusOK, StatusNoContent, StatusUnauthorized | JWT|\n\n\n----------------\n\n### Data Model\n\n* Article Model\n```\n    type Article struct {\n    \tID       string    `json:\"id\"`\n    \tTitle    string    `json:\"title\"`\n    \tBody     string    `json:\"body\"`\n    \tAuthor   Author    `json:\"author\"`\n    }\n```\n\n* Author Model\n```\n    type Author struct {\n    \tID       string    `json:\"id\"`\n    \tName     string    `json:\"name\"`\n    \tRating   float64   `json:\"rating\"`\n    }\n```\n\n----------------\n\n### Basic Authentication\n\n- implemented basic authentication middleware\n- give username : `admin` and password : `admin` for each query to the api endpoint otherwise access will be denied\n\n----------------\n\n### JWT Authentication\n\n- implemented JWT authentication\n- first of all user need to hit `/api/login` endpoint with basic authentication then a token will be given and with that token for specific time user can do other request\n----------------\n\n\n\n### curl commands without authentication (it is valid for version : v1.0.0)\n\n#### Run the API server\n\n- `httpAPIserver start --port=8080`\n\n#### Get all articles\n\n- `curl -X GET http://localhost:8080/api/articles`\n\n#### Get single article\n\n- `curl -X GET http://localhost:8080/api/article/1`\n\n#### Create new article\n\n```\ncurl -X POST -H \"Content-Type:application/json\" -d '{\"id\":\"10\",\"title\":\"oka\",\"body\":\"none\",\"author\":{\"id\":\"11\",\"name\":\"nobody\",\"rating\":10}}' http://localhost:8080/api/article\n```\n\n#### Update any article\n\n```\ncurl -X PUT -H \"Content-Type:application/json\" -d '{\"id\":\"10\",\"title\":\"update\",\"body\":\"me\",\"author\":{\"id\":\"11\",\"name\":\"somebody\",\"rating\":9}}' http://localhost:8080/api/article/10\n```\n\n#### Delete an article\n\n- `curl -X DELETE -H http://localhost:8080/api/article/1`\n\n----------------\n\n\n### curl commands with Basic Authentication (it is valid for version : v1.0.1 and login function of version : v1.0.2)\n\n#### Get all articles\n\n- `curl -X GET --user admin:admin http://localhost:8080/api/articles`\n\n#### Get single article\n\n- `curl -X GET --user admin:admin http://localhost:8080/api/article/1`\n\n#### Create new article\n\n```\ncurl -X POST --user admin:admin -H \"Conten-Type:application/json\" -d '{\"id\":\"10\",\"title\":\"update\",\"body\":\"me\",\"author\":{\"id\":\"11\",\"name\":\"somebody\",\"rating\":9}}' http://localhost:8080/api/article\n```\n\n#### Update an article\n\n```\ncurl -X PUT --user admin:admin -H \"Conten-Type:application/json\" -d '{\"id\":\"10\",\"title\":\"update\",\"body\":\"me\",\"author\":{\"id\":\"11\",\"name\":\"somebody\",\"rating\":9}}' http://localhost:8080/api/article/1\n```\n\n#### Delete an article\n\n- `curl -X DELETE --user admin:admin http://localhost:8080/api/article/1`\n\n----------------\n\n\n### curl commands with JWT authentication (valid for version : v1.0.2)\n\n#### Get jwt token via login with basic authentication\n\n- `curl -X GET --user admin:admin http://localhost:8080/api/login`\n\n#### Get all articles\n\n- `curl -X GET -H \"Token: \u003cjwt_token\u003e\" http://localhost:8080/api/articles`\n\n#### Get single article of id 1\n\n- `curl -X GET -H \"Token: \u003cjwt_token\u003e\" http://localhost:8080/api/article/1`\n\n#### Create new article\n\n```\ncurl -X POST -H \"Token: \u003cjwt_token\u003e\" -H \"Content-Type:application/json\" -d '{\"id\":\"10\",\"title\":\"update\",\"body\":\"me\",\"author\":{\"id\":\"11\",\"name\":\"somebody\",\"rating\":9}}' http://localhost:8080/api/article\n```\n\n#### Update an article\n\n```\ncurl -X POST -H \"Token: \u003cjwt_token\u003e\" -H \"Content-Type:application/json\" -d '{\"id\":\"10\",\"title\":\"update\",\"body\":\"me\",\"author\":{\"id\":\"11\",\"name\":\"somebody\",\"rating\":9}}' http://localhost:8080/api/article/1\n```\n\n#### Delete an article whose id is 1\n\n- `curl -X DELETE -H \"Token: \u003cjwt_token\u003e\" http://localhost:8080/api/article/1`\n\n\n----------------\n\n### API Endpoints Testing\n\n- Primarily tested the API endpoints by [Postman](https://github.com/postmanlabs)\n- E2E Testing.\n    - added unit testing for this API\n    - Checked response status code with our expected status code\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshahincsejnu%2Fhttpapiserver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshahincsejnu%2Fhttpapiserver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshahincsejnu%2Fhttpapiserver/lists"}