Skip to content

Commit

Permalink
STONEBLD-1868 Initial OTP server work
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Oct 24, 2023
1 parent 1da9999 commit 3d26bc1
Show file tree
Hide file tree
Showing 401 changed files with 76,708 additions and 2,164 deletions.
21 changes: 21 additions & 0 deletions Dockerfile.otp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Build the manager binary
FROM registry.access.redhat.com/ubi9/go-toolset:1.19 as builder

# Copy the Go Modules manifests
COPY go.mod go.mod
COPY vendor/ vendor/
COPY design/ design/
COPY gen/ gen/
COPY pkg/ pkg/
COPY cmd/ cmd/

# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o otp-server ./cmd/otp

# Use ubi-minimal as minimal base image to package the manager binary
# Refer to https://catalog.redhat.com/software/containers/ubi8/ubi-minimal/5c359a62bed8bd75a2c3fba8 for more details
FROM registry.access.redhat.com/ubi9/ubi-minimal:9.2
COPY --from=builder /opt/app-root/src/otp-server /
USER 65532:65532

ENTRYPOINT ["/otp-server"]
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ build:
go build -o out/multi-platform-controller cmd/controller/main.go
env GOOS=linux GOARCH=amd64 go build -mod=vendor -o out/multi-platform-controller ./cmd/controller

build-otp:
env GOOS=linux GOARCH=amd64 go build -mod=vendor -o out/otp-server ./cmd/otp

clean:
rm -rf out

Expand Down
39 changes: 39 additions & 0 deletions cmd/otp-cli/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"net/http"
"time"

cli "github.com/redhat-appstudio/multi-platform-controller/gen/http/cli/otp"
goahttp "goa.design/goa/v3/http"
goa "goa.design/goa/v3/pkg"
)

func doHTTP(scheme, host string, timeout int, debug bool) (goa.Endpoint, any, error) {
var (
doer goahttp.Doer
)
{
doer = &http.Client{Timeout: time.Duration(timeout) * time.Second}
if debug {
doer = goahttp.NewDebugDoer(doer)
}
}

return cli.ParseEndpoint(
scheme,
host,
doer,
goahttp.RequestEncoder,
goahttp.ResponseDecoder,
debug,
)
}

func httpUsageCommands() string {
return cli.UsageCommands()
}

func httpUsageExamples() string {
return cli.UsageExamples()
}
120 changes: 120 additions & 0 deletions cmd/otp-cli/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package main

import (
"context"
"encoding/json"
"flag"
"fmt"
"net/url"
"os"
"strings"

goa "goa.design/goa/v3/pkg"
)

func main() {
var (
hostF = flag.String("host", "localhost", "Server host (valid values: localhost)")
addrF = flag.String("url", "", "URL to service host")

verboseF = flag.Bool("verbose", false, "Print request and response details")
vF = flag.Bool("v", false, "Print request and response details")
timeoutF = flag.Int("timeout", 30, "Maximum number of seconds to wait for response")
)
flag.Usage = usage
flag.Parse()
var (
addr string
timeout int
debug bool
)
{
addr = *addrF
if addr == "" {
switch *hostF {
case "localhost":
addr = "http://localhost:8000"
default:
fmt.Fprintf(os.Stderr, "invalid host argument: %q (valid hosts: localhost)\n", *hostF)
os.Exit(1)
}
}
timeout = *timeoutF
debug = *verboseF || *vF
}

var (
scheme string
host string
)
{
u, err := url.Parse(addr)
if err != nil {
fmt.Fprintf(os.Stderr, "invalid URL %#v: %s\n", addr, err)
os.Exit(1)
}
scheme = u.Scheme
host = u.Host
}
var (
endpoint goa.Endpoint
payload any
err error
)
{
switch scheme {
case "http", "https":
endpoint, payload, err = doHTTP(scheme, host, timeout, debug)
default:
fmt.Fprintf(os.Stderr, "invalid scheme: %q (valid schemes: http)\n", scheme)
os.Exit(1)
}
}
if err != nil {
if err == flag.ErrHelp {
os.Exit(0)
}
fmt.Fprintln(os.Stderr, err.Error())
fmt.Fprintln(os.Stderr, "run '"+os.Args[0]+" --help' for detailed usage.")
os.Exit(1)
}

data, err := endpoint(context.Background(), payload)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}

if data != nil {
m, _ := json.MarshalIndent(data, "", " ")
fmt.Println(string(m))
}
}

func usage() {
fmt.Fprintf(os.Stderr, `%s is a command line client for the otp API.
Usage:
%s [-host HOST][-url URL][-timeout SECONDS][-verbose|-v] SERVICE ENDPOINT [flags]
-host HOST: server host (localhost). valid values: localhost
-url URL: specify service URL overriding host URL (http://localhost:8080)
-timeout: maximum number of seconds to wait for response (30)
-verbose|-v: print request and response details (false)
Commands:
%s
Additional help:
%s SERVICE [ENDPOINT] --help
Example:
%s
`, os.Args[0], os.Args[0], indent(httpUsageCommands()), os.Args[0], indent(httpUsageExamples()))
}

func indent(s string) string {
if s == "" {
return ""
}
return " " + strings.Replace(s, "\n", "\n ", -1)
}
115 changes: 115 additions & 0 deletions cmd/otp/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package main

import (
"context"
"log"
"net/http"
"net/url"
"os"
"sync"
"time"

otpsvr "github.com/redhat-appstudio/multi-platform-controller/gen/http/otp/server"
otp "github.com/redhat-appstudio/multi-platform-controller/gen/otp"
goahttp "goa.design/goa/v3/http"
httpmdlwr "goa.design/goa/v3/http/middleware"
"goa.design/goa/v3/middleware"
)

// handleHTTPServer starts configures and starts a HTTP server on the given
// URL. It shuts down the server if any error is received in the error channel.
func handleHTTPServer(ctx context.Context, u *url.URL, otpEndpoints *otp.Endpoints, wg *sync.WaitGroup, errc chan error, logger *log.Logger, debug bool) {

// Setup goa log adapter.
var (
adapter middleware.Logger
)
{
adapter = middleware.NewLogger(logger)
}

// Provide the transport specific request decoder and response encoder.
// The goa http package has built-in support for JSON, XML and gob.
// Other encodings can be used by providing the corresponding functions,
// see goa.design/implement/encoding.
var (
dec = goahttp.RequestDecoder
enc = goahttp.ResponseEncoder
)

// Build the service HTTP request multiplexer and configure it to serve
// HTTP requests to the service endpoints.
var mux goahttp.Muxer
{
mux = goahttp.NewMuxer()
}

// Wrap the endpoints with the transport specific layers. The generated
// server packages contains code generated from the design which maps
// the service input and output data structures to HTTP requests and
// responses.
var (
otpServer *otpsvr.Server
)
{
eh := errorHandler(logger)
otpServer = otpsvr.New(otpEndpoints, mux, dec, enc, eh, nil, nil)
if debug {
servers := goahttp.Servers{
otpServer,
}
servers.Use(httpmdlwr.Debug(mux, os.Stdout))
}
}
// Configure the mux.
otpsvr.Mount(mux, otpServer)

// Wrap the multiplexer with additional middlewares. Middlewares mounted
// here apply to all the service endpoints.
var handler http.Handler = mux
{
handler = httpmdlwr.Log(adapter)(handler)
handler = httpmdlwr.RequestID()(handler)
}

// Start HTTP server using default configuration, change the code to
// configure the server as required by your service.
srv := &http.Server{Addr: u.Host, Handler: handler, ReadHeaderTimeout: time.Second * 60}
for _, m := range otpServer.Mounts {
logger.Printf("HTTP %q mounted on %s %s", m.Method, m.Verb, m.Pattern)
}

(*wg).Add(1)
go func() {
defer (*wg).Done()

// Start HTTP server in a separate goroutine.
go func() {
logger.Printf("HTTP server listening on %q", u.Host)
errc <- srv.ListenAndServe()
}()

<-ctx.Done()
logger.Printf("shutting down HTTP server at %q", u.Host)

// Shutdown gracefully with a 30s timeout.
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

err := srv.Shutdown(ctx)
if err != nil {
logger.Printf("failed to shutdown: %v", err)
}
}()
}

// errorHandler returns a function that writes and logs the given error.
// The function also writes and logs the error unique ID so that it's possible
// to correlate.
func errorHandler(logger *log.Logger) func(context.Context, http.ResponseWriter, error) {
return func(ctx context.Context, w http.ResponseWriter, err error) {
id := ctx.Value(middleware.RequestIDKey).(string)
_, _ = w.Write([]byte("[" + id + "] encoding: " + err.Error()))
logger.Printf("[%s] ERROR: %s", id, err.Error())
}
}
Loading

0 comments on commit 3d26bc1

Please sign in to comment.