Dockerize configapi

pull/89/head
Aloïs Micard 4 years ago
parent 345f9c2669
commit 901653dc02
No known key found for this signature in database
GPG Key ID: 1A0EB82F071F5EFE

@ -0,0 +1,24 @@
# build image
FROM golang:1.15.0-alpine as builder
RUN apk update && apk upgrade && \
apk add --no-cache bash git openssh
WORKDIR /app
# Copy and download dependencies to cache them and faster build time
COPY go.mod go.sum ./
RUN go mod download
COPY . .
# Test then build app
RUN go build -v github.com/creekorful/trandoshan/cmd/tdsh-configapi
# runtime image
FROM alpine:latest
COPY --from=builder /app/tdsh-configapi /app/
WORKDIR /app/
ENTRYPOINT ["./tdsh-configapi"]

@ -0,0 +1,13 @@
package main
import (
"github.com/creekorful/trandoshan/internal/configapi"
"os"
)
func main() {
app := configapi.GetApp()
if err := app.Run(os.Args); err != nil {
os.Exit(1)
}
}

@ -41,14 +41,7 @@ services:
--hub-uri amqp://guest:guest@rabbitmq:5672
--api-uri http://api:8080
--api-token eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InNjaGVkdWxlciIsInJpZ2h0cyI6eyJHRVQiOlsiL3YxL3Jlc291cmNlcyJdfX0.dBR6KLQp2h2srY-By3zikEznhQplLCtDrvOkcXP6USY
--forbidden-extensions png
--forbidden-extensions gif
--forbidden-extensions jpg
--forbidden-extensions jpeg
--forbidden-extensions bmp
--forbidden-extensions css
--forbidden-extensions js
--forbidden-hostnames facebookcorewwwi.onion
--config-api-uri http://configapi:8080
restart: always
depends_on:
- rabbitmq
@ -84,9 +77,21 @@ services:
--signing-key K==M5RsU_DQa4_XSbkX?L27s^xWmde25
restart: always
depends_on:
- rabbitmq
- elasticsearch
ports:
- 15005:8080
configapi:
image: creekorful/tdsh-configapi:latest
command: >
--log-level debug
--hub-uri amqp://guest:guest@rabbitmq:5672
restart: always
depends_on:
- rabbitmq
ports:
- 15006:8080
volumes:
esdata:

@ -2,23 +2,71 @@ package configapi
import (
"github.com/creekorful/trandoshan/internal/configapi/api"
"github.com/creekorful/trandoshan/internal/configapi/service"
"github.com/creekorful/trandoshan/internal/event"
"github.com/creekorful/trandoshan/internal/logging"
"github.com/creekorful/trandoshan/internal/util"
"github.com/gorilla/mux"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
"io/ioutil"
"log"
"net/http"
)
type State struct {
// GetApp return the config api app
func GetApp() *cli.App {
return &cli.App{
Name: "tdsh-configapi",
Version: "0.7.0",
Usage: "Trandoshan ConfigAPI component",
Flags: []cli.Flag{
logging.GetLogFlag(),
util.GetHubURI(),
},
Action: execute,
}
}
func execute(ctx *cli.Context) error {
logging.ConfigureLogger(ctx)
log.Info().
Str("ver", ctx.App.Version).
Str("hub-uri", ctx.String("hub-uri")).
Msg("Starting tdsh-configapi")
// Create publisher
pub, err := event.NewPublisher(ctx.String("hub-uri"))
if err != nil {
return err
}
// Create the ConfigAPI service
s, err := service.NewService(nil, pub)
state := state{
api: s,
}
r := mux.NewRouter()
r.HandleFunc("/config/{key}", state.getConfiguration).Methods(http.MethodGet)
r.HandleFunc("/config/{key}", state.setConfiguration).Methods(http.MethodPut)
http.Handle("/", r)
return nil
}
type state struct {
api api.ConfigAPI
}
func (s *State) getConfiguration(w http.ResponseWriter, r *http.Request) {
func (state *state) getConfiguration(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
key := vars["key"]
b, err := s.api.Get(key)
b, err := state.api.Get(key)
if err != nil {
log.Printf("error while retrieving configuration: %s", err)
log.Err(err).Msg("error while retrieving configuration")
w.WriteHeader(http.StatusInternalServerError)
return
}
@ -27,19 +75,19 @@ func (s *State) getConfiguration(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write(b)
}
func (s *State) setConfiguration(w http.ResponseWriter, r *http.Request) {
func (state *state) setConfiguration(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
key := vars["key"]
b, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("error while reading body: %s", err)
log.Err(err).Msg("error while reading body")
w.WriteHeader(http.StatusUnprocessableEntity)
return
}
if err := s.api.Set(key, b); err != nil {
log.Printf("error while setting configuration: %s", err)
if err := state.api.Set(key, b); err != nil {
log.Err(err).Msg("error while setting configuration")
w.WriteHeader(http.StatusInternalServerError)
return
}

@ -23,7 +23,7 @@ func TestGetConfiguration(t *testing.T) {
rec := httptest.NewRecorder()
s := State{api: apiMock}
s := state{api: apiMock}
s.getConfiguration(rec, req)
if rec.Code != http.StatusOK {
@ -55,7 +55,7 @@ func TestSetConfiguration(t *testing.T) {
rec := httptest.NewRecorder()
s := State{api: apiMock}
s := state{api: apiMock}
s.setConfiguration(rec, req)
if rec.Code != http.StatusOK {

@ -2,23 +2,35 @@ package service
import (
"fmt"
"github.com/creekorful/trandoshan/internal/configapi/api"
"github.com/creekorful/trandoshan/internal/configapi/database"
"github.com/creekorful/trandoshan/internal/event"
"github.com/rs/zerolog/log"
)
// Service is the functionality layer provided by the ConfigAPI
type Service struct {
type service struct {
db database.Database
pub event.Publisher
}
// NewService create a new service exposing the ConfigAPI features
func NewService(db database.Database, pub event.Publisher) (api.ConfigAPI, error) {
return &service{
db: db,
pub: pub,
}, nil
}
// Get config using his key
func (s *Service) Get(key string) ([]byte, error) {
func (s *service) Get(key string) ([]byte, error) {
log.Debug().Str("key", key).Msg("Getting key")
return s.db.Get(key)
}
// Set config value
func (s *Service) Set(key string, value []byte) error {
func (s *service) Set(key string, value []byte) error {
log.Debug().Str("key", key).Bytes("value", value).Msg("Setting key")
if err := s.db.Set(key, value); err != nil {
return err
}

@ -14,7 +14,7 @@ func TestService_Get(t *testing.T) {
dbMock := database_mock.NewMockDatabase(mockCtrl)
dbMock.EXPECT().Get("test").Return([]byte("hello"), nil)
s := Service{
s := service{
db: dbMock,
}
@ -37,7 +37,7 @@ func TestService_Set(t *testing.T) {
dbMock.EXPECT().Set("test-key", []byte("hello")).Return(nil)
pubMock.EXPECT().PublishJSON("config.test-key", []byte("hello")).Return(nil)
s := Service{
s := service{
db: dbMock,
pub: pubMock,
}

Loading…
Cancel
Save