Skip to content

Commit

Permalink
refactor(http-server-go): move errors as last return
Browse files Browse the repository at this point in the history
Let's follow the common pattern used as a convention by the industry
  • Loading branch information
c100k committed Mar 21, 2024
1 parent 5c3ebf6 commit 7910942
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 45 deletions.
6 changes: 3 additions & 3 deletions impl/http-server-go/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func getRunnablesHandler(service Service) func(w http.ResponseWriter, r *http.Re
params.SetOffset(*offset)
}

err, res := service.list(params)
res, err := service.list(params)
if err != nil {
w.WriteHeader(err.HttpStatus)
encoder.Encode(openapi.NewErrorRes(err.Error()))
Expand All @@ -45,7 +45,7 @@ func postRunnableRebootHandler(service Service) func(w http.ResponseWriter, r *h
vars := mux.Vars(r)
id := vars["id"]

err, res := service.reboot(id)
res, err := service.reboot(id)
if err != nil {
w.WriteHeader(err.HttpStatus)
encoder.Encode(openapi.NewErrorRes(err.Error()))
Expand All @@ -64,7 +64,7 @@ func postRunnableStopHandler(service Service) func(w http.ResponseWriter, r *htt
vars := mux.Vars(r)
id := vars["id"]

err, res := service.stop(id)
res, err := service.stop(id)
if err != nil {
w.WriteHeader(err.HttpStatus)
encoder.Encode(openapi.NewErrorRes(err.Error()))
Expand Down
6 changes: 3 additions & 3 deletions impl/http-server-go/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import "openapi"

type Service interface {
list(params *openapi.ListRunnablesQueryParams) (*ServiceError, *openapi.ListResRunnable)
reboot(id string) (*ServiceError, *openapi.RunnableOperationRes)
stop(id string) (*ServiceError, *openapi.RunnableOperationRes)
list(params *openapi.ListRunnablesQueryParams) (*openapi.ListResRunnable, *ServiceError)
reboot(id string) (*openapi.RunnableOperationRes, *ServiceError)
stop(id string) (*openapi.RunnableOperationRes, *ServiceError)
}
44 changes: 22 additions & 22 deletions impl/http-server-go/service_file_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,85 +14,85 @@ type ServiceFileJson struct {
logger *slog.Logger
}

func (service ServiceFileJson) list(params *openapi.ListRunnablesQueryParams) (*ServiceError, *openapi.ListResRunnable) {
func (service ServiceFileJson) list(params *openapi.ListRunnablesQueryParams) (*openapi.ListResRunnable, *ServiceError) {
config := service.config

err, items := findItems(config)
items, err := findItems(config)
if err != nil {
return err, nil
return nil, err
}

total := int32(len(items))

res := openapi.NewListResRunnable(items, total)

return nil, res
return res, nil
}

func (service ServiceFileJson) reboot(id string) (*ServiceError, *openapi.RunnableOperationRes) {
func (service ServiceFileJson) reboot(id string) (*openapi.RunnableOperationRes, *ServiceError) {
config := service.config
logger := service.logger

err, item := findItem(config, id)
item, err := findItem(config, id)
if err != nil {
return err, nil
return nil, err
}

// In a real world, we would probably SSH into the instance to perform the command
logger.Info("Faking reboot", "id", item.Id, "name", item.Name)

return nil, openapi.NewRunnableOperationRes(*openapi.NewNullableString(nil))
return openapi.NewRunnableOperationRes(*openapi.NewNullableString(nil)), nil
}

func (service ServiceFileJson) stop(id string) (*ServiceError, *openapi.RunnableOperationRes) {
func (service ServiceFileJson) stop(id string) (*openapi.RunnableOperationRes, *ServiceError) {
config := service.config
logger := service.logger

err, item := findItem(config, id)
item, err := findItem(config, id)
if err != nil {
return err, nil
return nil, err
}

// In a real world, we would probably SSH into the instance to perform the command
logger.Info("Faking stop", "id", item.Id, "name", item.Name)

return nil, openapi.NewRunnableOperationRes(*openapi.NewNullableString(nil))
return openapi.NewRunnableOperationRes(*openapi.NewNullableString(nil)), nil
}

func findItems(config *Config) (*ServiceError, []openapi.Runnable) {
func findItems(config *Config) ([]openapi.Runnable, *ServiceError) {
file, err := os.Open(*config.serviceFileJsonFilePath)
if err != nil {
return &ServiceError{HttpStatus: 500, Message: err.Error()}, nil
return nil, &ServiceError{HttpStatus: 500, Message: err.Error()}
}

defer file.Close()

content, err := io.ReadAll(file)
if err != nil {
return &ServiceError{HttpStatus: 500, Message: err.Error()}, nil
return nil, &ServiceError{HttpStatus: 500, Message: err.Error()}
}

var items []openapi.Runnable
json.Unmarshal(content, &items)
if items == nil {
return &ServiceError{HttpStatus: 500, Message: "Fix your JSON file to respect the schema"}, nil
return nil, &ServiceError{HttpStatus: 500, Message: "Fix your JSON file to respect the schema"}
}

return nil, items
return items, nil
}

func findItem(config *Config, id string) (*ServiceError, *openapi.Runnable) {
err, items := findItems(config)
func findItem(config *Config, id string) (*openapi.Runnable, *ServiceError) {
items, err := findItems(config)
if err != nil {
return err, nil
return nil, err
}

// If this happens to be called lots of times and we know the file is not changed after starting the server,
// this can be optimized by creating a Map to search faster
idx := slices.IndexFunc(items, func(i openapi.Runnable) bool { return i.Id == id })
if idx == -1 {
return &ServiceError{HttpStatus: 404, Message: Err404Runnable}, nil
return nil, &ServiceError{HttpStatus: 404, Message: Err404Runnable}
}

return nil, &items[idx]
return &items[idx], nil
}
12 changes: 6 additions & 6 deletions impl/http-server-go/service_noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@ type ServiceNoop struct {
logger *slog.Logger
}

func (service ServiceNoop) list(params *openapi.ListRunnablesQueryParams) (*ServiceError, *openapi.ListResRunnable) {
func (service ServiceNoop) list(params *openapi.ListRunnablesQueryParams) (*openapi.ListResRunnable, *ServiceError) {
service.logger.Warn("Noop")

items := []openapi.Runnable{}
total := int32(len(items))

res := openapi.NewListResRunnable(items, total)

return nil, res
return res, nil
}

func (service ServiceNoop) reboot(id string) (*ServiceError, *openapi.RunnableOperationRes) {
func (service ServiceNoop) reboot(id string) (*openapi.RunnableOperationRes, *ServiceError) {
service.logger.Warn("Noop")

return nil, openapi.NewRunnableOperationRes(*openapi.NewNullableString(nil))
return openapi.NewRunnableOperationRes(*openapi.NewNullableString(nil)), nil
}

func (service ServiceNoop) stop(id string) (*ServiceError, *openapi.RunnableOperationRes) {
func (service ServiceNoop) stop(id string) (*openapi.RunnableOperationRes, *ServiceError) {
service.logger.Warn("Noop")

return nil, openapi.NewRunnableOperationRes(*openapi.NewNullableString(nil))
return openapi.NewRunnableOperationRes(*openapi.NewNullableString(nil)), nil
}
22 changes: 11 additions & 11 deletions impl/http-server-go/service_self.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ type ServiceSelf struct {
logger *slog.Logger
}

func (service ServiceSelf) list(params *openapi.ListRunnablesQueryParams) (*ServiceError, *openapi.ListResRunnable) {
func (service ServiceSelf) list(params *openapi.ListRunnablesQueryParams) (*openapi.ListResRunnable, *ServiceError) {
config := service.config

q := params.Q
if len(*q) > 0 {
err := checkThatRunnableExists(config, *q)
if err != nil {
return nil, openapi.NewListResRunnable([]openapi.Runnable{}, 0)
return openapi.NewListResRunnable([]openapi.Runnable{}, 0), nil
}
}

Expand Down Expand Up @@ -61,39 +61,39 @@ func (service ServiceSelf) list(params *openapi.ListRunnablesQueryParams) (*Serv

res := openapi.NewListResRunnable(items, total)

return nil, res
return res, nil
}

func (service ServiceSelf) reboot(id string) (*ServiceError, *openapi.RunnableOperationRes) {
func (service ServiceSelf) reboot(id string) (*openapi.RunnableOperationRes, *ServiceError) {
config := service.config

err := checkThatRunnableExists(config, id)
if err != nil {
return err, nil
return nil, err
}

errExec := execOperation(config, config.sysCmdReboot, syscall.LINUX_REBOOT_CMD_RESTART)
if errExec != nil {
return &ServiceError{HttpStatus: 500, Message: errExec.Error()}, nil
return nil, &ServiceError{HttpStatus: 500, Message: errExec.Error()}
}

return nil, openapi.NewRunnableOperationRes(*openapi.NewNullableString(nil))
return openapi.NewRunnableOperationRes(*openapi.NewNullableString(nil)), nil
}

func (service ServiceSelf) stop(id string) (*ServiceError, *openapi.RunnableOperationRes) {
func (service ServiceSelf) stop(id string) (*openapi.RunnableOperationRes, *ServiceError) {
config := service.config

err := checkThatRunnableExists(config, id)
if err != nil {
return err, nil
return nil, err
}

errExec := execOperation(config, config.sysCmdStop, syscall.LINUX_REBOOT_CMD_POWER_OFF)
if errExec != nil {
return &ServiceError{HttpStatus: 500, Message: errExec.Error()}, nil
return nil, &ServiceError{HttpStatus: 500, Message: errExec.Error()}
}

return nil, openapi.NewRunnableOperationRes(*openapi.NewNullableString(nil))
return openapi.NewRunnableOperationRes(*openapi.NewNullableString(nil)), nil
}

func checkThatRunnableExists(config *Config, id string) *ServiceError {
Expand Down

0 comments on commit 7910942

Please sign in to comment.