Skip to content

Commit

Permalink
refactor(http-server-go): split sys by platform
Browse files Browse the repository at this point in the history
  • Loading branch information
c100k committed Mar 22, 2024
1 parent e424eb2 commit 5b8bdae
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Folders (A => Z)
build
node_modules

# Files (A => Z)
Expand Down
4 changes: 1 addition & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ services:
- ./data:/data
- ./impl/http-server-go:/app
working_dir: "/app"
command: >
bash -c "go fmt
&& go run main.go config.go data.go errors.go handlers.go middlewares.go service.go service_file_json.go service_noop.go service_self.go utils.go"
command: bash -c "go fmt && go build -o /build/http-server-go -v && /build/http-server-go"
swagger-ui:
image: swaggerapi/swagger-ui:v5.12.0
environment:
Expand Down
9 changes: 1 addition & 8 deletions impl/http-server-go/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@ type Config struct {
bind string
pathPrefix string
port int32
provider string
protocol string
runnableFlavor string
runnableFQDN string
runnableGroupIdentifier string
runnableId string
runnableIPv4 string
runnableKind string
runnableNameFallback string
runnableScopesGeoLabel string
runnableScopesGeoValue string
Expand All @@ -30,8 +27,6 @@ type Config struct {
serviceImpl string
serviceFileJsonFilePath *string
sysCmdPkg string
sysCmdReboot string
sysCmdStop string
}

const ENV_VAR_PREFIX = "RBTX_"
Expand Down Expand Up @@ -59,8 +54,6 @@ func getConfig() *Config {
serviceImpl: getEnvOr("SERVICE_IMPL", "noop"),
serviceFileJsonFilePath: getNullableEnv("SERVICE_FILE_JSON_FILE_PATH"),
sysCmdPkg: getEnvOr("SYS_CMD_PKG", "syscall"),
sysCmdReboot: getEnvOr("SYS_CMD_REBOOT", "reboot"),
sysCmdStop: getEnvOr("SYS_CMD_STOP", "shutdown"),
}

assertServiceImpl(config)
Expand All @@ -82,7 +75,7 @@ func assertServiceImplFileJson(config Config) {
}

if config.serviceFileJsonFilePath == nil {
panic(fmt.Sprintf("You must provide a json file path when serviceImpl is 'fileJson'"))
panic("You must provide a json file path when serviceImpl is 'fileJson'")
}

path := *config.serviceFileJsonFilePath
Expand Down
4 changes: 2 additions & 2 deletions impl/http-server-go/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func authMiddleware(config *Config) func(http.Handler) http.Handler {
}
}

func headerMiddleware(config *Config) func(http.Handler) http.Handler {
func headerMiddleware(_ *Config) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", CONTENT_TYPE)
Expand All @@ -38,7 +38,7 @@ func headerMiddleware(config *Config) func(http.Handler) http.Handler {
}
}

func logMiddleware(config *Config, logger *slog.Logger) func(http.Handler) http.Handler {
func logMiddleware(_ *Config, logger *slog.Logger) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logger.Info(fmt.Sprintf("%s %s", r.Method, r.URL))
Expand Down
7 changes: 7 additions & 0 deletions impl/http-server-go/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ package main

import "openapi"

type ServiceOperationType int32

const (
REBOOT ServiceOperationType = 0
STOP ServiceOperationType = 1
)

type Service interface {
list(params *openapi.ListRunnablesQueryParams) (*openapi.ListResRunnable, *ServiceError)
reboot(id string) (*openapi.RunnableOperationRes, *ServiceError)
Expand Down
19 changes: 2 additions & 17 deletions impl/http-server-go/service_self.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package main

import (
"errors"
"log/slog"
"openapi"
"os"
"os/exec"
"syscall"
)

type ServiceSelf struct {
Expand Down Expand Up @@ -72,7 +69,7 @@ func (service ServiceSelf) reboot(id string) (*openapi.RunnableOperationRes, *Se
return nil, err
}

errExec := execOperation(config, config.sysCmdReboot, syscall.LINUX_REBOOT_CMD_RESTART)
errExec := performOpOnSelf(config, REBOOT)
if errExec != nil {
return nil, &ServiceError{HttpStatus: 500, Message: errExec.Error()}
}
Expand All @@ -88,7 +85,7 @@ func (service ServiceSelf) stop(id string) (*openapi.RunnableOperationRes, *Serv
return nil, err
}

errExec := execOperation(config, config.sysCmdStop, syscall.LINUX_REBOOT_CMD_POWER_OFF)
errExec := performOpOnSelf(config, STOP)
if errExec != nil {
return nil, &ServiceError{HttpStatus: 500, Message: errExec.Error()}
}
Expand All @@ -103,18 +100,6 @@ func checkThatRunnableExists(config *Config, id string) *ServiceError {
return nil
}

func execOperation(config *Config, forExec string, forSyscall int) error {
switch config.sysCmdPkg {
case "exec":
cmd := exec.Command(forExec)
return cmd.Run()
case "syscall":
return syscall.Reboot(forSyscall)
default:
return errors.New("Invalid sysCmdPkg")
}
}

func nameFromHostname(config *Config) string {
name := config.runnableNameFallback
hostname, err := os.Hostname()
Expand Down
24 changes: 24 additions & 0 deletions impl/http-server-go/service_self_sys_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"fmt"
"os/exec"
)

func performOpOnSelf(config *Config, op ServiceOperationType) error {
switch config.sysCmdPkg {
case "exec":
var cmd *exec.Cmd
switch op {
case REBOOT:
cmd = exec.Command("reboot")
case STOP:
cmd = exec.Command("shutdown")
}
return cmd.Run()
case "syscall":
return fmt.Errorf("sysCmdPkg not supported on this OS : %s", config.sysCmdPkg)
default:
return fmt.Errorf("invalid sysCmdPkg : %s", config.sysCmdPkg)
}
}
33 changes: 33 additions & 0 deletions impl/http-server-go/service_self_sys_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"errors"
"fmt"
"os/exec"
"syscall"
)

func performOpOnSelf(config *Config, op ServiceOperationType) error {
switch config.sysCmdPkg {
case "exec":
var cmd *exec.Cmd
switch op {
case REBOOT:
cmd = exec.Command("reboot")
case STOP:
cmd = exec.Command("shutdown")
}
return cmd.Run()
case "syscall":
var cmd int
switch op {
case REBOOT:
cmd = syscall.LINUX_REBOOT_CMD_RESTART
case STOP:
cmd = syscall.LINUX_REBOOT_CMD_POWER_OFF
}
return syscall.Reboot(cmd)
default:
return errors.New(fmt.Sprintf("Invalid sysCmdPkg : %s", config.sysCmdPkg))
}
}
24 changes: 24 additions & 0 deletions impl/http-server-go/service_self_sys_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"fmt"
"os/exec"
)

func performOpOnSelf(config *Config, op ServiceOperationType) error {
switch config.sysCmdPkg {
case "exec":
var cmd *exec.Cmd
switch op {
case REBOOT:
cmd = exec.Command("shutdown", "/s")
case STOP:
cmd = exec.Command("shutdown", "/r")
}
return cmd.Run()
case "syscall":
return fmt.Errorf("sysCmdPkg not supported on this OS : %s", config.sysCmdPkg)
default:
return fmt.Errorf("Invalid sysCmdPkg : %s", config.sysCmdPkg)
}
}

0 comments on commit 5b8bdae

Please sign in to comment.