├── .github └── workflows │ └── build.yaml ├── .gitignore ├── Dockerfile ├── LICENSE ├── README.md ├── constants ├── error.go ├── static.go └── variables.go ├── controller ├── route.go └── url.go ├── database └── connect.go ├── go.mod ├── go.sum ├── k8s-config ├── backend-config.yaml ├── backend-service.yaml ├── backend.yaml ├── ingress.yaml ├── nfs-client.yaml ├── nfs-server.yaml ├── postgres.yaml ├── redis-config.yaml └── redis.yaml ├── kustomization.yaml ├── middleware └── cache.go ├── model └── model.go ├── router └── router.go ├── server.go └── test ├── cache_test.go ├── error_test.go └── success_test.go /.github/workflows/build.yaml: -------------------------------------------------------------------------------- 1 | name: Build, Unit Testing, Versioning and Deploy 2 | 3 | on: 4 | push: 5 | tags: 6 | - 'v*.*.*' 7 | 8 | env: 9 | PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }} 10 | RUN_REGION: asia-east1 11 | IMAGE_NAME: url-shortener-backend 12 | CLUSTER_NAME: dcard-gke-single-region 13 | CONFIG_MAP_NAME: backend-config 14 | POSTGRESQL_CLUSTER_IP: ${{ secrets.POSTGRESQL_CLUSTER_IP }} 15 | REDIS_CLUSTER_IP: ${{ secrets.REDIS_CLUSTER_IP }} 16 | 17 | # Env for backend server usage 18 | SQL_USER: ${{ secrets.SQL_USER}} 19 | SQL_PASSWORD: ${{ secrets.SQL_PASSWORD }} 20 | DATABASE_NAME: postgres 21 | POSTGRESQL_IP: 127.0.0.1 22 | REDIS_IP: 127.0.0.1 23 | POSTGRESQL_PORT_NUMBER: 5432 24 | REDIS_PORT_NUMBER: 6379 25 | # Unit testing host 26 | SERVER_HOST: http://127.0.0.1:8080 27 | DEPLOYMENT_NAME: url-shortener-backend 28 | 29 | jobs: 30 | build-backend-image: 31 | name: Build Docker Image 32 | if: github.event.base_ref == 'refs/heads/main' 33 | runs-on: ubuntu-latest 34 | outputs: 35 | RELEASE_VERSION: ${{ steps.GET_RELEASE_VERSION_STEP.outputs.value }} 36 | MAJOR_VERSION: ${{ steps.GET_MAJOR_VERSION_STEP.outputs.value }} 37 | 38 | steps: 39 | - name: Checkout 40 | uses: actions/checkout@v2 41 | 42 | - name: Set version into environment variable 43 | run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV 44 | 45 | - id: GET_RELEASE_VERSION_STEP 46 | run: echo "::set-output name=value::$RELEASE_VERSION" 47 | 48 | - name: Set MAJOR version into environment variable 49 | run: echo "MAJOR_VERSION=$( echo $RELEASE_VERSION | cut -c1-2)" >> $GITHUB_ENV 50 | 51 | - id: GET_MAJOR_VERSION_STEP 52 | run: echo "::set-output name=value::$MAJOR_VERSION" 53 | 54 | - name: Set up golang 55 | uses: actions/setup-go@v2 56 | with: 57 | go-version: '1.17.3' 58 | 59 | - name: Build Image 60 | run: docker build -t asia.gcr.io/$PROJECT_ID/$MAJOR_VERSION/$IMAGE_NAME:$RELEASE_VERSION . 61 | 62 | - name: Save docker image as tar file 63 | run: docker save asia.gcr.io/$PROJECT_ID/$MAJOR_VERSION/$IMAGE_NAME:$RELEASE_VERSION > url.tar 64 | 65 | - name: Upload tar file to github artifact 66 | uses: actions/upload-artifact@v3 67 | with: 68 | name: image-artifact 69 | path: url.tar 70 | 71 | unit-tests: 72 | name: Unit tests 73 | if: github.event.base_ref == 'refs/heads/main' 74 | runs-on: ubuntu-latest 75 | steps: 76 | # Fetch repo files 77 | - name: Checkout 78 | uses: actions/checkout@v2 79 | 80 | - name: Google credential authentication 81 | uses: google-github-actions/auth@v0 82 | with: 83 | credentials_json: ${{ secrets.GCP_CREDENTIALS }} 84 | 85 | - name: Set up gcloud cli 86 | uses: google-github-actions/setup-gcloud@v0 87 | 88 | - name: Get gke credential 89 | run: gcloud container clusters get-credentials $CLUSTER_NAME --region $RUN_REGION --project $PROJECT_ID 90 | 91 | - name: Set up golang 92 | uses: actions/setup-go@v2 93 | with: 94 | go-version: '1.17.3' 95 | 96 | - name: Set up postgreSQL remote forwarding server 97 | run: | 98 | nohup kubectl port-forward $(kubectl get pod --selector="app=postgres" --output jsonpath='{.items[0].metadata.name}') $POSTGRESQL_PORT_NUMBER:$POSTGRESQL_PORT_NUMBER & 99 | sleep 10 100 | 101 | - name: Set up redis remote forwarding server 102 | run: | 103 | nohup kubectl port-forward $(kubectl get pod --selector="pod=redis" --output jsonpath='{.items[0].metadata.name}') $REDIS_PORT_NUMBER:$REDIS_PORT_NUMBER & 104 | sleep 10 105 | 106 | - name: Set up backend server 107 | run: | 108 | nohup go run server.go & 109 | sleep 10 110 | 111 | - name: Start unit test 112 | run: go test ./test -v 113 | 114 | push-image-to-gcr: 115 | name: Push image to GCR 116 | needs: [build-backend-image, unit-tests] 117 | runs-on: ubuntu-latest 118 | steps: 119 | # Here we use docker locally 120 | - name: Download image artifact 121 | uses: actions/download-artifact@v3 122 | with: 123 | name: image-artifact 124 | 125 | - name: Load out docker image 126 | run: docker load < url.tar 127 | 128 | # After this line, we use docker for uploading image to gcr 129 | - name: Google credential authentication 130 | uses: google-github-actions/auth@v0 131 | with: 132 | credentials_json: ${{ secrets.GCP_CREDENTIALS }} 133 | 134 | - name: Set up gcloud cli 135 | uses: google-github-actions/setup-gcloud@v0 136 | 137 | - name: Configure docker authentication 138 | run: gcloud auth configure-docker -q 139 | 140 | - name: Push docker image to GCR 141 | run: docker push asia.gcr.io/$PROJECT_ID/${{ needs.build-backend-image.outputs.MAJOR_VERSION}}/$IMAGE_NAME:${{ needs.build-backend-image.outputs.RELEASE_VERSION }} 142 | 143 | deploy-go-gke: 144 | name: Deploy to GKE 145 | needs: [build-backend-image, push-image-to-gcr] 146 | runs-on: ubuntu-latest 147 | steps: 148 | - name: Checkout 149 | uses: actions/checkout@v2 150 | 151 | - name: Google credential authentication 152 | uses: google-github-actions/auth@v0 153 | with: 154 | credentials_json: ${{ secrets.GCP_CREDENTIALS }} 155 | 156 | - name: Set up gcloud cli 157 | uses: google-github-actions/setup-gcloud@v0 158 | 159 | - name: Get gke credential 160 | run: | 161 | gcloud container clusters get-credentials $CLUSTER_NAME --region $RUN_REGION --project $PROJECT_ID 162 | 163 | # Semantic versioning 164 | - name: Do versioning jobs 165 | uses: mikefarah/yq@master 166 | with: 167 | cmd: | 168 | # Change deployment image name 169 | yq -i '.spec.template.spec.containers[0].image = "asia.gcr.io/${{ env.PROJECT_ID }}/${{ needs.build-backend-image.outputs.MAJOR_VERSION}}/${{ env.IMAGE_NAME }}:${{ needs.build-backend-image.outputs.RELEASE_VERSION }}"' ./k8s-config/backend.yaml 170 | 171 | #---------------- Deployment ---------------- 172 | # change deployment name 173 | yq -i '.metadata.name = "url-shortener-backend-${{ needs.build-backend-image.outputs.MAJOR_VERSION}}"' ./k8s-config/backend.yaml 174 | 175 | # change deployment label name 176 | yq -i '.metadata.labels.deployment = "url-shortener-backend-${{ needs.build-backend-image.outputs.MAJOR_VERSION}}"' ./k8s-config/backend.yaml 177 | 178 | 179 | # change deployment match pod name 180 | yq -i '.spec.selector.matchLabels.pod = "url-shortener-backend-${{ needs.build-backend-image.outputs.MAJOR_VERSION}}"' ./k8s-config/backend.yaml 181 | 182 | # change pod label name 183 | yq -i '.spec.template.metadata.labels.pod = "url-shortener-backend-${{ needs.build-backend-image.outputs.MAJOR_VERSION}}"' ./k8s-config/backend.yaml 184 | 185 | # change pod name 186 | yq -i '.spec.template.metadata.labels.name = "url-shortener-backend-${{ needs.build-backend-image.outputs.MAJOR_VERSION}}"' ./k8s-config/backend.yaml 187 | 188 | #---------------- Service ---------------- 189 | # change service name 190 | yq -i '.metadata.name = "url-shortener-backend-${{ needs.build-backend-image.outputs.MAJOR_VERSION}}"' ./k8s-config/backend-service.yaml 191 | 192 | # change service match pod name 193 | yq -i '.spec.selector.pod = "url-shortener-backend-${{ needs.build-backend-image.outputs.MAJOR_VERSION}}"' ./k8s-config/backend-service.yaml 194 | 195 | 196 | # Set up kustomize 197 | - name: Set up Kustomize 198 | run: |- 199 | curl -sfLo kustomize https://github.com/kubernetes-sigs/kustomize/releases/download/v3.1.0/kustomize_3.1.0_linux_amd64 200 | chmod u+x ./kustomize 201 | 202 | # Deploy the Docker image to the GKE cluster 203 | - name: Deploy to GKE 204 | run: |- 205 | ./kustomize edit set image asia.gcr.io/$PROJECT_ID/${{ needs.build-backend-image.outputs.MAJOR_VERSION}}/$IMAGE_NAME:${{ needs.build-backend-image.outputs.RELEASE_VERSION }} 206 | echo "Set image pass" 207 | ./kustomize build . | kubectl apply -f - --validate=false 208 | echo "Build pass" 209 | kubectl rollout status deployment/$DEPLOYMENT_NAME-${{ needs.build-backend-image.outputs.MAJOR_VERSION}} 210 | echo "Rollout pass" 211 | kubectl get services -o wide 212 | 213 | 214 | 215 | 216 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .idea 3 | cloud_sql_proxy 4 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | 2 | FROM golang:1.17.0-alpine3.14 3 | RUN mkdir /src 4 | WORKDIR /src 5 | COPY . /src/ 6 | RUN go build -o ./bin/url-shortener 7 | EXPOSE 8080 8 | CMD ["./bin/url-shortener"] -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Yenting Chen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Overview 2 | ![Build, Unit Testing, Versioning and Deploy ](https://github.com/SwarzChen/shorturl-maker/actions/workflows/build.yaml/badge.svg) 3 | ![GitHub](https://img.shields.io/github/license/SwarzChen/shorturl-maker) 4 | ![GitHub Repo stars](https://img.shields.io/github/stars/SwarzChen/shorturl-maker) 5 | ![GitHub release (latest by date)](https://img.shields.io/github/v/release/SwarzChen/shorturl-maker) 6 | 7 | This is a project for applying my first intern job at **Dcard Taiwan Ltd.** 😃 !!! 8 | [🔗 Backend documentation link 🔗](https://documenter.getpostman.com/view/12176709/UVypycK7) 9 | 10 | ### Company Requirements 11 | 12 | - [x] One POST api for uploading url 13 | - [x] One GET api for redirecting to original url 14 | - [x] Use one of the relational databases: MySQL, PostgresSQL, SQLite 15 | - [x] Use one of the cache database: Redis, Memcached 16 | - [x] Reasonable error handling 17 | - [x] No need to consider auth 18 | - [x] Simultaneously user access handling 19 | - [x] Non-existent shorten URL access handling 20 | 21 | ### Tech Stack 22 | * Using **Golang Gin** framework to build api 23 | * Using **postgresSQL** for relational database 24 | * Using **redis** for caching database 25 | * Deploy database and backend server on **Google Kubernetes Engine** 26 | * **Github Actions** for CI / CD 27 | * Implement **semantic versioning** with git 28 | 29 | ### Features 30 | * Deploy backend service on **GKE 3-Nodes distributed systems** 31 | * Deploy databases on **GKE 3-Nodes distributed systems** 32 | * Handling invalid access and simultaneously access by **caching** 33 | * Automatically **unit testing** in CI/CD workflow using github action 34 | * Improve CI/CD efficiency with **pipeline workflow** 35 | * Automatically **semantic versioning** in CI/CD workflow base on git label 36 | * DNS and proxy server configuration using **cloudflare** 37 | 38 | ### Detail explanation 39 | [How do I design my backend system architecture ?](https://medium.com/@aaaa102234/crazy-go-day-k8s-system-design-for-go-gin-redis-postgresql-957c74b4b25) 40 | [How do I handle simultaneously access and non-existed-url access with cache ?](https://medium.com/@aaaa102234/crazy-go-day-access-caching-go-gin-redis-58d0446e9a3a) 41 | [Why do I choose Gin framework and how do I handle error in it?](https://medium.com/@aaaa102234/crazy-go-day-why-using-gin-for-golang-backend-9ca48ec5d855) 42 | [How do I integrate both versioning process and unit tests into CI/CD process ?](https://medium.com/@aaaa102234/crazy-go-day-integrate-semantic-versioning-and-unit-tests-into-ci-cd-workflow-827d07495ca) 43 | [How do I implement unit tests ?](https://medium.com/@aaaa102234/crazy-go-day-simple-golang-unit-test-implementation-73518086496e) 44 | 45 | ### Future TODO list 46 | - [ ] Build frontend UI with next.js and ts 47 | - [ ] Feature: Upload video and image 48 | - [ ] Feature: User can set password for uploaded resource 49 | 50 | 51 | -------------------------------------------------------------------------------- /constants/error.go: -------------------------------------------------------------------------------- 1 | package constants 2 | 3 | import ( 4 | "fmt" 5 | "net/http" 6 | ) 7 | 8 | type ErrorEntity struct { 9 | StatusCode int 10 | ErrorCode string 11 | Message string 12 | } 13 | 14 | func (err *ErrorEntity) Error() string { 15 | return fmt.Sprintf("StatusCode: %d, ErrorCode: %s, Message: %s", err.StatusCode, err.ErrorCode, err.Message) 16 | } 17 | 18 | // STATUS_INTERNAL_SERVER_ERROR 19 | var SOME_ENV_VARIABLE_MISSING_ERROR = &ErrorEntity{ 20 | StatusCode: http.StatusInternalServerError, 21 | ErrorCode: "SOME_ENV_VARIABLES_MISSING_ERROR", 22 | Message: "Some environment variables are missing.", 23 | } 24 | 25 | var DATABASE_CONNETION_ERROR = &ErrorEntity{ 26 | StatusCode: http.StatusInternalServerError, 27 | ErrorCode: "DATABASE_CONNETION_ERROR", 28 | } 29 | 30 | var REDIS_INSERTION_ERROR = &ErrorEntity{ 31 | StatusCode: http.StatusInternalServerError, 32 | ErrorCode: "REDIS_INSERTION_ERROR", 33 | } 34 | var POSTGRESQL_DELETION_ERROR = &ErrorEntity{ 35 | StatusCode: http.StatusInternalServerError, 36 | ErrorCode: "POSTGRESQL_DELETION_ERROR", 37 | } 38 | var REQUEST_BODY_PARSING_ERROR = &ErrorEntity{ 39 | StatusCode: http.StatusInternalServerError, 40 | ErrorCode: "REQUEST_BODY_PARSING_ERROR", 41 | } 42 | var POSTGRESQL_INSERTION_ERROR = &ErrorEntity{ 43 | StatusCode: http.StatusInternalServerError, 44 | ErrorCode: "POSTGRESQL_INSERTION_ERROR", 45 | } 46 | var POSTGRESQL_QUERY_ERROR = &ErrorEntity{ 47 | StatusCode: http.StatusInternalServerError, 48 | ErrorCode: "POSTGRESQL_QUERY_ERROR", 49 | } 50 | var SETTING_GENERATOR_SEED_ERROR = &ErrorEntity{ 51 | StatusCode: http.StatusInternalServerError, 52 | ErrorCode: "SETTING_GENERATOR_SEED_ERROR", 53 | } 54 | var ID_GENERATION_ERROR = &ErrorEntity{ 55 | StatusCode: http.StatusInternalServerError, 56 | ErrorCode: "ID_GENERATION_ERROR", 57 | } 58 | 59 | // STATUS_NOT_FOUND 60 | var URL_ID_MISSING_ERROR = &ErrorEntity{ 61 | StatusCode: http.StatusNotFound, 62 | ErrorCode: "URL_ID_MISSING_ERROR", 63 | Message: "There is no url_id in url.", 64 | } 65 | var URL_ID_NOT_EXIST_ERROR = &ErrorEntity{ 66 | StatusCode: http.StatusNotFound, 67 | ErrorCode: "URL_ID_NOT_EXIST_ERROR", 68 | Message: "This url id does not exist", 69 | } 70 | var URL_NOT_EXIST_OR_EXPIRE = &ErrorEntity{ 71 | StatusCode: http.StatusNotFound, 72 | ErrorCode: "URL_NOT_EXIST_OR_EXPIRE", 73 | Message: "This url was expired or it does not exist.", 74 | } 75 | 76 | // Bad Request 77 | var REQUEST_BODY_MALFORMED = &ErrorEntity{ 78 | StatusCode: http.StatusBadRequest, 79 | ErrorCode: "REQUEST_BODY_MALFORMED", 80 | } 81 | var INVALID_TIME_FORMAT = &ErrorEntity{ 82 | StatusCode: http.StatusBadRequest, 83 | ErrorCode: "INVALID_TIME_FORMAT", 84 | } 85 | var INPUT_EXPIRE_TIME_IS_INVALID = &ErrorEntity{ 86 | StatusCode: http.StatusBadRequest, 87 | ErrorCode: "INPUT_EXPIRE_TIME_IS_INVALID", 88 | Message: "Expiration time is behind current time, which is no sense.", 89 | } 90 | -------------------------------------------------------------------------------- /constants/static.go: -------------------------------------------------------------------------------- 1 | package constants 2 | 3 | import "time" 4 | 5 | const ID_NOT_EXIST = "ID_NOT_EXIST" 6 | 7 | var TIME_ZONE, _ = time.LoadLocation("UTC") 8 | 9 | const CURRENT_VERSION = "v1" 10 | -------------------------------------------------------------------------------- /constants/variables.go: -------------------------------------------------------------------------------- 1 | package constants 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | var SQL_USER string 9 | var SQL_PASSWORD string 10 | var DATABASE_NAME string 11 | var POSTGRESQL_IP string 12 | var REDIS_IP string 13 | var POSTGRESQL_PORT_NUMBER string 14 | var REDIS_PORT_NUMBER string 15 | var SERVER_HOST string 16 | 17 | func FetchEnvVariables() error { 18 | var isExisted bool 19 | SQL_USER, isExisted = os.LookupEnv("SQL_USER") 20 | if !isExisted { 21 | fmt.Println("SQL_USER_MISSING") 22 | return SOME_ENV_VARIABLE_MISSING_ERROR 23 | } 24 | 25 | SQL_PASSWORD, isExisted = os.LookupEnv("SQL_PASSWORD") 26 | if !isExisted { 27 | fmt.Println("SQL_PASSWORD_MISSING") 28 | return SOME_ENV_VARIABLE_MISSING_ERROR 29 | } 30 | 31 | DATABASE_NAME, isExisted = os.LookupEnv("DATABASE_NAME") 32 | if !isExisted { 33 | fmt.Println("DATABASE_NAME_MISSING") 34 | return SOME_ENV_VARIABLE_MISSING_ERROR 35 | } 36 | 37 | POSTGRESQL_IP, isExisted = os.LookupEnv("POSTGRESQL_IP") 38 | if !isExisted { 39 | fmt.Println("POSTGRESQL_IP_MISSING") 40 | return SOME_ENV_VARIABLE_MISSING_ERROR 41 | } 42 | 43 | REDIS_IP, isExisted = os.LookupEnv("REDIS_IP") 44 | if !isExisted { 45 | fmt.Println("REDIS_IP_MISSING") 46 | return SOME_ENV_VARIABLE_MISSING_ERROR 47 | } 48 | 49 | POSTGRESQL_PORT_NUMBER, isExisted = os.LookupEnv("POSTGRESQL_PORT_NUMBER") 50 | if !isExisted { 51 | fmt.Println("POSTGRESQL_PORT_NUMBER_MISSING") 52 | return SOME_ENV_VARIABLE_MISSING_ERROR 53 | } 54 | 55 | REDIS_PORT_NUMBER, isExisted = os.LookupEnv("REDIS_PORT_NUMBER") 56 | if !isExisted { 57 | fmt.Println("REDIS_PORT_NUMBER_MISSING") 58 | return SOME_ENV_VARIABLE_MISSING_ERROR 59 | } 60 | 61 | SERVER_HOST, isExisted = os.LookupEnv("SERVER_HOST") 62 | if !isExisted { 63 | fmt.Println("SERVER_HOST_MISSING") 64 | return SOME_ENV_VARIABLE_MISSING_ERROR 65 | } 66 | 67 | return nil 68 | } 69 | -------------------------------------------------------------------------------- /controller/route.go: -------------------------------------------------------------------------------- 1 | package controller 2 | 3 | import ( 4 | "github.com/gin-gonic/gin" 5 | "net/http" 6 | ) 7 | 8 | func MissingRouteHandler(c *gin.Context) { 9 | c.JSON(http.StatusOK, gin.H{ 10 | "message": "This routing path has not supported yet, please check out the API documentation below.", 11 | "documentation": "https://documenter.getpostman.com/view/12176709/UVypycK7", 12 | }) 13 | return 14 | } 15 | -------------------------------------------------------------------------------- /controller/url.go: -------------------------------------------------------------------------------- 1 | package controller 2 | 3 | import ( 4 | "database/sql" 5 | "encoding/json" 6 | "github.com/SwarzChen/url-shortener/constants" 7 | "github.com/SwarzChen/url-shortener/database" 8 | "github.com/SwarzChen/url-shortener/model" 9 | "github.com/gin-gonic/gin" 10 | "github.com/teris-io/shortid" 11 | "io/ioutil" 12 | "net/http" 13 | "time" 14 | ) 15 | 16 | func GetUrlHandler(c *gin.Context) { 17 | urlId := c.Param("url_id") 18 | 19 | postgreSQLClient := database.DatabaseClient.PostgreSQLClient 20 | redisClient := database.DatabaseClient.RedisClient 21 | ctx := c.Request.Context() 22 | 23 | var urlEntity model.UrlEntityInDB 24 | // get url 25 | err := postgreSQLClient.QueryRowContext(ctx, "SELECT * FROM url WHERE id=$1", urlId).Scan(&urlEntity.Id, 26 | &urlEntity.Url, &urlEntity.ExpireAt, &urlEntity.CreateAt) 27 | 28 | switch { 29 | // not exist in database 30 | case err == sql.ErrNoRows: 31 | // record to redis 32 | err = redisClient.Set(ctx, urlId, constants.ID_NOT_EXIST, time.Hour).Err() 33 | if err != nil { 34 | errorCopy := constants.REDIS_INSERTION_ERROR 35 | errorCopy.Message = err.Error() 36 | c.JSON(errorCopy.StatusCode, errorCopy) 37 | return 38 | } 39 | 40 | c.JSON(constants.URL_NOT_EXIST_OR_EXPIRE.StatusCode, constants.URL_NOT_EXIST_OR_EXPIRE) 41 | return 42 | 43 | // other kinds database error 44 | case err != nil: 45 | errorCopy := constants.POSTGRESQL_QUERY_ERROR 46 | errorCopy.Message = err.Error() 47 | c.JSON(errorCopy.StatusCode, errorCopy) 48 | return 49 | } 50 | 51 | expireUTCTime, _ := time.Parse(time.RFC3339, urlEntity.ExpireAt) 52 | currentTime := time.Now().In(constants.TIME_ZONE) 53 | 54 | // This url expired, we will delete it 55 | if currentTime.After(expireUTCTime) { 56 | // remove expired url from database 57 | _, err = postgreSQLClient.ExecContext(ctx, "DELETE FROM url WHERE id=$1", urlId) 58 | if err != nil { 59 | errorCopy := constants.POSTGRESQL_DELETION_ERROR 60 | errorCopy.Message = err.Error() 61 | c.JSON(errorCopy.StatusCode, errorCopy) 62 | return 63 | } 64 | 65 | // record to cache 66 | err = redisClient.Set(ctx, urlEntity.Id, constants.ID_NOT_EXIST, time.Hour).Err() 67 | if err != nil { 68 | errorCopy := constants.REDIS_INSERTION_ERROR 69 | errorCopy.Message = err.Error() 70 | c.JSON(errorCopy.StatusCode, errorCopy) 71 | return 72 | } 73 | 74 | c.JSON(constants.URL_NOT_EXIST_OR_EXPIRE.StatusCode, constants.URL_NOT_EXIST_OR_EXPIRE) 75 | return 76 | } 77 | 78 | // calculate cache time 79 | cacheExpireTime := currentTime.Add(1 * time.Hour) 80 | if expireUTCTime.Before(cacheExpireTime) { 81 | cacheExpireTime = expireUTCTime 82 | } 83 | 84 | // put it to the cache 85 | err = redisClient.Set(ctx, urlEntity.Id, urlEntity.Url, cacheExpireTime.Sub(currentTime)).Err() 86 | if err != nil { 87 | errorCopy := constants.REDIS_INSERTION_ERROR 88 | errorCopy.Message = err.Error() 89 | c.JSON(errorCopy.StatusCode, errorCopy) 90 | return 91 | } 92 | 93 | // redirect to url 94 | c.Redirect(http.StatusFound, urlEntity.Url) 95 | return 96 | } 97 | 98 | func CreateUrlHandler(c *gin.Context) { 99 | body, err := ioutil.ReadAll(c.Request.Body) 100 | if err != nil { 101 | errorCopy := constants.REQUEST_BODY_MALFORMED 102 | errorCopy.Message = err.Error() 103 | c.JSON(errorCopy.StatusCode, errorCopy) 104 | return 105 | } 106 | 107 | var urlEntity model.UrlEntityInDB 108 | err = json.Unmarshal(body, &urlEntity) 109 | if err != nil { 110 | errorCopy := constants.REQUEST_BODY_PARSING_ERROR 111 | errorCopy.Message = err.Error() 112 | c.JSON(errorCopy.StatusCode, errorCopy) 113 | return 114 | } 115 | 116 | expireUTCTime, err := time.Parse(time.RFC3339, urlEntity.ExpireAt) 117 | if err != nil { 118 | errorCopy := constants.INVALID_TIME_FORMAT 119 | errorCopy.Message = err.Error() 120 | c.JSON(errorCopy.StatusCode, errorCopy) 121 | return 122 | } 123 | 124 | currentTime := time.Now().In(constants.TIME_ZONE) 125 | currentTimeString := time.Now().In(constants.TIME_ZONE).Format(time.RFC3339) 126 | 127 | // check expire time validation 128 | if expireUTCTime.Before(currentTime) { 129 | c.JSON(constants.INPUT_EXPIRE_TIME_IS_INVALID.StatusCode, constants.INPUT_EXPIRE_TIME_IS_INVALID) 130 | return 131 | } 132 | 133 | // generate short unique id 134 | generator, err := shortid.New(1, shortid.DefaultABC, 2342) 135 | if err != nil { 136 | errorCopy := constants.SETTING_GENERATOR_SEED_ERROR 137 | errorCopy.Message = err.Error() 138 | c.JSON(errorCopy.StatusCode, errorCopy) 139 | return 140 | } 141 | 142 | sid, err := generator.Generate() 143 | if err != nil { 144 | errorCopy := constants.ID_GENERATION_ERROR 145 | errorCopy.Message = err.Error() 146 | c.JSON(errorCopy.StatusCode, errorCopy) 147 | return 148 | } 149 | 150 | // insert into postgresql 151 | ctx := c.Request.Context() 152 | postgreSQLClient := database.DatabaseClient.PostgreSQLClient 153 | 154 | _, err = postgreSQLClient.ExecContext(ctx, "INSERT INTO url (id, url, expire_at, create_at) VALUES ($1, $2, $3, "+ 155 | "$4);", sid, urlEntity.Url, urlEntity.ExpireAt, currentTimeString) 156 | if err != nil { 157 | errorCopy := constants.POSTGRESQL_INSERTION_ERROR 158 | errorCopy.Message = err.Error() 159 | c.JSON(errorCopy.StatusCode, errorCopy) 160 | return 161 | } 162 | 163 | urlResponseEntity := model.UrlEntity{ 164 | Id: sid, 165 | ShortUrl: constants.SERVER_HOST + "/" + constants.CURRENT_VERSION + "/" + sid, 166 | } 167 | 168 | c.JSON(http.StatusOK, urlResponseEntity) 169 | return 170 | } 171 | -------------------------------------------------------------------------------- /database/connect.go: -------------------------------------------------------------------------------- 1 | package database 2 | 3 | import ( 4 | "database/sql" 5 | "fmt" 6 | "github.com/SwarzChen/url-shortener/constants" 7 | "github.com/go-redis/redis/v8" 8 | _ "github.com/lib/pq" 9 | "log" 10 | ) 11 | 12 | var DatabaseClient = ConstructDB() 13 | 14 | type DB struct { 15 | PostgreSQLClient *sql.DB 16 | RedisClient *redis.Client 17 | } 18 | 19 | func ConstructDB() *DB { 20 | db := new(DB) 21 | err := db.Connect() 22 | if err != nil { 23 | log.Fatalln(err) 24 | } 25 | return db 26 | } 27 | 28 | func (db *DB) Connect() error { 29 | // fetch env variables 30 | err := constants.FetchEnvVariables() 31 | if err != nil { 32 | return err 33 | } 34 | 35 | // connect to postgreSQL 36 | dbURI := fmt.Sprintf("host=%s user=%s password=%s port=%s database=%s sslmode=disable", constants.POSTGRESQL_IP, 37 | constants.SQL_USER, 38 | constants.SQL_PASSWORD, constants.POSTGRESQL_PORT_NUMBER, constants.DATABASE_NAME) 39 | 40 | db.PostgreSQLClient, err = sql.Open("postgres", dbURI) 41 | if err != nil { 42 | errorCopy := constants.DATABASE_CONNETION_ERROR 43 | errorCopy.Message = err.Error() 44 | return errorCopy 45 | } 46 | 47 | // connect to redis 48 | db.RedisClient = redis.NewClient(&redis.Options{ 49 | Addr: constants.REDIS_IP + ":" + constants.REDIS_PORT_NUMBER, 50 | Password: "", // no password set 51 | DB: 0, // use default DB 52 | }) 53 | 54 | return nil 55 | } 56 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/SwarzChen/url-shortener 2 | 3 | go 1.17 4 | 5 | require ( 6 | github.com/gin-contrib/cors v1.3.1 7 | github.com/gin-gonic/gin v1.7.7 8 | github.com/go-redis/redis/v8 v8.11.5 9 | github.com/lib/pq v1.10.4 10 | github.com/stretchr/testify v1.7.0 11 | github.com/teris-io/shortid v0.0.0-20201117134242-e59966efd125 12 | ) 13 | 14 | require ( 15 | github.com/cespare/xxhash/v2 v2.1.2 // indirect 16 | github.com/davecgh/go-spew v1.1.1 // indirect 17 | github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect 18 | github.com/gin-contrib/sse v0.1.0 // indirect 19 | github.com/go-playground/locales v0.14.0 // indirect 20 | github.com/go-playground/universal-translator v0.18.0 // indirect 21 | github.com/go-playground/validator/v10 v10.10.1 // indirect 22 | github.com/golang/protobuf v1.5.2 // indirect 23 | github.com/json-iterator/go v1.1.12 // indirect 24 | github.com/leodido/go-urn v1.2.1 // indirect 25 | github.com/mattn/go-isatty v0.0.14 // indirect 26 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect 27 | github.com/modern-go/reflect2 v1.0.2 // indirect 28 | github.com/pmezard/go-difflib v1.0.0 // indirect 29 | github.com/ugorji/go/codec v1.2.7 // indirect 30 | golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd // indirect 31 | golang.org/x/sys v0.0.0-20220317061510-51cd9980dadf // indirect 32 | golang.org/x/text v0.3.7 // indirect 33 | golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect 34 | google.golang.org/protobuf v1.27.1 // indirect 35 | gopkg.in/yaml.v2 v2.4.0 // indirect 36 | gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect 37 | ) 38 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= 2 | github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= 3 | github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= 4 | github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= 5 | github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= 6 | github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= 7 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 8 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 9 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 10 | github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= 11 | github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= 12 | github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= 13 | github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= 14 | github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= 15 | github.com/gin-contrib/cors v1.3.1 h1:doAsuITavI4IOcd0Y19U4B+O0dNWihRyX//nn4sEmgA= 16 | github.com/gin-contrib/cors v1.3.1/go.mod h1:jjEJ4268OPZUcU7k9Pm653S7lXUGcqMADzFA61xsmDk= 17 | github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= 18 | github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= 19 | github.com/gin-gonic/gin v1.5.0/go.mod h1:Nd6IXA8m5kNZdNEHMBd93KT+mdY3+bewLgRvmCsR2Do= 20 | github.com/gin-gonic/gin v1.7.7 h1:3DoBmSbJbZAWqXJC3SLjAPfutPJJRN1U5pALB7EeTTs= 21 | github.com/gin-gonic/gin v1.7.7/go.mod h1:axIBovoeJpVj8S3BwE0uPMTeReE4+AfFtqpqaZ1qq1U= 22 | github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A= 23 | github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= 24 | github.com/go-playground/locales v0.12.1/go.mod h1:IUMDtCfWo/w/mtMfIE/IG2K+Ey3ygWanZIBtBW0W2TM= 25 | github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= 26 | github.com/go-playground/locales v0.14.0 h1:u50s323jtVGugKlcYeyzC0etD1HifMjqmJqb8WugfUU= 27 | github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs= 28 | github.com/go-playground/universal-translator v0.16.0/go.mod h1:1AnU7NaIRDWWzGEKwgtJRd2xk99HeFyHw3yid4rvQIY= 29 | github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= 30 | github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/jYrnRPArHwAcmLoJZxyho= 31 | github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA= 32 | github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= 33 | github.com/go-playground/validator/v10 v10.10.1 h1:uA0+amWMiglNZKZ9FJRKUAe9U3RX91eVn1JYXMWt7ig= 34 | github.com/go-playground/validator/v10 v10.10.1/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4dMGDBiPU55YFDl0WbKdWU= 35 | github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI= 36 | github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo= 37 | github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= 38 | github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 39 | github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 40 | github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= 41 | github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= 42 | github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= 43 | github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= 44 | github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= 45 | github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= 46 | github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= 47 | github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= 48 | github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= 49 | github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= 50 | github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= 51 | github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= 52 | github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 53 | github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= 54 | github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 55 | github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= 56 | github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= 57 | github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= 58 | github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= 59 | github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= 60 | github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= 61 | github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= 62 | github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= 63 | github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= 64 | github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= 65 | github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= 66 | github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= 67 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= 68 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= 69 | github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= 70 | github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= 71 | github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw= 72 | github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= 73 | github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w= 74 | github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY= 75 | github.com/lib/pq v1.10.4 h1:SO9z7FRPzA03QhHKJrH5BXA6HU1rS4V2nIVrrNC1iYk= 76 | github.com/lib/pq v1.10.4/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= 77 | github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= 78 | github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= 79 | github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= 80 | github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= 81 | github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 82 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= 83 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 84 | github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= 85 | github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= 86 | github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= 87 | github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= 88 | github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= 89 | github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= 90 | github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= 91 | github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= 92 | github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0= 93 | github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= 94 | github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU= 95 | github.com/onsi/ginkgo/v2 v2.0.0/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c= 96 | github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= 97 | github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= 98 | github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY= 99 | github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE= 100 | github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs= 101 | github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= 102 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 103 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 104 | github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= 105 | github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8= 106 | github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= 107 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 108 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 109 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 110 | github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= 111 | github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 112 | github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= 113 | github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 114 | github.com/teris-io/shortid v0.0.0-20201117134242-e59966efd125 h1:3SNcvBmEPE1YlB1JpVZouslJpI3GBNoiqW7+wb0Rz7w= 115 | github.com/teris-io/shortid v0.0.0-20201117134242-e59966efd125/go.mod h1:M8agBzgqHIhgj7wEn9/0hJUZcrvt9VY+Ln+S1I5Mha0= 116 | github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= 117 | github.com/ugorji/go v1.2.7 h1:qYhyWUUd6WbiM+C6JZAUkIJt/1WrjzNHY9+KCIjVqTo= 118 | github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M= 119 | github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= 120 | github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0= 121 | github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY= 122 | github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= 123 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 124 | golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= 125 | golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= 126 | golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= 127 | golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd h1:XcWmESyNjXJMLahc3mqVQJcgSTDxFxhETVlfk9uGc38= 128 | golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= 129 | golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= 130 | golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 131 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 132 | golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 133 | golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= 134 | golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= 135 | golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk= 136 | golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 h1:CIJ76btIcR3eFI5EgSo6k1qKw9KJexJuRLI9G7Hp5wE= 137 | golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= 138 | golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 139 | golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 140 | golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 141 | golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 142 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 143 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 144 | golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 145 | golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 146 | golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 147 | golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 148 | golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 149 | golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 150 | golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 151 | golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 152 | golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 153 | golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 154 | golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 155 | golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 156 | golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 157 | golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 158 | golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 159 | golang.org/x/sys v0.0.0-20220317061510-51cd9980dadf h1:Fm4IcnUL803i92qDlmB0obyHmosDrxZWxJL3gIeNqOw= 160 | golang.org/x/sys v0.0.0-20220317061510-51cd9980dadf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 161 | golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= 162 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 163 | golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= 164 | golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 165 | golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 166 | golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= 167 | golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= 168 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 169 | golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= 170 | golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= 171 | golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 172 | golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 173 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 174 | golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= 175 | golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 176 | google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= 177 | google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= 178 | google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= 179 | google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= 180 | google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= 181 | google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= 182 | google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= 183 | google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= 184 | google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ= 185 | google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= 186 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 187 | gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 188 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= 189 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= 190 | gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= 191 | gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= 192 | gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE= 193 | gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ= 194 | gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= 195 | gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= 196 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 197 | gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 198 | gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 199 | gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 200 | gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= 201 | gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= 202 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 203 | gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= 204 | gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 205 | -------------------------------------------------------------------------------- /k8s-config/backend-config.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: ConfigMap 3 | metadata: 4 | name: backend-config 5 | data: 6 | REDIS_IP: "10.12.2.9" 7 | POSTGRESQL_IP: "10.12.15.112" 8 | DATABASE_NAME: "postgres" 9 | POSTGRESQL_PORT_NUMBER: "5432" 10 | REDIS_PORT_NUMBER: "6379" 11 | SERVER_HOST: "https://dcard-api.yentingchen.me" -------------------------------------------------------------------------------- /k8s-config/backend-service.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | name: url-shortener-backend 5 | spec: 6 | selector: 7 | pod: url-shortener-backend 8 | ports: 9 | - protocol: TCP 10 | port: 8080 11 | targetPort: 8080 -------------------------------------------------------------------------------- /k8s-config/backend.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | name: url-shortener-backend 5 | labels: 6 | deployment: url-shortener-backend 7 | spec: 8 | replicas: 3 9 | selector: 10 | matchLabels: 11 | pod: url-shortener-backend 12 | template: 13 | metadata: 14 | labels: 15 | name: url-shortener-backend 16 | pod: url-shortener-backend 17 | spec: 18 | containers: 19 | - name: url-shortener-backend 20 | image: IMAGE_NAME 21 | env: 22 | - name: REDIS_IP 23 | valueFrom: 24 | configMapKeyRef: 25 | name: backend-config 26 | key: REDIS_IP 27 | - name: POSTGRESQL_IP 28 | valueFrom: 29 | configMapKeyRef: 30 | name: backend-config 31 | key: POSTGRESQL_IP 32 | - name: DATABASE_NAME 33 | valueFrom: 34 | configMapKeyRef: 35 | name: backend-config 36 | key: DATABASE_NAME 37 | - name: POSTGRESQL_PORT_NUMBER 38 | valueFrom: 39 | configMapKeyRef: 40 | name: backend-config 41 | key: POSTGRESQL_PORT_NUMBER 42 | - name: REDIS_PORT_NUMBER 43 | valueFrom: 44 | configMapKeyRef: 45 | name: backend-config 46 | key: REDIS_PORT_NUMBER 47 | - name: SERVER_HOST 48 | valueFrom: 49 | configMapKeyRef: 50 | name: backend-config 51 | key: SERVER_HOST 52 | 53 | - name: SQL_USER 54 | valueFrom: 55 | configMapKeyRef: 56 | name: backend-secret 57 | key: SQL_USER 58 | - name: SQL_PASSWORD 59 | valueFrom: 60 | configMapKeyRef: 61 | name: backend-secret 62 | key: SQL_PASSWORD 63 | ports: 64 | - containerPort: 8080 65 | -------------------------------------------------------------------------------- /k8s-config/ingress.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: networking.k8s.io/v1 2 | kind: Ingress 3 | metadata: 4 | name: url-shortener-ingress 5 | annotations: 6 | kubernetes.io/ingress.global-static-ip-name: url-shortener-ip 7 | spec: 8 | rules: 9 | - http: 10 | paths: 11 | - path: /v1 12 | pathType: Prefix 13 | backend: 14 | service: 15 | name: url-shortener-backend-v1 16 | port: 17 | number: 8080 18 | -------------------------------------------------------------------------------- /k8s-config/nfs-client.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | name: nfs-client-provisioner 5 | spec: 6 | replicas: 1 7 | selector: 8 | matchLabels: 9 | app: nfs-client-provisioner 10 | strategy: 11 | type: Recreate 12 | template: 13 | metadata: 14 | labels: 15 | app: nfs-client-provisioner 16 | spec: 17 | serviceAccountName: nfs-client-provisioner 18 | containers: 19 | - name: nfs-client-provisioner 20 | image: k8s.gcr.io/sig-storage/nfs-subdir-external-provisioner:v4.0.2 21 | volumeMounts: 22 | - name: nfs-client-root 23 | mountPath: /persistentvolumes 24 | env: 25 | - name: PROVISIONER_NAME 26 | value: mynfs 27 | - name: NFS_SERVER 28 | value: nfs-server.default.svc.cluster.local 29 | - name: NFS_PATH 30 | value: /exports 31 | volumes: 32 | - name: nfs-client-root 33 | nfs: 34 | server: nfs-server.default.svc.cluster.local 35 | path: /exports 36 | --- 37 | apiVersion: storage.k8s.io/v1 38 | kind: StorageClass 39 | metadata: 40 | name: mynfs 41 | provisioner: mynfs 42 | parameters: 43 | archiveOnDelete: "false" 44 | --- 45 | apiVersion: v1 46 | kind: ServiceAccount 47 | metadata: 48 | name: nfs-client-provisioner 49 | --- 50 | kind: ClusterRole 51 | apiVersion: rbac.authorization.k8s.io/v1 52 | metadata: 53 | name: nfs-client-provisioner-runner 54 | rules: 55 | - apiGroups: [""] 56 | resources: ["persistentvolumes"] 57 | verbs: ["get", "list", "watch", "create", "delete"] 58 | - apiGroups: [""] 59 | resources: ["persistentvolumeclaims"] 60 | verbs: ["get", "list", "watch", "update"] 61 | - apiGroups: ["storage.k8s.io"] 62 | resources: ["storageclasses"] 63 | verbs: ["get", "list", "watch"] 64 | - apiGroups: [""] 65 | resources: ["events"] 66 | verbs: ["create", "update", "patch"] 67 | --- 68 | kind: ClusterRoleBinding 69 | apiVersion: rbac.authorization.k8s.io/v1 70 | metadata: 71 | name: run-nfs-client-provisioner 72 | subjects: 73 | - kind: ServiceAccount 74 | name: nfs-client-provisioner 75 | namespace: default 76 | roleRef: 77 | kind: ClusterRole 78 | name: nfs-client-provisioner-runner 79 | apiGroup: rbac.authorization.k8s.io 80 | --- 81 | kind: Role 82 | apiVersion: rbac.authorization.k8s.io/v1 83 | metadata: 84 | name: leader-locking-nfs-client-provisioner 85 | rules: 86 | - apiGroups: [""] 87 | resources: ["endpoints"] 88 | verbs: ["get", "list", "watch", "create", "update", "patch"] 89 | --- 90 | kind: RoleBinding 91 | apiVersion: rbac.authorization.k8s.io/v1 92 | metadata: 93 | name: leader-locking-nfs-client-provisioner 94 | subjects: 95 | - kind: ServiceAccount 96 | name: nfs-client-provisioner 97 | roleRef: 98 | kind: Role 99 | name: leader-locking-nfs-client-provisioner 100 | apiGroup: rbac.authorization.k8s.io 101 | -------------------------------------------------------------------------------- /k8s-config/nfs-server.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: PersistentVolumeClaim 3 | metadata: 4 | name: nfs-pvc 5 | spec: 6 | storageClassName: "standard" 7 | resources: 8 | requests: 9 | storage: 20Gi 10 | accessModes: 11 | - ReadWriteOnce 12 | --- 13 | apiVersion: apps/v1 14 | kind: Deployment 15 | metadata: 16 | name: nfs-server 17 | spec: 18 | replicas: 1 19 | selector: 20 | matchLabels: 21 | role: nfs-server 22 | template: 23 | metadata: 24 | labels: 25 | role: nfs-server 26 | spec: 27 | containers: 28 | - name: nfs-server 29 | image: gcr.io/google_containers/volume-nfs:0.8 30 | ports: 31 | - name: nfs 32 | containerPort: 2049 33 | - name: mountd 34 | containerPort: 20048 35 | - name: rpcbind 36 | containerPort: 111 37 | securityContext: 38 | privileged: true 39 | volumeMounts: 40 | - mountPath: /exports 41 | name: nfs-pvc 42 | volumes: 43 | - name: nfs-pvc 44 | persistentVolumeClaim: 45 | claimName: nfs-pvc 46 | --- 47 | apiVersion: v1 48 | kind: Service 49 | metadata: 50 | name: nfs-server 51 | spec: 52 | ports: 53 | - name: nfs 54 | port: 2049 55 | - name: mountd 56 | port: 20048 57 | - name: rpcbind 58 | port: 111 59 | selector: 60 | role: nfs-server 61 | -------------------------------------------------------------------------------- /k8s-config/postgres.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | apiVersion: v1 3 | kind: PersistentVolumeClaim 4 | metadata: 5 | name: postgre-pvc 6 | spec: 7 | storageClassName: "mynfs" 8 | resources: 9 | requests: 10 | storage: 10Gi 11 | accessModes: 12 | - ReadWriteMany 13 | --- 14 | apiVersion: apps/v1 15 | kind: Deployment 16 | metadata: 17 | name: postgres 18 | spec: 19 | strategy: 20 | rollingUpdate: 21 | maxSurge: 1 22 | maxUnavailable: 1 23 | type: RollingUpdate 24 | replicas: 3 25 | selector: 26 | matchLabels: 27 | app: postgres 28 | template: 29 | metadata: 30 | labels: 31 | app: postgres 32 | spec: 33 | containers: 34 | - name: postgres 35 | image: postgres:10 36 | resources: 37 | limits: 38 | cpu: "1" 39 | memory: "4Gi" 40 | requests: 41 | cpu: "1" 42 | memory: "4Gi" 43 | ports: 44 | - containerPort: 5432 45 | env: 46 | - name: POSTGRES_PASSWORD 47 | valueFrom: 48 | secretKeyRef: 49 | name: postgre-secret 50 | key: postgre-password 51 | - name: PGDATA 52 | value: /var/lib/postgresql/data/pgdata 53 | volumeMounts: 54 | - mountPath: /var/lib/postgresql/data 55 | name: postgre-volume 56 | volumes: 57 | - name: postgre-volume 58 | persistentVolumeClaim: 59 | claimName: postgre-pvc 60 | --- 61 | apiVersion: v1 62 | kind: Service 63 | metadata: 64 | name: postgres 65 | spec: 66 | ports: 67 | - port: 5432 68 | selector: 69 | app: postgres 70 | clusterIP: None 71 | 72 | -------------------------------------------------------------------------------- /k8s-config/redis-config.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: ConfigMap 3 | metadata: 4 | name: redis-config 5 | data: 6 | redis-config: | 7 | maxmemory 2mb 8 | maxmemory-policy allkeys-lru 9 | -------------------------------------------------------------------------------- /k8s-config/redis.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | name: redis 5 | labels: 6 | deployment: redis 7 | spec: 8 | replicas: 3 9 | selector: 10 | matchLabels: 11 | pod: redis 12 | template: 13 | metadata: 14 | labels: 15 | name: redis 16 | pod: redis 17 | spec: 18 | containers: 19 | - name: redis 20 | image: redis:latest 21 | command: 22 | - redis-server 23 | - "/redis-master/redis.conf" 24 | env: 25 | - name: MASTER 26 | value: "true" 27 | ports: 28 | - containerPort: 6379 29 | resources: 30 | limits: 31 | cpu: "0.1" 32 | volumeMounts: 33 | - mountPath: /redis-master-data 34 | name: data 35 | - mountPath: /redis-master 36 | name: config 37 | volumes: 38 | - name: data 39 | emptyDir: {} 40 | - name: config 41 | configMap: 42 | name: redis-config 43 | items: 44 | - key: redis-config 45 | path: redis.conf 46 | --- 47 | apiVersion: v1 48 | kind: Service 49 | metadata: 50 | name: redis 51 | spec: 52 | selector: 53 | pod: redis 54 | ports: 55 | - protocol: TCP 56 | port: 6379 57 | targetPort: 6379 58 | -------------------------------------------------------------------------------- /kustomization.yaml: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | apiVersion: kustomize.config.k8s.io/v1beta1 16 | kind: Kustomization 17 | resources: 18 | - ./k8s-config/backend.yaml 19 | - ./k8s-config/backend-config.yaml 20 | - ./k8s-config/backend-service.yaml 21 | - ./k8s-config/ingress.yaml -------------------------------------------------------------------------------- /middleware/cache.go: -------------------------------------------------------------------------------- 1 | package middleware 2 | 3 | import ( 4 | "github.com/SwarzChen/url-shortener/constants" 5 | "github.com/SwarzChen/url-shortener/database" 6 | "github.com/gin-gonic/gin" 7 | "net/http" 8 | 9 | "context" 10 | ) 11 | 12 | func Cache() gin.HandlerFunc { 13 | return func(c *gin.Context) { 14 | urlId := c.Param("url_id") 15 | if len(urlId) == 0 { 16 | c.JSON(constants.URL_ID_MISSING_ERROR.StatusCode, constants.URL_ID_MISSING_ERROR) 17 | c.Abort() 18 | return 19 | } 20 | 21 | redisClient := database.DatabaseClient.RedisClient 22 | // check whether this id has cache miss before 23 | ctx := context.Background() 24 | idContent, err := redisClient.Get(ctx, urlId).Result() 25 | if err != nil { 26 | // not in cache or expire 27 | c.Next() 28 | return 29 | } 30 | 31 | // this url id does not exist 32 | if idContent == constants.ID_NOT_EXIST { 33 | c.JSON(constants.URL_ID_NOT_EXIST_ERROR.StatusCode, constants.URL_ID_NOT_EXIST_ERROR) 34 | c.Abort() 35 | return 36 | } 37 | 38 | // this url content is in cache 39 | c.Redirect(http.StatusFound, idContent) 40 | c.Abort() 41 | return 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /model/model.go: -------------------------------------------------------------------------------- 1 | package model 2 | 3 | type UrlEntity struct { 4 | Id string `json:"id"` 5 | ShortUrl string `json:"shortUrl"` 6 | } 7 | 8 | type UrlEntityInDB struct { 9 | Id string `json:"id"` 10 | Url string `json:"url"` 11 | ExpireAt string `json:"expireAt"` 12 | CreateAt string `json:"createAt"` 13 | } 14 | -------------------------------------------------------------------------------- /router/router.go: -------------------------------------------------------------------------------- 1 | package router 2 | 3 | import ( 4 | "github.com/SwarzChen/url-shortener/constants" 5 | "github.com/SwarzChen/url-shortener/controller" 6 | "github.com/SwarzChen/url-shortener/middleware" 7 | "github.com/gin-contrib/cors" 8 | "github.com/gin-gonic/gin" 9 | ) 10 | 11 | func InitRoutes() *gin.Engine { 12 | router := gin.Default() 13 | 14 | // Setting up basic cors config 15 | config := cors.DefaultConfig() 16 | config.AllowCredentials = true 17 | config.AllowOrigins = []string{ 18 | "http://localhost:8080", 19 | } 20 | config.AddAllowHeaders("Authorization") 21 | router.Use(cors.New(config)) 22 | 23 | // Define routing path 24 | versionRouter := router.Group("/" + constants.CURRENT_VERSION) 25 | versionRouter.GET("/:url_id/", middleware.Cache(), controller.GetUrlHandler) 26 | versionRouter.POST("/urls/", controller.CreateUrlHandler) 27 | 28 | // Handling route not existing problem 29 | router.NoRoute(controller.MissingRouteHandler) 30 | return router 31 | } 32 | -------------------------------------------------------------------------------- /server.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/SwarzChen/url-shortener/router" 5 | ) 6 | 7 | func main() { 8 | engine := router.InitRoutes() 9 | panic(engine.Run()) 10 | } 11 | -------------------------------------------------------------------------------- /test/cache_test.go: -------------------------------------------------------------------------------- 1 | package test 2 | 3 | import ( 4 | "encoding/json" 5 | "github.com/SwarzChen/url-shortener/constants" 6 | "github.com/stretchr/testify/assert" 7 | "github.com/teris-io/shortid" 8 | "net/http" 9 | "os" 10 | "testing" 11 | ) 12 | 13 | func TestCacheHandlingInvalidUrlQuery(t *testing.T) { 14 | // get server host 15 | serverHost, isExisted := os.LookupEnv("SERVER_HOST") 16 | if !isExisted { 17 | panic("MISSING_SERVER_HOST_ENV_VAR") 18 | } 19 | 20 | // generate short unique id 21 | generator, err := shortid.New(1, shortid.DefaultABC, 2342) 22 | if err != nil { 23 | panic(err.Error()) 24 | } 25 | 26 | notExistedId, err := generator.Generate() 27 | if err != nil { 28 | panic(err.Error()) 29 | } 30 | 31 | // first query 32 | req, err := http.NewRequest("GET", serverHost+"/"+constants.CURRENT_VERSION+"/"+notExistedId, nil) 33 | if err != nil { 34 | panic(err.Error()) 35 | } 36 | 37 | client := &http.Client{} 38 | resp, err := client.Do(req) 39 | if err != nil { 40 | panic(err.Error()) 41 | } 42 | 43 | var rspEntity constants.ErrorEntity 44 | err = json.NewDecoder(resp.Body).Decode(&rspEntity) 45 | if err != nil { 46 | panic(err.Error()) 47 | } 48 | 49 | // first query is not caught by the cache 50 | assert.Equal(t, http.StatusNotFound, rspEntity.StatusCode) 51 | assert.Equal(t, constants.URL_NOT_EXIST_OR_EXPIRE.ErrorCode, rspEntity.ErrorCode) 52 | 53 | // second query 54 | resp, err = client.Do(req) 55 | if err != nil { 56 | panic(err.Error()) 57 | } 58 | defer resp.Body.Close() 59 | 60 | err = json.NewDecoder(resp.Body).Decode(&rspEntity) 61 | if err != nil { 62 | panic(err.Error()) 63 | } 64 | 65 | // second query is caught by cache 66 | assert.Equal(t, http.StatusNotFound, rspEntity.StatusCode) 67 | assert.Equal(t, constants.URL_ID_NOT_EXIST_ERROR.ErrorCode, rspEntity.ErrorCode) 68 | } 69 | -------------------------------------------------------------------------------- /test/error_test.go: -------------------------------------------------------------------------------- 1 | package test 2 | 3 | import ( 4 | "bytes" 5 | "encoding/json" 6 | "github.com/SwarzChen/url-shortener/constants" 7 | "github.com/SwarzChen/url-shortener/model" 8 | "github.com/stretchr/testify/assert" 9 | "net/http" 10 | "os" 11 | "testing" 12 | "time" 13 | ) 14 | 15 | func TestCreateUrlWithInvalidExpireTime(t *testing.T) { 16 | // create invalid expire time 17 | testUrlEntity := model.UrlEntityInDB{ 18 | Url: "test", 19 | ExpireAt: time.Now().In(constants.TIME_ZONE).Add(-time.Hour).Format(time.RFC3339), 20 | } 21 | 22 | body, err := json.Marshal(testUrlEntity) 23 | if err != nil { 24 | panic(err.Error()) 25 | } 26 | 27 | // get server host 28 | serverHost, isExisted := os.LookupEnv("SERVER_HOST") 29 | if !isExisted { 30 | panic("MISSING_SERVER_HOST_ENV_VAR") 31 | } 32 | 33 | req, err := http.NewRequest("POST", serverHost+"/"+constants.CURRENT_VERSION+"/urls/", bytes.NewBuffer(body)) 34 | if err != nil { 35 | panic(err.Error()) 36 | } 37 | 38 | client := &http.Client{} 39 | resp, err := client.Do(req) 40 | if err != nil { 41 | panic(err.Error()) 42 | } 43 | defer resp.Body.Close() 44 | 45 | var rspEntity constants.ErrorEntity 46 | err = json.NewDecoder(resp.Body).Decode(&rspEntity) 47 | if err != nil { 48 | panic(err.Error()) 49 | } 50 | 51 | // checking error handling 52 | assert.Equal(t, http.StatusBadRequest, resp.StatusCode) 53 | assert.Equal(t, constants.INPUT_EXPIRE_TIME_IS_INVALID.ErrorCode, rspEntity.ErrorCode) 54 | } 55 | -------------------------------------------------------------------------------- /test/success_test.go: -------------------------------------------------------------------------------- 1 | package test 2 | 3 | import ( 4 | "bytes" 5 | "encoding/json" 6 | "github.com/SwarzChen/url-shortener/constants" 7 | "github.com/SwarzChen/url-shortener/model" 8 | "github.com/stretchr/testify/assert" 9 | "net/http" 10 | "os" 11 | "strings" 12 | "testing" 13 | "time" 14 | ) 15 | 16 | func TestCreateUrlEntitySuccess(t *testing.T) { 17 | testUrlEntity := model.UrlEntityInDB{ 18 | Url: "test", 19 | ExpireAt: time.Now().In(constants.TIME_ZONE).Add(time.Hour).Format(time.RFC3339), 20 | } 21 | 22 | body, err := json.Marshal(testUrlEntity) 23 | if err != nil { 24 | panic(err.Error()) 25 | } 26 | 27 | // get server host 28 | serverHost, isExisted := os.LookupEnv("SERVER_HOST") 29 | if !isExisted { 30 | panic("MISSING_SERVER_HOST_ENV_VAR") 31 | } 32 | 33 | req, err := http.NewRequest("POST", serverHost+"/"+constants.CURRENT_VERSION+"/urls/", bytes.NewBuffer(body)) 34 | if err != nil { 35 | panic(err.Error()) 36 | } 37 | 38 | client := &http.Client{} 39 | resp, err := client.Do(req) 40 | if err != nil { 41 | panic(err.Error()) 42 | } 43 | defer resp.Body.Close() 44 | 45 | var rspEntity model.UrlEntity 46 | err = json.NewDecoder(resp.Body).Decode(&rspEntity) 47 | if err != nil { 48 | panic(err.Error()) 49 | } 50 | 51 | // split short url to check form 52 | partition := strings.Split(rspEntity.ShortUrl, "/") 53 | 54 | // checking 55 | assert.Equal(t, http.StatusOK, resp.StatusCode) 56 | assert.Equal(t, 5, len(partition)) 57 | assert.Equal(t, serverHost, "http://"+partition[2]) 58 | assert.Equal(t, constants.CURRENT_VERSION, partition[3]) 59 | } 60 | 61 | func TestRedirectToUrlSuccess(t *testing.T) { 62 | testUrl := "test.com" 63 | 64 | // Create url 65 | testUrlEntity := model.UrlEntityInDB{ 66 | Url: testUrl, 67 | ExpireAt: time.Now().In(constants.TIME_ZONE).Add(time.Hour).Format(time.RFC3339), 68 | } 69 | 70 | body, err := json.Marshal(testUrlEntity) 71 | if err != nil { 72 | panic(err.Error()) 73 | } 74 | 75 | // get server host 76 | serverHost, isExisted := os.LookupEnv("SERVER_HOST") 77 | if !isExisted { 78 | panic("MISSING_SERVER_HOST_ENV_VAR") 79 | } 80 | 81 | req, err := http.NewRequest("POST", serverHost+"/"+constants.CURRENT_VERSION+"/urls/", bytes.NewBuffer(body)) 82 | if err != nil { 83 | panic(err.Error()) 84 | } 85 | 86 | client := &http.Client{} 87 | resp, err := client.Do(req) 88 | if err != nil { 89 | panic(err.Error()) 90 | } 91 | 92 | var rspEntity model.UrlEntity 93 | err = json.NewDecoder(resp.Body).Decode(&rspEntity) 94 | if err != nil { 95 | panic(err.Error()) 96 | } 97 | 98 | // test redirecting 99 | req, err = http.NewRequest("GET", serverHost+"/"+constants.CURRENT_VERSION+"/"+rspEntity.Id, nil) 100 | if err != nil { 101 | panic(err.Error()) 102 | } 103 | 104 | client = &http.Client{ 105 | // do not follow the redirect path, return 302 response directly 106 | CheckRedirect: func(req *http.Request, via []*http.Request) error { 107 | // skip first 301 redirect 108 | if len(via) <= 1 { 109 | return nil 110 | } 111 | return http.ErrUseLastResponse 112 | }, 113 | } 114 | 115 | resp, err = client.Do(req) 116 | if err != nil { 117 | panic(err.Error()) 118 | } 119 | defer resp.Body.Close() 120 | 121 | // check redirect location 122 | rsp302Header := resp.Header.Get("Location") 123 | partition := strings.Split(rsp302Header, "/") 124 | rspTargetLocationUrl := partition[len(partition)-1] 125 | 126 | assert.Equal(t, testUrl, rspTargetLocationUrl) 127 | } 128 | --------------------------------------------------------------------------------