[feature] Added new pipeline. Build and check is server starting up

This commit is contained in:
Piotr Biernat 2021-11-20 18:02:18 +01:00
parent a19be84c4b
commit eeaf6e75e8
4 changed files with 149 additions and 62 deletions

View File

@ -1,6 +1,7 @@
---
kind: pipeline
type: docker
name: default
name: Code Quality
steps:
- name: static_check
@ -31,5 +32,54 @@ steps:
volumes:
- name: env_cache
host:
path: /tmp/drone/envs/vegvisir
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
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: {}

44
pkg/handler/http.go Normal file
View File

@ -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()
//}

View File

@ -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
// }
//}

19
vegvisir.test.json Normal file
View File

@ -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"
}
}