diff options
| author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2025-01-02 18:03:28 -0800 |
|---|---|---|
| committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2025-01-02 18:03:28 -0800 |
| commit | f1963beac75d13352a08d5918e596b781facbdf8 (patch) | |
| tree | 5d5a1aa0e7a1bec9892813040df58c4c7415c040 | |
| parent | 94dd1ca677c716975d43f323c97ea26f8fd4c962 (diff) | |
| download | something-main.tar.gz something-main.zip | |
| -rw-r--r-- | api/api.go | 8 | ||||
| -rw-r--r-- | api/api_test.go | 74 | ||||
| -rw-r--r-- | args/args.go | 56 | ||||
| -rw-r--r-- | main.go | 20 | ||||
| -rw-r--r-- | scheduler/scheduler.go | 12 |
5 files changed, 85 insertions, 85 deletions
@@ -82,10 +82,10 @@ func MakeMux(argv *args.Arguments, dbConn *sql.DB) *http.ServeMux { mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) { requestContext := makeRequestContext() - (*requestContext.TemplateData)["Service"] = "something" - templateFile := "hello.html" - LogRequestContinuation(requestContext, r, w)(template.TemplateContinuation(templateFile, true), FailurePassingContinuation)(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation) + (*requestContext.TemplateData)["Service"] = "something" + templateFile := "hello.html" + LogRequestContinuation(requestContext, r, w)(template.TemplateContinuation(templateFile, true), FailurePassingContinuation)(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation) }) - return mux + return mux } diff --git a/api/api_test.go b/api/api_test.go index 5cd1280..6dbd44a 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -2,7 +2,7 @@ package api_test import ( "database/sql" - "io" + "io" "net/http/httptest" "os" "strings" @@ -20,70 +20,70 @@ func setup(t *testing.T) (*sql.DB, *httptest.Server) { testDb := database.MakeConn(&randomDb) database.Migrate(testDb) - arguments := &args.Arguments{ - TemplatePath: "../templates", - StaticPath: "../static", - } + arguments := &args.Arguments{ + TemplatePath: "../templates", + StaticPath: "../static", + } - mux := api.MakeMux(arguments, testDb) - testServer := httptest.NewServer(mux) + mux := api.MakeMux(arguments, testDb) + testServer := httptest.NewServer(mux) - t.Cleanup(func() { + t.Cleanup(func() { testServer.Close() testDb.Close() os.Remove(randomDb) - }) + }) return testDb, testServer } func assertResponseCode(t *testing.T, resp *httptest.ResponseRecorder, statusCode int) { - if resp.Code != statusCode { - t.Errorf("code is unexpected: %d, expected %d", resp.Code, statusCode) - } + if resp.Code != statusCode { + t.Errorf("code is unexpected: %d, expected %d", resp.Code, statusCode) + } } func assertResponseBody(t *testing.T, resp *httptest.ResponseRecorder, body string) { - buf := new(strings.Builder) - _, err := io.Copy(buf, resp.Body) - if err != nil { - panic("could not read response body") - } - bodyStr := buf.String() - if bodyStr != body { - t.Errorf("body is unexpected: %s, expected %s", bodyStr, body) - } + buf := new(strings.Builder) + _, err := io.Copy(buf, resp.Body) + if err != nil { + panic("could not read response body") + } + bodyStr := buf.String() + if bodyStr != body { + t.Errorf("body is unexpected: %s, expected %s", bodyStr, body) + } } func TestHealthcheck(t *testing.T) { _, testServer := setup(t) - req := httptest.NewRequest("GET", "/health", nil) - resp := httptest.NewRecorder() - testServer.Config.Handler.ServeHTTP(resp, req) + req := httptest.NewRequest("GET", "/health", nil) + resp := httptest.NewRecorder() + testServer.Config.Handler.ServeHTTP(resp, req) - assertResponseCode(t, resp, 200) - assertResponseBody(t, resp, "healthy") + assertResponseCode(t, resp, 200) + assertResponseBody(t, resp, "healthy") } func TestHello(t *testing.T) { _, testServer := setup(t) - req := httptest.NewRequest("GET", "/", nil) - resp := httptest.NewRecorder() - testServer.Config.Handler.ServeHTTP(resp, req) + req := httptest.NewRequest("GET", "/", nil) + resp := httptest.NewRecorder() + testServer.Config.Handler.ServeHTTP(resp, req) - assertResponseCode(t, resp, 200) + assertResponseCode(t, resp, 200) } func TestCachingStaticFiles(t *testing.T) { _, testServer := setup(t) - req := httptest.NewRequest("GET", "/static/css/styles.css", nil) - resp := httptest.NewRecorder() - testServer.Config.Handler.ServeHTTP(resp, req) + req := httptest.NewRequest("GET", "/static/css/styles.css", nil) + resp := httptest.NewRecorder() + testServer.Config.Handler.ServeHTTP(resp, req) - assertResponseCode(t, resp, 200) - if resp.Header().Get("Cache-Control") != "public, max-age=3600" { - t.Errorf("client cache will live indefinitely for static files, which is probably not great! %s", resp.Header().Get("Cache-Control")) - } + assertResponseCode(t, resp, 200) + if resp.Header().Get("Cache-Control") != "public, max-age=3600" { + t.Errorf("client cache will live indefinitely for static files, which is probably not great! %s", resp.Header().Get("Cache-Control")) + } } diff --git a/args/args.go b/args/args.go index bd3dd04..8c03ecb 100644 --- a/args/args.go +++ b/args/args.go @@ -2,9 +2,9 @@ package args import ( "flag" - "fmt" - "os" - "sync" + "fmt" + "os" + "sync" ) type Arguments struct { @@ -29,27 +29,27 @@ func isDirectory(path string) (bool, error) { } func validateArgs(args *Arguments) error { - templateIsDir, err := isDirectory(args.TemplatePath) - if err != nil || !templateIsDir { - return fmt.Errorf("template path is not an accessible directory %s", err) - } - staticPathIsDir, err := isDirectory(args.StaticPath) - if err != nil || !staticPathIsDir { - return fmt.Errorf("static path is not an accessible directory %s", err) - } - return nil + templateIsDir, err := isDirectory(args.TemplatePath) + if err != nil || !templateIsDir { + return fmt.Errorf("template path is not an accessible directory %s", err) + } + staticPathIsDir, err := isDirectory(args.StaticPath) + if err != nil || !staticPathIsDir { + return fmt.Errorf("static path is not an accessible directory %s", err) + } + return nil } var lock = &sync.Mutex{} var args *Arguments func GetArgs() (*Arguments, error) { - lock.Lock() - defer lock.Unlock() + lock.Lock() + defer lock.Unlock() - if args != nil { - return args, nil - } + if args != nil { + return args, nil + } databasePath := flag.String("database-path", "./something.db", "Path to the SQLite database") @@ -65,18 +65,18 @@ func GetArgs() (*Arguments, error) { flag.Parse() args = &Arguments{ - DatabasePath: *databasePath, - TemplatePath: *templatePath, - StaticPath: *staticPath, - Port: *port, - Server: *server, - Migrate: *migrate, - Scheduler: *scheduler, + DatabasePath: *databasePath, + TemplatePath: *templatePath, + StaticPath: *staticPath, + Port: *port, + Server: *server, + Migrate: *migrate, + Scheduler: *scheduler, + } + err := validateArgs(args) + if err != nil { + return nil, err } - err := validateArgs(args) - if err != nil { - return nil, err - } return args, nil } @@ -37,19 +37,19 @@ func main() { } if argv.Scheduler { - go func() { - scheduler.StartScheduler(dbConn, argv) - }() + go func() { + scheduler.StartScheduler(dbConn, argv) + }() } if argv.Server { mux := api.MakeMux(argv, dbConn) log.Println("🚀🚀 something API listening on port", argv.Port) go func() { - server := &http.Server{ - Addr: ":" + fmt.Sprint(argv.Port), - Handler: mux, - } + server := &http.Server{ + Addr: ":" + fmt.Sprint(argv.Port), + Handler: mux, + } err = server.ListenAndServe() if err != nil { log.Fatal(err) @@ -57,7 +57,7 @@ func main() { }() } - if argv.Server || argv.Scheduler { - select {} // block forever - } + if argv.Server || argv.Scheduler { + select {} // block forever + } } diff --git a/scheduler/scheduler.go b/scheduler/scheduler.go index c152179..e6c08bf 100644 --- a/scheduler/scheduler.go +++ b/scheduler/scheduler.go @@ -6,13 +6,13 @@ import ( "time" "git.simponic.xyz/simponic/something/args" - "github.com/go-co-op/gocron/v2" + "github.com/go-co-op/gocron/v2" ) func StartScheduler(_dbConn *sql.DB, argv *args.Arguments) { scheduler, err := gocron.NewScheduler() if err != nil { - panic("could not create scheduler") + panic("could not create scheduler") } _, err = scheduler.NewJob( @@ -21,14 +21,14 @@ func StartScheduler(_dbConn *sql.DB, argv *args.Arguments) { ), gocron.NewTask( func(msg string) { - log.Println(msg) + log.Println(msg) }, "it's a beautiful new day!", ), ) - if err != nil { - panic("could not create job") - } + if err != nil { + panic("could not create job") + } scheduler.Start() } |
