-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
039fc31
commit f67c2df
Showing
12 changed files
with
177 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package main | ||
|
||
import ( | ||
"database/sql" | ||
"errors" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/golang-migrate/migrate/v4" | ||
) | ||
|
||
type MigrationLogger struct { | ||
Prefix string | ||
} | ||
|
||
func (m MigrationLogger) Printf(format string, v ...interface{}) { | ||
log.Print(m.Prefix, fmt.Sprintf(format, v...)) | ||
} | ||
|
||
func (m MigrationLogger) Verbose() bool { | ||
return false | ||
} | ||
|
||
func connectAndMigrateDB(dbConn string) (_ *sql.DB, close func() error, err error) { | ||
if dbConn == "" { | ||
return nil, nil, fmt.Errorf("env var DB_CONN is not provided") | ||
} | ||
|
||
m, err := migrate.New("file://migrations", dbConn) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("create migrator: %w", err) | ||
} | ||
m.Log = MigrationLogger{Prefix: "migration: "} | ||
err = m.Up() | ||
if err != nil && !errors.Is(err, migrate.ErrNoChange) { | ||
return nil, nil, fmt.Errorf("migrate up: %w", err) | ||
} | ||
|
||
close = func() error { return nil } | ||
db, err := sql.Open("postgres", dbConn) | ||
if err == nil { | ||
close = db.Close | ||
err = db.Ping() | ||
} | ||
|
||
if err != nil { | ||
close() | ||
return nil, nil, fmt.Errorf("connect to db: %w", err) | ||
} | ||
return db, close, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"runtime/pprof" | ||
) | ||
|
||
func NewDebugMiddleware(debugToken string) func(http.Handler) http.Handler { | ||
return func(next http.Handler) http.Handler { | ||
if debugToken == "" { | ||
return http.HandlerFunc(http.NotFound) | ||
} | ||
fn := func(w http.ResponseWriter, r *http.Request) { | ||
if r.FormValue("debug_token") == debugToken { | ||
w.Header().Set("Content-Type", "application/octet-stream") | ||
w.Header().Set("Content-Disposition", `attachment; filename="profile"`) | ||
if err := pprof.StartCPUProfile(w); err != nil { | ||
w.Header().Set("Content-Type", "text/plain; charset=utf-8") | ||
w.Header().Del("Content-Disposition") | ||
w.Header().Set("X-Go-Pprof", "1") | ||
w.WriteHeader(http.StatusInternalServerError) | ||
fmt.Fprintf(w, "Could not enable CPU profiling: %s\n", err) | ||
return | ||
} | ||
rr := httptest.ResponseRecorder{} | ||
next.ServeHTTP(&rr, r) | ||
pprof.StopCPUProfile() | ||
} else { | ||
http.NotFound(w, r) | ||
} | ||
} | ||
return http.HandlerFunc(fn) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.