Skip to content

Commit

Permalink
Merge pull request #187 from safing/maintain/small-fixes
Browse files Browse the repository at this point in the history
Small fixes and improvements
  • Loading branch information
dhaavi authored Sep 22, 2022
2 parents ff88d9e + 9e8d1fd commit 85a84c1
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 23 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ jobs:

- uses: actions/setup-go@v3
with:
go-version: '^1.18'
go-version: '^1.19'

- name: Run golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.45.1
version: v1.49.0
only-new-issues: true
args: -c ./.golangci.yml
args: -c ./.golangci.yml --timeout 15m

- name: Get dependencies
run: go mod download
Expand All @@ -45,7 +45,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: '^1.18'
go-version: '^1.19'

- name: Get dependencies
run: go mod download
Expand Down
6 changes: 5 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ linters:
- contextcheck
- cyclop
- exhaustivestruct
- exhaustruct
- forbidigo
- funlen
- gochecknoglobals
Expand All @@ -17,21 +18,24 @@ linters:
- goerr113
- gomnd
- ifshort
- interfacebloat
- interfacer
- ireturn
- lll
- nestif
- nilnil
- nlreturn
- noctx
- nolintlint
- nonamedreturns
- nosnakecase
- revive
- tagliatelle
- testpackage
- varnamelen
- whitespace
- wrapcheck
- wsl
- nolintlint

linters-settings:
revive:
Expand Down
2 changes: 1 addition & 1 deletion api/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func startDatabaseAPI(w http.ResponseWriter, r *http.Request) {
if err != nil {
errMsg := fmt.Sprintf("could not upgrade: %s", err)
log.Error(errMsg)
http.Error(w, errMsg, 400)
http.Error(w, errMsg, http.StatusBadRequest)
return
}

Expand Down
44 changes: 29 additions & 15 deletions api/endpoints_debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"os"
"runtime/pprof"
"strings"
"time"

"github.com/safing/portbase/utils/debug"
Expand Down Expand Up @@ -44,11 +45,16 @@ func registerDebugEndpoints() error {
}

if err := RegisterEndpoint(Endpoint{
Path: "debug/cpu",
Read: PermitAnyone,
DataFunc: handleCPUProfile,
Name: "Get CPU Profile",
Description: "",
Path: "debug/cpu",
Read: PermitAnyone,
DataFunc: handleCPUProfile,
Name: "Get CPU Profile",
Description: strings.ReplaceAll(`Gather and return the CPU profile.
This data needs to gathered over a period of time, which is specified using the duration parameter.
You can easily view this data in your browser with this command (with Go installed):
"go tool pprof -http :8888 http://127.0.0.1:817/api/v1/debug/cpu"
`, `"`, "`"),
Parameters: []Parameter{{
Method: http.MethodGet,
Field: "duration",
Expand All @@ -60,21 +66,29 @@ func registerDebugEndpoints() error {
}

if err := RegisterEndpoint(Endpoint{
Path: "debug/heap",
Read: PermitAnyone,
DataFunc: handleHeapProfile,
Name: "Get Heap Profile",
Description: "",
Path: "debug/heap",
Read: PermitAnyone,
DataFunc: handleHeapProfile,
Name: "Get Heap Profile",
Description: strings.ReplaceAll(`Gather and return the heap memory profile.
You can easily view this data in your browser with this command (with Go installed):
"go tool pprof -http :8888 http://127.0.0.1:817/api/v1/debug/heap"
`, `"`, "`"),
}); err != nil {
return err
}

if err := RegisterEndpoint(Endpoint{
Path: "debug/allocs",
Read: PermitAnyone,
DataFunc: handleAllocsProfile,
Name: "Get Allocs Profile",
Description: "",
Path: "debug/allocs",
Read: PermitAnyone,
DataFunc: handleAllocsProfile,
Name: "Get Allocs Profile",
Description: strings.ReplaceAll(`Gather and return the memory allocation profile.
You can easily view this data in your browser with this command (with Go installed):
"go tool pprof -http :8888 http://127.0.0.1:817/api/v1/debug/allocs"
`, `"`, "`"),
}); err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ var (
mainMux = mux.NewRouter()

// server is the main server.
server = &http.Server{}
server = &http.Server{
ReadHeaderTimeout: 10 * time.Second,
}
handlerLock sync.RWMutex

allowedDevCORSOrigins = []string{
Expand Down
6 changes: 6 additions & 0 deletions modules/mgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ func EnableModuleManagement(changeNotifyFn func(*Module)) bool {
return false
}

// DisableModuleManagement disables module management and returns the module
// system to the default start/stop behavior.
func DisableModuleManagement() {
moduleMgmtEnabled.UnSet()
}

func (m *Module) notifyOfChange() {
if moduleMgmtEnabled.IsSet() && modulesChangeNotifyFn != nil {
m.StartWorker("notify of change", func(ctx context.Context) error {
Expand Down
12 changes: 11 additions & 1 deletion run/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func Run() int {
// Start
err := modules.Start()
if err != nil {
// Immediately return for a clean exit.
if errors.Is(err, modules.ErrCleanExit) {
return 0
}
Expand All @@ -41,8 +42,17 @@ func Run() int {
printStackTo(os.Stdout, "PRINTING STACK ON EXIT (STARTUP ERROR)")
}

// Trigger shutdown and wait for it to complete.
_ = modules.Shutdown()
return modules.GetExitStatusCode()
exitCode := modules.GetExitStatusCode()

// Return the exit code, if it was set.
if exitCode > 0 {
return exitCode
}

// Otherwise, return a default 1.
return 1
}

// Shutdown
Expand Down

0 comments on commit 85a84c1

Please sign in to comment.