Unverified Commit b6b885b4 authored by Kevin Lyda's avatar Kevin Lyda
Browse files

Initial server.

parent 322a00d3
Loading
Loading
Loading
Loading
+24 −24
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ import (
type ServerInterface interface {
	// Returns main page
	// (GET /)
	Index(w http.ResponseWriter, r *http.Request)
	GetIndex(w http.ResponseWriter, r *http.Request)
}

// ServerInterfaceWrapper converts contexts to parameters.
@@ -37,12 +37,12 @@ type ServerInterfaceWrapper struct {

type MiddlewareFunc func(http.Handler) http.Handler

// Index operation middleware
func (siw *ServerInterfaceWrapper) Index(w http.ResponseWriter, r *http.Request) {
// GetIndex operation middleware
func (siw *ServerInterfaceWrapper) GetIndex(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()

	handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		siw.Handler.Index(w, r)
		siw.Handler.GetIndex(w, r)
	}))

	for _, middleware := range siw.HandlerMiddlewares {
@@ -166,24 +166,24 @@ func HandlerWithOptions(si ServerInterface, options StdHTTPServerOptions) http.H
		ErrorHandlerFunc:   options.ErrorHandlerFunc,
	}

	m.HandleFunc("GET "+options.BaseURL+"/", wrapper.Index)
	m.HandleFunc("GET "+options.BaseURL+"/", wrapper.GetIndex)

	return m
}

type IndexRequestObject struct {
type GetIndexRequestObject struct {
}

type IndexResponseObject interface {
	VisitIndexResponse(w http.ResponseWriter) error
type GetIndexResponseObject interface {
	VisitGetIndexResponse(w http.ResponseWriter) error
}

type Index200TexthtmlResponse struct {
type GetIndex200TexthtmlResponse struct {
	Body          io.Reader
	ContentLength int64
}

func (response Index200TexthtmlResponse) VisitIndexResponse(w http.ResponseWriter) error {
func (response GetIndex200TexthtmlResponse) VisitGetIndexResponse(w http.ResponseWriter) error {
	w.Header().Set("Content-Type", "text/html")
	if response.ContentLength != 0 {
		w.Header().Set("Content-Length", fmt.Sprint(response.ContentLength))
@@ -201,7 +201,7 @@ func (response Index200TexthtmlResponse) VisitIndexResponse(w http.ResponseWrite
type StrictServerInterface interface {
	// Returns main page
	// (GET /)
	Index(ctx context.Context, request IndexRequestObject) (IndexResponseObject, error)
	GetIndex(ctx context.Context, request GetIndexRequestObject) (GetIndexResponseObject, error)
}

type StrictHandlerFunc = strictnethttp.StrictHTTPHandlerFunc
@@ -233,23 +233,23 @@ type strictHandler struct {
	options     StrictHTTPServerOptions
}

// Index operation middleware
func (sh *strictHandler) Index(w http.ResponseWriter, r *http.Request) {
	var request IndexRequestObject
// GetIndex operation middleware
func (sh *strictHandler) GetIndex(w http.ResponseWriter, r *http.Request) {
	var request GetIndexRequestObject

	handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, request interface{}) (interface{}, error) {
		return sh.ssi.Index(ctx, request.(IndexRequestObject))
		return sh.ssi.GetIndex(ctx, request.(GetIndexRequestObject))
	}
	for _, middleware := range sh.middlewares {
		handler = middleware(handler, "Index")
		handler = middleware(handler, "GetIndex")
	}

	response, err := handler(r.Context(), w, r, request)

	if err != nil {
		sh.options.ResponseErrorHandlerFunc(w, r, err)
	} else if validResponse, ok := response.(IndexResponseObject); ok {
		if err := validResponse.VisitIndexResponse(w); err != nil {
	} else if validResponse, ok := response.(GetIndexResponseObject); ok {
		if err := validResponse.VisitGetIndexResponse(w); err != nil {
			sh.options.ResponseErrorHandlerFunc(w, r, err)
		}
	} else if response != nil {
@@ -260,12 +260,12 @@ func (sh *strictHandler) Index(w http.ResponseWriter, r *http.Request) {
// Base64 encoded, gzipped, json marshaled Swagger object
var swaggerSpec = []string{

	"H4sIAAAAAAAC/2RRwY7UMAz9leidq3aAW05wHAECsbsn4JBtPW1E41SOO2w16r8jh1k0Yi5VYz+/9/x8",
	"QeRThr+gz6yhV/ulFOIMj190jvx+3obQRkIDDong8dHK7tM2BDRYxZCT6lJ8171i9wYDlV7iojEzPB4n",
	"citHLe43PbunY4sGc+yJC5nilfnz8fGOMi/EJa/SU5tl7K5DXYpqKkqSypfTA8k59nQzNkZtr266ukdX",
	"5dFAo86GfKp2+sxnEiVBgzNJ+Wv3TXtoD8Zv6mGJ8HhXSw2WoFMxz519RqqJ3S/7HAq5JYzkTlmcTuQe",
	"vn5ofzAqpwSDHgd4HHmgFzQQKkvmQpX77eHwehPiqqD0ot2kabZH6SdKoZa3xXYpKpFH7Ptd8ClErj5g",
	"vbKmFGSDxzfSVbi4274BSCwF+O+X/+5Q4/sXKfaf+58AAAD//yaFJNI8AgAA",
	"H4sIAAAAAAAC/2RRTW/cIBD9K+idkdm2N07tqVq1VasmufVC8KyNagYEYzdW5P9eQTbSSntBMPPmfQyv",
	"CHxJsK/wicV5aVeKLiyw+Etb4M/LProhEDTYRYLFt1ZW3/fRQWMtDTmL5GqNecceGiNVX0KWkBgWjzOp",
	"lYNU9Y+e1dN5gMYSPHGlpnhl/nF+vKNMmbimtXgaUpnMdcjEIE1FqMT68/JAZQuebsamIMPVjek5TJeH",
	"hgRZGvKp2/GJNypCBRoblfpm98NwGk6Nv6m7HGDxqZc0spO5Ns+mHRP1jd2HfXaVVHYTqUsqSmZSD7++",
	"DH8YnbO4Bj2PsPhKcuaRXqBRqObElTr9x9Pp/VuIu4jQi5hZ4tIe1c8UXS/vucWpUgJPOI673UcXuFtB",
	"69U1Rld2WPwmWQtXdds/jv8BAAD//xuhfPkSAgAA",
}

// GetSwagger returns the content of the embedded swagger specification file

api/implementation.go

0 → 100644
+28 −0
Original line number Diff line number Diff line
// Package api implements the units API.
package api

import (
	"context"
	"strings"
)

// UnitsAPI implements the units web ui.
type UnitsAPI struct {
}

// GetIndex implements the main page.
func (ua UnitsAPI) GetIndex(_ context.Context, _ GetIndexRequestObject) (GetIndexResponseObject, error) {
	body := `<html>
<head>
    <script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
</head>
<body>
    <h1 x-data="{ message: 'I ❤️ Alpine' }" x-text="message"></h1>
</body>
</html>`
	response := GetIndex200TexthtmlResponse{
		Body:          strings.NewReader(body),
		ContentLength: int64(len(body)),
	}
	return response, nil
}

api/server.gen.go

deleted100644 → 0
+0 −135
Original line number Diff line number Diff line
// Package api provides primitives to interact with the openapi HTTP API.
//
// Code generated by github.com/deepmap/oapi-codegen version v1.16.3 DO NOT EDIT.
package api

import (
	"context"
	"fmt"
	"io"
	"net/http"

	"github.com/labstack/echo/v4"
	strictecho "github.com/oapi-codegen/runtime/strictmiddleware/echo"
)

// ServerInterface represents all server handlers.
type ServerInterface interface {
	// Returns main page
	// (GET /)
	Index(ctx echo.Context) error
}

// ServerInterfaceWrapper converts echo contexts to parameters.
type ServerInterfaceWrapper struct {
	Handler ServerInterface
}

// Index converts echo context to params.
func (w *ServerInterfaceWrapper) Index(ctx echo.Context) error {
	var err error

	// Invoke the callback with all the unmarshaled arguments
	err = w.Handler.Index(ctx)
	return err
}

// This is a simple interface which specifies echo.Route addition functions which
// are present on both echo.Echo and echo.Group, since we want to allow using
// either of them for path registration
type EchoRouter interface {
	CONNECT(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	DELETE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	GET(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	HEAD(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	OPTIONS(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	PATCH(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	POST(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	PUT(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	TRACE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
}

// RegisterHandlers adds each server route to the EchoRouter.
func RegisterHandlers(router EchoRouter, si ServerInterface) {
	RegisterHandlersWithBaseURL(router, si, "")
}

// Registers handlers, and prepends BaseURL to the paths, so that the paths
// can be served under a prefix.
func RegisterHandlersWithBaseURL(router EchoRouter, si ServerInterface, baseURL string) {

	wrapper := ServerInterfaceWrapper{
		Handler: si,
	}

	router.GET(baseURL+"/", wrapper.Index)

}

type IndexRequestObject struct {
}

type IndexResponseObject interface {
	VisitIndexResponse(w http.ResponseWriter) error
}

type Index200TexthtmlResponse struct {
	Body          io.Reader
	ContentLength int64
}

func (response Index200TexthtmlResponse) VisitIndexResponse(w http.ResponseWriter) error {
	w.Header().Set("Content-Type", "text/html")
	if response.ContentLength != 0 {
		w.Header().Set("Content-Length", fmt.Sprint(response.ContentLength))
	}
	w.WriteHeader(200)

	if closer, ok := response.Body.(io.ReadCloser); ok {
		defer closer.Close()
	}
	_, err := io.Copy(w, response.Body)
	return err
}

// StrictServerInterface represents all server handlers.
type StrictServerInterface interface {
	// Returns main page
	// (GET /)
	Index(ctx context.Context, request IndexRequestObject) (IndexResponseObject, error)
}

type StrictHandlerFunc = strictecho.StrictEchoHandlerFunc
type StrictMiddlewareFunc = strictecho.StrictEchoMiddlewareFunc

func NewStrictHandler(ssi StrictServerInterface, middlewares []StrictMiddlewareFunc) ServerInterface {
	return &strictHandler{ssi: ssi, middlewares: middlewares}
}

type strictHandler struct {
	ssi         StrictServerInterface
	middlewares []StrictMiddlewareFunc
}

// Index operation middleware
func (sh *strictHandler) Index(ctx echo.Context) error {
	var request IndexRequestObject

	handler := func(ctx echo.Context, request interface{}) (interface{}, error) {
		return sh.ssi.Index(ctx.Request().Context(), request.(IndexRequestObject))
	}
	for _, middleware := range sh.middlewares {
		handler = middleware(handler, "Index")
	}

	response, err := handler(ctx, request)

	if err != nil {
		return err
	} else if validResponse, ok := response.(IndexResponseObject); ok {
		return validResponse.VisitIndexResponse(ctx.Response())
	} else if response != nil {
		return fmt.Errorf("unexpected response type: %T", response)
	}
	return nil
}
+5 −3
Original line number Diff line number Diff line
@@ -12,15 +12,17 @@ info:
  license:
    name: MIT
    url: https://opensource.org/license/mit
servers:
  - url: https://units.lyda.ie/

# servers:
#  - url: https://units.lyda.ie/

paths:
  /:
    get:
      summary: Returns main page
      description: |
        The base page for the SPA.
      operationId: index
      operationId: GetIndex
      responses:
        '200':
          description: main page
+5 −10
Original line number Diff line number Diff line
@@ -29,7 +29,9 @@ TODO: example 3`,
}

func run(_ *cobra.Command, _ []string) {
	server := &web.Config{}
	fmt.Printf("Server listening on '%s'\n", viper.GetString("listen"))
	server, err := web.New(viper.GetString("listen"))
	cobra.CheckErr(err)

	ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
	defer stop()
@@ -48,16 +50,8 @@ func Execute() {

func init() {
	cobra.OnInitialize(initConfig)

	// Here you will define your flags and configuration settings.
	// Cobra supports persistent flags, which, if defined here,
	// will be global for your application.

	rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.units.yaml)")

	// Cobra also supports local flags, which will only run
	// when this action is called directly.
	rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
	rootCmd.Flags().StringP("listen", "l", "0.0.0.0:8989", "host:port to listen on")
}

// initConfig reads in config file and ENV variables if set.
@@ -77,6 +71,7 @@ func initConfig() {
	}

	viper.AutomaticEnv() // read in environment variables that match
	viper.BindPFlags(rootCmd.Flags())

	// If a config file is found, read it in.
	if err := viper.ReadInConfig(); err == nil {
Loading