Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: First commit #1

Merged
merged 1 commit into from
Apr 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ EXPOSE 1080

ENV API_PORT 1080
ENV API_CSP "default-src 'self'; base-uri 'self'; script-src 'self' 'unsafe-inline' unpkg.com/swagger-ui-dist@3/; style-src 'self' 'unsafe-inline' unpkg.com/swagger-ui-dist@3/; img-src 'self' data:"
ENV API_SWAGGER_TITLE GoWeb
ENV API_SWAGGER_TITLE ketchup

HEALTHCHECK --retries=5 CMD [ "/goweb", "-url", "http://localhost:1080/health" ]
ENTRYPOINT [ "/goweb" ]
HEALTHCHECK --retries=5 CMD [ "/ketchup", "-url", "http://localhost:1080/health" ]
ENTRYPOINT [ "/ketchup" ]

ARG VERSION
ENV VERSION=${VERSION}
Expand All @@ -18,4 +18,4 @@ ARG TARGETARCH

COPY cacert.pem /etc/ssl/certs/ca-certificates.crt
COPY zoneinfo.zip /
COPY release/goweb_${TARGETOS}_${TARGETARCH} /goweb
COPY release/ketchup_${TARGETOS}_${TARGETARCH} /ketchup
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ ifneq ("$(wildcard .env)","")
export
endif

APP_NAME = goweb
APP_NAME = ketchup
PACKAGES ?= ./...

MAIN_SOURCE = cmd/goweb/api.go
MAIN_SOURCE = cmd/ketchup/api.go
MAIN_RUNNER = go run $(MAIN_SOURCE)
ifeq ($(DEBUG), true)
MAIN_RUNNER = dlv debug $(MAIN_SOURCE) --
Expand Down
57 changes: 29 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# goweb
# ketchup

[![Build Status](https://travis-ci.com/ViBiOh/goweb.svg?branch=master)](https://travis-ci.com/ViBiOh/goweb)
[![codecov](https://codecov.io/gh/ViBiOh/goweb/branch/master/graph/badge.svg)](https://codecov.io/gh/ViBiOh/goweb)
[![Go Report Card](https://goreportcard.com/badge/github.com/ViBiOh/goweb)](https://goreportcard.com/report/github.com/ViBiOh/goweb)
[![Dependabot Status](https://api.dependabot.com/badges/status?host=github&repo=ViBiOh/goweb)](https://dependabot.com)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=ViBiOh_goweb&metric=alert_status)](https://sonarcloud.io/dashboard?id=ViBiOh_goweb)
[![Build Status](https://travis-ci.com/ViBiOh/ketchup.svg?branch=master)](https://travis-ci.com/ViBiOh/ketchup)
[![codecov](https://codecov.io/gh/ViBiOh/ketchup/branch/master/graph/badge.svg)](https://codecov.io/gh/ViBiOh/ketchup)
[![Go Report Card](https://goreportcard.com/badge/github.com/ViBiOh/ketchup)](https://goreportcard.com/report/github.com/ViBiOh/ketchup)
[![Dependabot Status](https://api.dependabot.com/badges/status?host=github&repo=ViBiOh/ketchup)](https://dependabot.com)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=ViBiOh_ketchup&metric=alert_status)](https://sonarcloud.io/dashboard?id=ViBiOh_ketchup)

## CI

Expand All @@ -15,49 +15,50 @@ Following variables are required for CI:
| **DOMAIN** | for setting Traefik domain for app |
| **DOCKER_USER** | for publishing Docker image |
| **DOCKER_PASS** | for publishing Docker image |
| **SCRIPTS_NO_INTERACTIVE** | for disabling prompt in CI |

## Usage

```bash
Usage of api:
Usage of ketchup:
-address string
[http] Listen address {API_ADDRESS}
[http] Listen address {KETCHUP_ADDRESS}
-cert string
[http] Certificate file {API_CERT}
[http] Certificate file {KETCHUP_CERT}
-corsCredentials
[cors] Access-Control-Allow-Credentials {API_CORS_CREDENTIALS}
[cors] Access-Control-Allow-Credentials {KETCHUP_CORS_CREDENTIALS}
-corsExpose string
[cors] Access-Control-Expose-Headers {API_CORS_EXPOSE}
[cors] Access-Control-Expose-Headers {KETCHUP_CORS_EXPOSE}
-corsHeaders string
[cors] Access-Control-Allow-Headers {API_CORS_HEADERS} (default "Content-Type")
[cors] Access-Control-Allow-Headers {KETCHUP_CORS_HEADERS} (default "Content-Type")
-corsMethods string
[cors] Access-Control-Allow-Methods {API_CORS_METHODS} (default "GET")
[cors] Access-Control-Allow-Methods {KETCHUP_CORS_METHODS} (default "GET")
-corsOrigin string
[cors] Access-Control-Allow-Origin {API_CORS_ORIGIN} (default "*")
[cors] Access-Control-Allow-Origin {KETCHUP_CORS_ORIGIN} (default "*")
-csp string
[owasp] Content-Security-Policy {API_CSP} (default "default-src 'self'; base-uri 'self'")
[owasp] Content-Security-Policy {KETCHUP_CSP} (default "default-src 'self'; base-uri 'self'")
-frameOptions string
[owasp] X-Frame-Options {API_FRAME_OPTIONS} (default "deny")
[owasp] X-Frame-Options {KETCHUP_FRAME_OPTIONS} (default "deny")
-githubToken string
[github] OAuth Token {KETCHUP_GITHUB_TOKEN}
-graceDuration string
[http] Grace duration when SIGTERM received {API_GRACE_DURATION} (default "15s")
[http] Grace duration when SIGTERM received {KETCHUP_GRACE_DURATION} (default "15s")
-hsts
[owasp] Indicate Strict Transport Security {API_HSTS} (default true)
[owasp] Indicate Strict Transport Security {KETCHUP_HSTS} (default true)
-key string
[http] Key file {API_KEY}
-location string
[hello] TimeZone for displaying current time {API_LOCATION} (default "Europe/Paris")
[http] Key file {KETCHUP_KEY}
-okStatus int
[http] Healthy HTTP Status code {API_OK_STATUS} (default 204)
[http] Healthy HTTP Status code {KETCHUP_OK_STATUS} (default 204)
-port uint
[http] Listen port {API_PORT} (default 1080)
[http] Listen port {KETCHUP_PORT} (default 1080)
-prometheusPath string
[prometheus] Path for exposing metrics {API_PROMETHEUS_PATH} (default "/metrics")
[prometheus] Path for exposing metrics {KETCHUP_PROMETHEUS_PATH} (default "/metrics")
-swaggerTitle string
[swagger] API Title {API_SWAGGER_TITLE} (default "API")
[swagger] API Title {KETCHUP_SWAGGER_TITLE} (default "API")
-swaggerVersion string
[swagger] API Version {API_SWAGGER_VERSION} (default "1.0.0")
[swagger] API Version {KETCHUP_SWAGGER_VERSION} (default "1.0.0")
-url string
[alcotest] URL to check {API_URL}
[alcotest] URL to check {KETCHUP_URL}
-userAgent string
[alcotest] User-Agent for check {API_USER_AGENT} (default "Alcotest")
[alcotest] User-Agent for check {KETCHUP_USER_AGENT} (default "Alcotest")
```
29 changes: 12 additions & 17 deletions cmd/goweb/api.go → cmd/ketchup/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ import (
"flag"
"net/http"
"os"
"strings"

"github.com/ViBiOh/goweb/pkg/dump"
"github.com/ViBiOh/goweb/pkg/hello"
"github.com/ViBiOh/httputils/v3/pkg/alcotest"
"github.com/ViBiOh/httputils/v3/pkg/cors"
"github.com/ViBiOh/httputils/v3/pkg/httputils"
"github.com/ViBiOh/httputils/v3/pkg/logger"
"github.com/ViBiOh/httputils/v3/pkg/owasp"
"github.com/ViBiOh/httputils/v3/pkg/prometheus"
"github.com/ViBiOh/httputils/v3/pkg/swagger"
"github.com/ViBiOh/ketchup/pkg/github"
)

const (
Expand All @@ -23,7 +21,7 @@ const (
)

func main() {
fs := flag.NewFlagSet("api", flag.ExitOnError)
fs := flag.NewFlagSet("ketchup", flag.ExitOnError)

serverConfig := httputils.Flags(fs, "")
alcotestConfig := alcotest.Flags(fs, "")
Expand All @@ -32,7 +30,7 @@ func main() {
corsConfig := cors.Flags(fs, "cors")
swaggerConfig := swagger.Flags(fs, "swagger")

helloConfig := hello.Flags(fs, "")
githubConfig := github.Flags(fs, "github")

logger.Fatal(fs.Parse(os.Args[1:]))

Expand All @@ -43,23 +41,20 @@ func main() {
server.Middleware(owasp.New(owaspConfig).Middleware)
server.Middleware(cors.New(corsConfig).Middleware)

swaggerApp, err := swagger.New(swaggerConfig, server.Swagger, hello.Swagger)
githubApp := github.New(githubConfig)

viwsRelease, err := githubApp.LastRelease("vibioh", "viws")
if err != nil {
logger.Fatal(err)
}
logger.Info("%+v", viwsRelease)

swaggerApp, err := swagger.New(swaggerConfig, server.Swagger)
logger.Fatal(err)

helloHandler := http.StripPrefix(helloPath, hello.Handler(helloConfig))
dumpHandler := http.StripPrefix(dumpPath, dump.Handler())
swaggerHandler := swaggerApp.Handler()

handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, helloPath) {
helloHandler.ServeHTTP(w, r)
return
}
if strings.HasPrefix(r.URL.Path, dumpPath) {
dumpHandler.ServeHTTP(w, r)
return
}

swaggerHandler.ServeHTTP(w, r)
})

Expand Down
22 changes: 11 additions & 11 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ version: "2"

services:
api:
image: ${DOCKER_USER}/goweb
image: ${DOCKER_USER}/ketchup
labels:
traefik.http.routers.goweb-http.rule: Host(`${DOMAIN}`)
traefik.http.routers.goweb-http.entrypoints: http
traefik.http.routers.goweb-http.middlewares: redirect@file
traefik.http.routers.goweb-https.rule: Host(`${DOMAIN}`)
traefik.http.routers.goweb-https.entrypoints: https
traefik.http.routers.goweb-https.middlewares: compress@file, web-limit@file
traefik.http.routers.goweb-https.tls: "true"
traefik.http.routers.goweb-https.tls.certresolver: default
traefik.http.services.goweb-service.loadbalancer.server.port: "1080"
traefik.http.routers.ketchup-http.rule: Host(`${DOMAIN}`)
traefik.http.routers.ketchup-http.entrypoints: http
traefik.http.routers.ketchup-http.middlewares: redirect@file
traefik.http.routers.ketchup-https.rule: Host(`${DOMAIN}`)
traefik.http.routers.ketchup-https.entrypoints: https
traefik.http.routers.ketchup-https.middlewares: compress@file, web-limit@file
traefik.http.routers.ketchup-https.tls: "true"
traefik.http.routers.ketchup-https.tls.certresolver: default
traefik.http.services.ketchup-service.loadbalancer.server.port: "1080"
environment:
API_CSP: "default-src 'self'; base-uri 'self'; script-src 'self' 'unsafe-inline' unpkg.com/swagger-ui-dist@3/; style-src 'self' 'unsafe-inline' unpkg.com/swagger-ui-dist@3/; img-src 'self' data:"
API_SWAGGER_TITLE: "GoWeb"
API_SWAGGER_TITLE: "ketchup"
restart: on-failure:5
read_only: true
cpu_shares: 128
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/ViBiOh/goweb
module github.com/ViBiOh/ketchup

go 1.14

Expand Down
80 changes: 0 additions & 80 deletions pkg/dump/dump.go

This file was deleted.

75 changes: 75 additions & 0 deletions pkg/github/github.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package github

import (
"context"
"encoding/json"
"flag"
"fmt"
"strings"

"github.com/ViBiOh/httputils/v3/pkg/flags"
"github.com/ViBiOh/httputils/v3/pkg/request"
)

const (
apiURL = "https://api.github.com"
)

// Release describes a Github Release
type Release struct {
TagName string `json:"tag_name"`
Body string `json:"body"`
}

// App of package
type App interface {
LastRelease(owner, repository string) (Release, error)
}

// Config of package
type Config struct {
token *string
}

type app struct {
token string
}

// Flags adds flags for configuring package
func Flags(fs *flag.FlagSet, prefix string) Config {
return Config{
token: flags.New(prefix, "github").Name("Token").Default("").Label("OAuth Token").ToString(fs),
}
}

// New creates new App from Config
func New(config Config) App {
return &app{
token: strings.TrimSpace(*config.token),
}
}

func (a app) newClient() *request.Request {
return request.New().Header("Authorization", fmt.Sprintf("token %s", a.token))
}

func (a app) LastRelease(owner, repository string) (Release, error) {
var release Release

req := a.newClient()
resp, err := req.Get(fmt.Sprintf("%s/repos/%s/%s/releases/latest", apiURL, owner, repository)).Send(context.Background(), nil)
if err != nil {
return release, fmt.Errorf("unable to get latest release for %s/%s: %s", owner, repository, err)
}

payload, err := request.ReadBodyResponse(resp)
if err != nil {
return release, fmt.Errorf("unable to read release body for %s/%s: %s", owner, repository, err)
}

if err := json.Unmarshal(payload, &release); err != nil {
return release, fmt.Errorf("unable to parse release body for %s/%s: %s", owner, repository, err)
}

return release, err
}
Loading