diff --git a/.drone.yml b/.drone.yml index 7f3e920..5f3fb32 100644 --- a/.drone.yml +++ b/.drone.yml @@ -1,35 +1,85 @@ +--- kind: pipeline type: docker -name: default +name: Code Quality steps: - - name: static_check - image: golang:1.17 - commands: - - go install honnef.co/go/tools/cmd/staticcheck@latest - - staticcheck -checks all ./pkg/... - volumes: - - name: env_cache - path: /go +- name: static_check + image: golang:1.17 + commands: + - go install honnef.co/go/tools/cmd/staticcheck@latest + - staticcheck -checks all ./pkg/... + volumes: + - name: env_cache + path: /go - - name: lint - image: golang:1.17 - commands: - - go install golang.org/x/lint/golint@latest - - golint -set_exit_status ./pkg/... - volumes: - - name: env_cache - path: /go +- name: lint + image: golang:1.17 + commands: + - go install golang.org/x/lint/golint@latest + - golint -set_exit_status ./pkg/... + volumes: + - name: env_cache + path: /go - - name: vet - image: golang:1.17 - commands: - - go vet ./pkg/... - volumes: - - name: env_cache - path: /go +- name: vet + image: golang:1.17 + commands: + - go vet ./pkg/... + volumes: + - name: env_cache + path: /go volumes: +- name: env_cache + temp: {} + +--- +kind: pipeline +type: docker +name: Test + +steps: +- name: modules_install + image: golang:1.17 + commands: + - go mod tidy && go mod download + volumes: - name: env_cache - host: - path: /tmp/drone/envs/vegvisir + path: /go + +- name: build + image: golang:1.17 + commands: + - go build -o /tmp/vegvisir pkg/main.go + volumes: + - name: tmp_cache + path: /tmp + - name: env_cache + path: /go + +- name: run + image: golang:1.17 + detach: true + commands: + - echo $(hostname) >> /tmp/server_hostname + - /tmp/vegvisir -c ./vegvisir.test.json + volumes: + - name: tmp_cache + path: /tmp + +- name: test + image: golang:1.17 + commands: +# - sleep 5 + - server_hostname=$(cat /tmp/server_hostname) + - "[ $(curl -fsS http://$server_hostname:8080/health | grep OK | wc -l) = 0 ] && { echo 'Not running.'; return 1; } || { echo 'Vegvisir is running.'; return 0; }" + volumes: + - name: tmp_cache + path: /tmp + +volumes: +- name: env_cache + temp: {} +- name: tmp_cache + temp: {} diff --git a/pkg/handler/http.go b/pkg/handler/http.go new file mode 100644 index 0000000..1d3e179 --- /dev/null +++ b/pkg/handler/http.go @@ -0,0 +1,44 @@ +// ___ ____ ___ ___ +// \ \ / / | _ | __| \ \ / / || | __ || || _ | +// \ \/ / |___ | |__ \ \/ / || |___ || ||___| +// \ / | _ | _ | \ / || __ | || ||\\ +// \/ |___ |___ | \/ || ____| || || \\ +// +// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License +// Repo: https://git.pbiernat.dev/golang/vegvisir + +// Package handler contain handlers for various protocols +package handler + +//func (s *Server) httpHandler(ctx *fasthttp.RequestCtx) { +// // http := client.NewHttpClient(ctx) +// +// // move all below logic to concrete handler or sth.... +// reqURL, sReqURL, sReqMethod := ctx.RequestURI(), string(ctx.RequestURI()), string(ctx.Method()) +// //log.Println("Incoming request:", sReqMethod, sReqURL) +// +// found, route := s.router.FindByRequestURL(reqURL) +// if !found { +// // FIXME: return 404 or 5xx error in response? Maybe define it in concrete Backend config? +// ctx.SetStatusCode(fasthttp.StatusNotFound) +// //ctx.SetConnectionClose() +// +// log.Println("404:", sReqMethod, sReqURL) +// return +// } +// +// response, err := s.respCM.Fetch(sReqURL, sReqMethod, route) +// if err != nil { +// // FIXME: Response read error(sending 500 error response) +// ctx.SetStatusCode(fasthttp.StatusInternalServerError) +// //ctx.SetConnectionClose() // not sure if change abything with connection: keep-alive/close issue ^^ +// +// log.Println("Response read error(sending 500 error response)", err) +// return +// } +// +// ctx.Response.Header.SetBytesV(fasthttp.HeaderContentType, response.Headers.ContentType()) +// ctx.SetStatusCode(response.Code) +// ctx.SetBodyString(response.Body) +// //ctx.SetConnectionClose() +//} diff --git a/pkg/server/server.go b/pkg/server/server.go index f9065e1..4be2e31 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -19,6 +19,7 @@ import ( "log" "os" "os/signal" + "strings" "syscall" "time" ) @@ -104,7 +105,15 @@ func (s *Server) mainHandler(ctx *fasthttp.RequestCtx) { // move all below logic to concrete handler or sth.... reqURL, sReqURL, sReqMethod := ctx.RequestURI(), string(ctx.RequestURI()), string(ctx.Method()) - //log.Println("Incoming request:", sReqMethod, sReqURL) + log.Println("Incoming request:", sReqMethod, sReqURL) + + if strings.Contains(sReqURL, "/health") { // quick and tmp fix, hack... + ctx.SetStatusCode(fasthttp.StatusOK) + ctx.SetContentType("application/json") + ctx.SetBodyString("{\"health\":\"OK\"}") + + return + } found, route := s.router.FindByRequestURL(reqURL) if !found { @@ -117,7 +126,6 @@ func (s *Server) mainHandler(ctx *fasthttp.RequestCtx) { } response, err := s.respCM.Fetch(sReqURL, sReqMethod, route) - //err, response := s.processUrl(sReqURL, sReqMethod, route) if err != nil { // FIXME: Response read error(sending 500 error response) ctx.SetStatusCode(fasthttp.StatusInternalServerError) @@ -132,37 +140,3 @@ func (s *Server) mainHandler(ctx *fasthttp.RequestCtx) { ctx.SetBodyString(response.Body) //ctx.SetConnectionClose() } - -//func (s *Server) processUrl(url, method string, route *cache.RouteCache) (error, *cache.ResponseCache) { -// // handle response caching -// cacheKey := method + "_" + url -// if ok, data := s.respCM.load(cacheKey, &cache.ResponseCache{}); ok { -// log.Println("Read resp from cache: ", route.TargetURL, url) -// -// return nil, data.(*cache.ResponseCache) // FIXME #67 - simplify -// } else { -// //log.Println("Send req to backend url: ", route.TargetURL, url) -// ////start := time.Now() -// //bckReq := fasthttp.AcquireRequest() -// //bckResp := fasthttp.AcquireResponse() -// //defer fasthttp.ReleaseRequest(bckReq) -// //defer fasthttp.ReleaseResponse(bckResp) -// // -// //// copy headers from backend response and prepare request for backend - separate -// //bckReq.SetRequestURI(route.TargetURL) -// //bckReq.Header.SetMethod(method) -// // -// //err := fasthttp.Do(bckReq, bckResp) -// //if err != nil { -// // return err, nil -// //} -// // -// //body, code := bckResp.Body(), bckResp.StatusCode() -// ////log.Printf("%s, %s, %d bytes\n", url, time.Since(start), len(body)) -// //// save response to cache -// //respCache := cache.NewResponseCache(url, method, body, code, &bckResp.Header) -// //s.respCM.save(cacheKey, respCache) -// // -// //return nil, respCache -// } -//} diff --git a/vegvisir.test.json b/vegvisir.test.json new file mode 100644 index 0000000..1dd6889 --- /dev/null +++ b/vegvisir.test.json @@ -0,0 +1,19 @@ +{ + "server": { + "address": "0.0.0.0", + "port": 8080 + }, + "backends": { + "news-app": { + "prefixURL": "/", + "backendAddress": "http://127.0.0.1:3030/api/news", + "routes": [{ + "pattern": "(.*)", + "target": "$1" + }] + } + }, + "cache": { + "type": "memory" + } +}