store: move creation to main.go

Let the weldr API take the store as an argument, rather than create it
itself. This will allow us to share the store with the jobqueue API in
follow-up patches.

Signed-off-by: Tom Gundersen <teg@jklm.no>
This commit is contained in:
Tom Gundersen 2019-10-05 17:18:08 +02:00 committed by Lars Karlitski
parent 4fcb5d66fa
commit 89fd2e6037
3 changed files with 23 additions and 20 deletions

View file

@ -7,9 +7,11 @@ import (
"os"
"path/filepath"
"osbuild-composer/internal/blueprint"
"osbuild-composer/internal/job"
"osbuild-composer/internal/jobqueue"
"osbuild-composer/internal/rpmmd"
"osbuild-composer/internal/store"
"osbuild-composer/internal/weldr"
"github.com/coreos/go-systemd/activation"
@ -63,8 +65,21 @@ func main() {
stateChannel := make(chan []byte, 10)
jobChannel := make(chan job.Job, 200)
jobUpdateChannel := make(chan job.Status, 200)
store := store.New(state, stateChannel, jobChannel, jobUpdateChannel)
// sample blueprint on first run
if state == nil {
store.PushBlueprint(blueprint.Blueprint{
Name: "example",
Description: "An Example",
Version: "1",
Packages: []blueprint.Package{{"httpd", "2.*"}},
Modules: []blueprint.Package{},
})
}
jobAPI := jobqueue.New(logger, jobChannel, jobUpdateChannel)
weldrAPI := weldr.New(repo, packages, logger, state, stateChannel, jobChannel, jobUpdateChannel)
weldrAPI := weldr.New(repo, packages, logger, store)
go func() {
for {
err := writeFileAtomically(StateFile, <-stateChannel, 0755)

View file

@ -14,7 +14,6 @@ import (
"github.com/julienschmidt/httprouter"
"osbuild-composer/internal/blueprint"
"osbuild-composer/internal/job"
"osbuild-composer/internal/rpmmd"
"osbuild-composer/internal/store"
)
@ -29,27 +28,16 @@ type API struct {
router *httprouter.Router
}
func New(repo rpmmd.RepoConfig, packages rpmmd.PackageList, logger *log.Logger, initialState []byte, stateChannel chan<- []byte, jobs chan<- job.Job, jobStatus <-chan job.Status) *API {
func New(repo rpmmd.RepoConfig, packages rpmmd.PackageList, logger *log.Logger, store *store.Store) *API {
// This needs to be shared with the worker API so that they can communicate with each other
// builds := make(chan queue.Build, 200)
api := &API{
store: store.New(initialState, stateChannel, jobs, jobStatus),
store: store,
repo: repo,
packages: packages,
logger: logger,
}
// sample blueprint on first run
if initialState == nil {
api.store.PushBlueprint(blueprint.Blueprint{
Name: "example",
Description: "An Example",
Version: "1",
Packages: []blueprint.Package{{"httpd", "2.*"}},
Modules: []blueprint.Package{},
})
}
api.router = httprouter.New()
api.router.RedirectTrailingSlash = false
api.router.RedirectFixedPath = false

View file

@ -15,6 +15,7 @@ import (
"osbuild-composer/internal/job"
"osbuild-composer/internal/rpmmd"
"osbuild-composer/internal/store"
"osbuild-composer/internal/weldr"
)
@ -147,20 +148,19 @@ func TestBasic(t *testing.T) {
{"/api/v0/modules/info", http.StatusNotFound, ``},
{"/api/v0/modules/info/", http.StatusNotFound, ``},
{"/api/v0/blueprints/list", http.StatusOK, `{"total":1,"offset":0,"limit":1,"blueprints":["example"]}`},
{"/api/v0/blueprints/list", http.StatusOK, `{"total":0,"offset":0,"limit":0,"blueprints":[]}`},
{"/api/v0/blueprints/info/", http.StatusNotFound, ``},
{"/api/v0/blueprints/info/foo", http.StatusNotFound, `{"status":false}`},
{"/api/v0/blueprints/info/example", http.StatusOK, `*`},
}
for _, c := range cases {
api := weldr.New(repo, packages, nil, nil, nil, nil, nil)
api := weldr.New(repo, packages, nil, store.New(nil, nil, nil, nil))
testRoute(t, api, "GET", c.Path, ``, c.ExpectedStatus, c.ExpectedJSON)
}
}
func TestBlueprints(t *testing.T) {
api := weldr.New(repo, packages, nil, nil, nil, nil, nil)
api := weldr.New(repo, packages, nil, store.New(nil, nil, nil, nil))
testRoute(t, api, "POST", "/api/v0/blueprints/new",
`{"name":"test","description":"Test","packages":[{"name":"httpd","version":"2.4.*"}],"version":"0.0.0"}`,
@ -184,7 +184,7 @@ func TestBlueprints(t *testing.T) {
func TestCompose(t *testing.T) {
jobChannel := make(chan job.Job, 200)
api := weldr.New(repo, packages, nil, nil, nil, jobChannel, nil)
api := weldr.New(repo, packages, nil, store.New(nil, nil, jobChannel, nil))
testRoute(t, api, "POST", "/api/v0/blueprints/new",
`{"name":"test","description":"Test","packages":[{"name":"httpd","version":"2.4.*"}],"version":"0.0.0"}`,