diff --git a/.editorconfig b/.editorconfig index c0896ac..15b3f2a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -8,6 +8,6 @@ insert_final_newline = true indent_style = tab indent_size = tab -[{*.yaml,*.yml, .nix}] +[{*.yaml,*.yml,*.nix}] indent_style = space indent_size = 2 diff --git a/.pg_format b/.pg_format new file mode 100644 index 0000000..4affa06 --- /dev/null +++ b/.pg_format @@ -0,0 +1 @@ +tabs=1 diff --git a/api/go.mod b/api/go.mod index 76fee6b..45ee440 100644 --- a/api/go.mod +++ b/api/go.mod @@ -5,7 +5,11 @@ go 1.20 require ( github.com/cenkalti/hub v1.0.1-0.20160527103212-11382a9960d3 // indirect github.com/cenkalti/rpc2 v0.0.0-20180727162946-9642ea02d0aa // indirect + github.com/golang-migrate/migrate/v4 v4.16.2 // indirect github.com/gorilla/websocket v1.4.1 // indirect + github.com/hashicorp/errwrap v1.1.0 // indirect + github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/lib/pq v1.10.9 // indirect github.com/siku2/arigo v0.2.0 // indirect + go.uber.org/atomic v1.7.0 // indirect ) diff --git a/api/go.sum b/api/go.sum index 1c2f564..3ae3c22 100644 --- a/api/go.sum +++ b/api/go.sum @@ -4,8 +4,16 @@ github.com/cenkalti/hub v1.0.1-0.20160527103212-11382a9960d3/go.mod h1:tcYwtS3a2 github.com/cenkalti/rpc2 v0.0.0-20180727162946-9642ea02d0aa h1:t+iWhuJE2aropY4uxKMVbyP+IJ29o422f7YAd73aTjg= github.com/cenkalti/rpc2 v0.0.0-20180727162946-9642ea02d0aa/go.mod h1:v2npkhrXyk5BCnkNIiPdRI23Uq6uWPUQGL2hnRcRr/M= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/golang-migrate/migrate/v4 v4.16.2 h1:8coYbMKUyInrFk1lfGfRovTLAW7PhWp8qQDT2iKfuoA= +github.com/golang-migrate/migrate/v4 v4.16.2/go.mod h1:pfcJX4nPHaVdc5nmdCikFBWtm+UBpiZjRNNsyBbp0/o= github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM= github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I= +github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo= +github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -13,3 +21,5 @@ github.com/siku2/arigo v0.2.0 h1:gB5zGgCNtRd83IrdeimL+Jp5Yhj9wKo/DhAvVXh/l4k= github.com/siku2/arigo v0.2.0/go.mod h1:/slSGOCL5awvY4Q9SlyR15wBjeGllHLrLE1E/2MztMQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= +go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= diff --git a/api/main.go b/api/main.go index 22c1909..a09e5b6 100644 --- a/api/main.go +++ b/api/main.go @@ -1,13 +1,40 @@ package main import ( - "fmt" + "log" "net/http" + "encoding/json" + + services "tide/api/services" + controllers "tide/api/controllers" ) func main() { - http.HandleFunc("/", func(resp http.ResponseWriter, req *http.Request) { - fmt.Println("test") + d, err := services.NewDatabase() + if err != nil { + log.Fatal("Could not create database: ", err) + } + err = d.Migrate() + if err != nil { + log.Fatal("Could not migrate database: ", err) + } + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case http.MethodPost: + // r.Body + var newItem NewItem + err := json.NewDecoder(r.Body).Decode(newItem) + if err != nil { + http.Error(w, "Bad request", http.StatusBadRequest) + return + } + item := controllers.NewItem() + json.NewEncoder(w).Encode(item) + w.WriteHeader(http.StatusCreated) + default: + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + } }) http.ListenAndServe(":7890", nil) } diff --git a/api/migrations/20230916184309_initial.down.sql b/api/migrations/20230916184309_initial.down.sql new file mode 100644 index 0000000..dfed2b1 --- /dev/null +++ b/api/migrations/20230916184309_initial.down.sql @@ -0,0 +1,2 @@ +DROP TABLE items; + diff --git a/api/migrations/20230916184309_initial.up.sql b/api/migrations/20230916184309_initial.up.sql new file mode 100644 index 0000000..4181ecf --- /dev/null +++ b/api/migrations/20230916184309_initial.up.sql @@ -0,0 +1,9 @@ +CREATE TABLE items ( + id varchar(255) NOT NULL, + name varchar(255) NOT NULL, + path text NOT NULL, + size bigint, + files json, + PRIMARY KEY (id) +); + diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 3125192..f49233d 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -2,7 +2,7 @@ version: "3.8" services: api: - build: + build: context: ./api dockerfile: Dockerfile.dev ports: @@ -34,3 +34,6 @@ services: interval: 5s timeout: 5s retries: 5 + +volumes: + db: diff --git a/docker-compose.yml b/docker-compose.yml index f32106c..e42c3d3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -25,3 +25,6 @@ services: interval: 5s timeout: 5s retries: 5 + +volumes: + db: diff --git a/shell.nix b/shell.nix index b0ef626..81e106a 100644 --- a/shell.nix +++ b/shell.nix @@ -4,5 +4,7 @@ pkgs.mkShell { go wgo aria + go-migrate + pgformatter ]; }