chore(tfstated): refactor backend code to a dedicated package
This commit is contained in:
parent
1dbb1b9ee7
commit
36e3d473f2
9 changed files with 60 additions and 36 deletions
|
@ -4,17 +4,14 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.adyxax.org/adyxax/tfstated/pkg/backend"
|
||||
"git.adyxax.org/adyxax/tfstated/pkg/database"
|
||||
"git.adyxax.org/adyxax/tfstated/pkg/logger"
|
||||
)
|
||||
|
||||
func run(
|
||||
|
@ -33,31 +30,7 @@ func run(
|
|||
return err
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
addRoutes(
|
||||
mux,
|
||||
db,
|
||||
)
|
||||
|
||||
host := getenv("TFSTATED_HOST")
|
||||
if host == "" {
|
||||
host = "127.0.0.1"
|
||||
}
|
||||
port := getenv("TFSTATED_PORT")
|
||||
if port == "" {
|
||||
port = "8080"
|
||||
}
|
||||
|
||||
httpServer := &http.Server{
|
||||
Addr: net.JoinHostPort(host, port),
|
||||
Handler: logger.Middleware(mux, false),
|
||||
}
|
||||
go func() {
|
||||
log.Printf("listening on %s\n", httpServer.Addr)
|
||||
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
_, _ = fmt.Fprintf(stderr, "error listening and serving: %+v\n", err)
|
||||
}
|
||||
}()
|
||||
httpServer := backend.Run(ctx, db, getenv, stderr)
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package backend
|
||||
|
||||
import (
|
||||
"fmt"
|
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package backend
|
||||
|
||||
import (
|
||||
"fmt"
|
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package backend
|
||||
|
||||
import "net/http"
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package backend
|
||||
|
||||
import (
|
||||
"fmt"
|
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package backend
|
||||
|
||||
import (
|
||||
"fmt"
|
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package backend
|
||||
|
||||
import (
|
||||
"net/http"
|
51
pkg/backend/run.go
Normal file
51
pkg/backend/run.go
Normal file
|
@ -0,0 +1,51 @@
|
|||
package backend
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
"git.adyxax.org/adyxax/tfstated/pkg/database"
|
||||
"git.adyxax.org/adyxax/tfstated/pkg/logger"
|
||||
)
|
||||
|
||||
func Run(
|
||||
ctx context.Context,
|
||||
db *database.DB,
|
||||
//args []string,
|
||||
getenv func(string) string,
|
||||
//stdin io.Reader,
|
||||
//stdout io.Writer,
|
||||
stderr io.Writer,
|
||||
) *http.Server {
|
||||
mux := http.NewServeMux()
|
||||
addRoutes(
|
||||
mux,
|
||||
db,
|
||||
)
|
||||
|
||||
host := getenv("TFSTATED_HOST")
|
||||
if host == "" {
|
||||
host = "127.0.0.1"
|
||||
}
|
||||
port := getenv("TFSTATED_PORT")
|
||||
if port == "" {
|
||||
port = "8080"
|
||||
}
|
||||
|
||||
httpServer := &http.Server{
|
||||
Addr: net.JoinHostPort(host, port),
|
||||
Handler: logger.Middleware(mux, false),
|
||||
}
|
||||
go func() {
|
||||
log.Printf("listening on %s\n", httpServer.Addr)
|
||||
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
_, _ = fmt.Fprintf(stderr, "error listening and serving: %+v\n", err)
|
||||
}
|
||||
}()
|
||||
|
||||
return httpServer
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package backend
|
||||
|
||||
import (
|
||||
"fmt"
|
Loading…
Add table
Reference in a new issue