Merged in feature/shorttestsanddirtidy (pull request #26)
Add short tests and Tidy internal directories * complete the tasks
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"queryorchestration/internal/server"
|
||||
"strconv"
|
||||
|
||||
"github.com/getkin/kin-openapi/openapi3"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
_ "github.com/lib/pq"
|
||||
echoSwagger "github.com/swaggo/echo-swagger"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
RegisterHandlers func(*server.Config, *echo.Echo) *APIConfig
|
||||
BasePath string
|
||||
}
|
||||
|
||||
type APIConfig struct {
|
||||
Swagger *openapi3.T
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
port int
|
||||
host string
|
||||
address string
|
||||
server *echo.Echo
|
||||
}
|
||||
|
||||
func New(ctx context.Context, cfg *Config) *Server {
|
||||
serverCfg := server.New(ctx, &server.NewConfig{
|
||||
BasePath: cfg.BasePath,
|
||||
})
|
||||
|
||||
e := echo.New()
|
||||
|
||||
e.Use(middleware.Logger())
|
||||
e.Use(middleware.Recover())
|
||||
e.Use(middleware.CORS())
|
||||
|
||||
apicfg := cfg.RegisterHandlers(serverCfg, e)
|
||||
|
||||
e.GET("/swagger/doc.json", func(c echo.Context) error {
|
||||
spec, err := apicfg.Swagger.MarshalJSON()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.Blob(http.StatusOK, "application/json", spec)
|
||||
})
|
||||
e.GET("/swagger/doc.yaml", func(c echo.Context) error {
|
||||
spec, err := apicfg.Swagger.MarshalYAML()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
yaml, err := yaml.Marshal(spec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Blob(http.StatusOK, "application/yaml", yaml)
|
||||
})
|
||||
|
||||
e.GET("/swagger/*", echoSwagger.EchoWrapHandler(
|
||||
echoSwagger.DocExpansion("full"),
|
||||
echoSwagger.DeepLinking(true),
|
||||
echoSwagger.DomID("swagger-ui"),
|
||||
echoSwagger.PersistAuthorization(true),
|
||||
))
|
||||
|
||||
host := "0.0.0.0"
|
||||
port := 8080
|
||||
address := net.JoinHostPort(host, strconv.Itoa(port))
|
||||
|
||||
return &Server{
|
||||
port: port,
|
||||
host: host,
|
||||
address: address,
|
||||
server: e,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) Listen() {
|
||||
log.Printf("Listening on port %d", s.port)
|
||||
|
||||
err := s.server.Start(s.address)
|
||||
if err != nil {
|
||||
s.server.Logger.Panic(err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path"
|
||||
"queryorchestration/internal/database"
|
||||
"queryorchestration/internal/server"
|
||||
"queryorchestration/internal/test"
|
||||
"testing"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("Skipping long test in short mode")
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
_, cleanup := test.CreateDB(t, ctx, &test.CreateDatabaseConfig{
|
||||
Migrations: &database.MigrationConfig{
|
||||
BasePath: path.Join(os.Getenv("PWD"), "../.."),
|
||||
},
|
||||
})
|
||||
defer cleanup()
|
||||
|
||||
registerHandlers := func(c *server.Config, e *echo.Echo) *APIConfig {
|
||||
return nil
|
||||
}
|
||||
cfg := &Config{
|
||||
RegisterHandlers: registerHandlers,
|
||||
BasePath: path.Join(os.Getenv("PWD"), "../.."),
|
||||
}
|
||||
|
||||
server := New(ctx, cfg)
|
||||
assert.Equal(t, 8080, server.port)
|
||||
assert.Equal(t, "0.0.0.0", server.host)
|
||||
assert.Equal(t, "0.0.0.0:8080", server.address)
|
||||
}
|
||||
|
||||
func TestListen(t *testing.T) {
|
||||
server := Server{}
|
||||
|
||||
assert.Panics(t, func() { server.Listen() })
|
||||
}
|
||||
Reference in New Issue
Block a user