Skip to content

Commit

Permalink
refactor config
Browse files Browse the repository at this point in the history
  • Loading branch information
anthdm committed Jan 11, 2024
1 parent ce40d62 commit 782dfca
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 52 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ PROTO_PATH := "../../go/pkg/mod/github.com/anthdm/[email protected]

build:
@go build -o bin/api cmd/api/main.go
@go build -o bin/wasmserver cmd/wasmserver/main.go
@go build -o bin/ingress cmd/ingress/main.go
@go build -o bin/raptor cmd/cli/main.go
@go build -o bin/runtime cmd/runtime/main.go

wasmserver: build
@./bin/wasmserver
ingress: build
@./bin/ingress

runtime: build
@./bin/runtime
Expand Down
6 changes: 3 additions & 3 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func main() {
}

server := api.NewServer(store, store, modCache)
fmt.Printf("api server running\t%s\n", config.GetApiUrl())
log.Fatal(server.Listen(config.Get().APIServerAddr))
fmt.Printf("api server running\t%s\n", config.ApiUrl())
log.Fatal(server.Listen(config.Get().HTTPAPIAddr))
}

func seedEndpoint(store storage.Store, cache storage.ModCacher) {
Expand Down Expand Up @@ -81,7 +81,7 @@ func seedEndpoint(store storage.Store, cache storage.ModCacher) {
if err != nil {
log.Fatal(err)
}
fmt.Printf("endpoint seeded: %s/live/%s\n", config.GetWasmUrl(), endpoint.ID)
fmt.Printf("endpoint seeded: %s/live/%s\n", config.IngressUrl(), endpoint.ID)
}

func compile(ctx context.Context, cache wazero.CompilationCache, blob []byte) {
Expand Down
4 changes: 2 additions & 2 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func main() {
printUsage()
}

c := client.New(client.NewConfig().WithURL(config.GetApiUrl()))
c := client.New(client.NewConfig().WithURL(config.ApiUrl()))
command := command{
client: c,
}
Expand Down Expand Up @@ -175,7 +175,7 @@ func (c command) handleDeploy(args []string) {
}
fmt.Println(string(b))
fmt.Println()
fmt.Printf("deploy preview: %s/preview/%s\n", config.GetWasmUrl(), deploy.ID)
fmt.Printf("deploy preview: %s/preview/%s\n", config.IngressUrl(), deploy.ID)
}

func (c command) handleServeEndpoint(args []string) {
Expand Down
26 changes: 17 additions & 9 deletions cmd/wasmserver/main.go → cmd/ingress/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@ import (
)

func main() {
var configFile string
flagSet := flag.NewFlagSet("raptor", flag.ExitOnError)
var (
configFile string
address string
id string
region string
)

flagSet := flag.NewFlagSet("ingress", flag.ExitOnError)
flagSet.StringVar(&configFile, "config", "config.toml", "")
flagSet.StringVar(&address, "cluster-addr", "127.0.0.1:8132", "")
flagSet.StringVar(&id, "id", "ingress", "")
flagSet.StringVar(&region, "region", "default", "")
flagSet.Parse(os.Args[1:])

err := config.Parse(configFile)
if err != nil {
if err := config.Parse(configFile); err != nil {
log.Fatal(err)
}

Expand All @@ -44,9 +52,9 @@ func main() {
)

clusterConfig := cluster.NewConfig().
WithListenAddr(config.Get().Cluster.Address).
WithRegion(config.Get().Cluster.Region).
WithID(config.Get().Cluster.ID)
WithListenAddr(address).
WithRegion(region).
WithID(id)
c, err := cluster.New(clusterConfig)
if err != nil {
log.Fatal(err)
Expand All @@ -58,13 +66,13 @@ func main() {
c.Start()

server := actrs.NewWasmServer(
config.Get().WASMServerAddr,
config.Get().HTTPIngressAddr,
c,
store,
metricStore,
modCache)
c.Engine().Spawn(server, actrs.KindWasmServer)
fmt.Printf("wasm server running\t%s\n", config.Get().WASMServerAddr)
fmt.Printf("ingress server running\t%s\n", config.Get().HTTPIngressAddr)

sigch := make(chan os.Signal, 1)
signal.Notify(sigch, syscall.SIGINT, syscall.SIGTERM)
Expand Down
30 changes: 23 additions & 7 deletions cmd/runtime/main.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
package main

import (
"flag"
"log"
"os"
"os/signal"
"syscall"

"github.com/anthdm/hollywood/actor"
"github.com/anthdm/hollywood/cluster"
"github.com/anthdm/raptor/internal/actrs"
"github.com/anthdm/raptor/internal/config"
"github.com/anthdm/raptor/internal/storage"
)

func main() {
if err := config.Parse("config.toml"); err != nil {
var (
configFile string
address string
id string
region string
)

flagSet := flag.NewFlagSet("runtime", flag.ExitOnError)
flagSet.StringVar(&configFile, "config", "config.toml", "")
flagSet.StringVar(&address, "cluster-addr", "127.0.0.1:8134", "")
flagSet.StringVar(&id, "id", "runtime", "")
flagSet.StringVar(&region, "region", "default", "")
flagSet.Parse(os.Args[1:])

if err := config.Parse(configFile); err != nil {
log.Fatal(err)
}

var (
user = config.Get().Storage.User
pw = config.Get().Storage.Password
Expand All @@ -33,17 +50,16 @@ func main() {
// metricStore = store
)
clusterConfig := cluster.NewConfig().
WithListenAddr("127.0.0.1:6000").
WithRegion("eu-west").
WithID("member_2")
WithListenAddr(address).
WithRegion(region).
WithID(id)
c, err := cluster.New(clusterConfig)
if err != nil {
log.Fatal(err)
}
c.RegisterKind(actrs.KindRuntime, actrs.NewRuntime(store, modCache), &cluster.KindConfig{})
// c.Engine().Spawn(actrs.NewMetric, actrs.KindMetric, actor.WithID("1"))
// c.Engine().Spawn(actrs.NewRuntimeManager(c), actrs.KindRuntimeManager, actor.WithID("1"))
// c.Engine().Spawn(actrs.NewRuntimeLog, actrs.KindRuntimeLog, actor.WithID("1"))
c.Engine().Spawn(actrs.NewMetric, actrs.KindMetric, actor.WithID("1"))
c.Engine().Spawn(actrs.NewRuntimeLog, actrs.KindRuntimeLog, actor.WithID("1"))
c.Start()

sigch := make(chan os.Signal, 1)
Expand Down
1 change: 1 addition & 0 deletions cmd/runtime/runtime.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Runtime
2 changes: 1 addition & 1 deletion internal/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (s *Server) handlePublish(w http.ResponseWriter, r *http.Request) error {

resp := PublishResponse{
DeploymentID: deploy.ID,
URL: fmt.Sprintf("%s/live/%s", config.GetWasmUrl(), endpoint.ID),
URL: fmt.Sprintf("%s/live/%s", config.IngressUrl(), endpoint.ID),
}
return writeJSON(w, http.StatusOK, resp)
}
Expand Down
41 changes: 14 additions & 27 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,12 @@ import (
)

const defaultConfig = `
wasmServerAddr = "127.0.0.1:5000"
apiServerAddr = "127.0.0.1:3000"
storageDriver = "sqlite"
apiToken = "foobarbaz"
httpIngressAddr = "127.0.0.1:5000"
httpAPIAddr = "127.0.0.1:3000"
storageDriver = "postgres"
apiToken = ""
authorization = false
[cluster]
address = "127.0.0.1:6666"
id = "wasm_member_1"
region = "eu-west"
[storage]
user = "postgres"
password = "postgres"
Expand All @@ -41,21 +36,13 @@ type Storage struct {
SSLMode string
}

type Cluster struct {
Address string
ID string
Region string
}

type Config struct {
APIServerAddr string
WASMServerAddr string
StorageDriver string
APIToken string
Authorization bool

Storage Storage
Cluster Cluster
HTTPAPIAddr string
HTTPIngressAddr string
StorageDriver string
APIToken string
Authorization bool
Storage Storage
}

func Parse(path string) error {
Expand Down Expand Up @@ -93,10 +80,10 @@ func makeURL(address string) string {
return "http://" + net.JoinHostPort(host, port)
}

func GetWasmUrl() string {
return makeURL(config.WASMServerAddr)
func IngressUrl() string {
return makeURL(config.HTTPIngressAddr)
}

func GetApiUrl() string {
return makeURL(config.APIServerAddr)
func ApiUrl() string {
return makeURL(config.HTTPAPIAddr)
}

0 comments on commit 782dfca

Please sign in to comment.