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

Add external requests handling for ratsd core #17

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
413 changes: 413 additions & 0 deletions api/api.gen.go

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions api/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package: api
output: api.gen.go
generate:
std-http-server: true
models: true
embedded-spec: true
3 changes: 3 additions & 0 deletions api/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package api

//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=config.yaml ../docs/api/ratsd.yaml
38 changes: 38 additions & 0 deletions api/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package api

import (
"encoding/json"
"net/http"

"go.uber.org/zap"
)

// Ensure that we implement the server interface
var _ ServerInterface = (*Server)(nil)

type Server struct{
logger *zap.SugaredLogger
}

func NewServer(logger *zap.SugaredLogger) *Server {
return &Server{
logger: logger,
}
}

func (s *Server) RatsdChares(w http.ResponseWriter, r *http.Request, param RatsdCharesParams) {
var requestData ChaResRequest

err := json.NewDecoder(r.Body).Decode(&requestData)
if err != nil {
s.logger.Error("fail to retrieve nonce from request")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

s.logger.Info("request nonce: ", requestData.Nonce)
s.logger.Info("request media type: ", *(param.Accept))
w.Header().Set("Content-Type", "application/eat+jwt; eat_profile=\"tag:github.com,2024:veraison/ratsd\"")
w.WriteHeader(http.StatusOK)
w.Write([]byte("hello, rastd!"))
}
5 changes: 5 additions & 0 deletions cmd/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
logging:
level: debug # valid levels: error, warning, info, debug
ratsd:
listen-addr: 0.0.0.0:8888
protocol: http
77 changes: 77 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package main

import (
"errors"
"net/http"

"github.com/veraison/ratsd/api"
"github.com/veraison/services/config"
"github.com/veraison/services/log"
)

var (
DefaultListenAddr = "localhost:8888"
)

type cfg struct {
ListenAddr string `mapstructure:"listen-addr" valid:"dialstring"`
Protocol string `mapstructure:"protocol" valid:"in(http|https)"`
Cert string `mapstructure:"cert" config:"zerodefault"`
CertKey string `mapstructure:"cert-key" config:"zerodefault"`
}

func (o cfg) Validate() error {
if o.Protocol == "https" && (o.Cert == "" || o.CertKey == "") {
return errors.New(`both cert and cert-key must be specified when protocol is "https"`)
}

return nil
}

func main() {
config.CmdLine()

v, err := config.ReadRawConfig(*config.File, false)
if err != nil {
log.Fatalf("Could not read config sources: %v", err)
}

cfg := cfg{
ListenAddr: DefaultListenAddr,
Protocol: "https",
}

subs, err := config.GetSubs(v, "ratsd", "*logging")
if err != nil {
log.Fatal(err)
}

classifiers := map[string]interface{}{"ratsd": "core"}
if err := log.Init(subs["logging"], classifiers); err != nil {
log.Fatalf("could not configure logging: %v", err)
}

log.Infow("Initializing ratsd core")

loader := config.NewLoader(&cfg)
if err = loader.LoadFromViper(subs["ratsd"]); err != nil {
log.Fatalf("Could not load config: %v", err)
}

svr := api.NewServer(log.Named("api"))
r := http.NewServeMux()
h := api.HandlerFromMux(svr, r)

s := &http.Server{
Handler: h,
Addr: cfg.ListenAddr,
}

if cfg.Protocol == "https" {
log.Infow("initializing ratsd HTTPS service", "address", cfg.ListenAddr)
log.Fatal(s.ListenAndServeTLS(cfg.Cert, cfg.CertKey))
} else {
log.Infow("initializing ratsd HTTP service", "address", cfg.ListenAddr)
log.Fatal(s.ListenAndServe())
}
}
73 changes: 72 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,74 @@
module github.com/veraison/ratsd

go 1.20
go 1.22

toolchain go1.22.7

require (
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
github.com/bytedance/sonic v1.11.3 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.0 // indirect
github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/getkin/kin-openapi v0.127.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.9.1 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.19.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/go-hclog v1.2.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/invopop/yaml v0.3.1 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/moogar0880/problems v0.1.1 // indirect
github.com/oapi-codegen/oapi-codegen/v2 v2.4.1 // indirect
github.com/oapi-codegen/runtime v1.1.1 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.2.0 // indirect
github.com/perimeterx/marshmallow v1.1.5 // indirect
github.com/petar-dambovaliev/aho-corasick v0.0.0-20211021192214-5ab2d9280aa9 // indirect
github.com/speakeasy-api/openapi-overlay v0.9.0 // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.13.0 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/veraison/services v0.0.2412 // indirect
github.com/vmware-labs/yaml-jsonpath v0.3.2 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.23.0 // indirect
golang.org/x/arch v0.7.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading
Loading