basic read and write

This commit is contained in:
Erik Winter 2023-12-16 14:45:38 +01:00
parent 1992dd31cc
commit ca0abff155
9 changed files with 407 additions and 9 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
test.db

1
app/emdb.go Normal file
View File

@ -0,0 +1 @@
package app

View File

@ -1,8 +1,9 @@
package handler package app
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log/slog"
"net/http" "net/http"
) )
@ -11,7 +12,9 @@ func Index(w http.ResponseWriter) {
fmt.Fprint(w, `{"message":"emdb index"}`) fmt.Fprint(w, `{"message":"emdb index"}`)
} }
func Error(w http.ResponseWriter, status int, message string, err error) { func Error(w http.ResponseWriter, status int, message string, err error, logger *slog.Logger) {
logger.Error(message, "error", err)
w.WriteHeader(status) w.WriteHeader(status)
var resBody []byte var resBody []byte

124
app/movie.go Normal file
View File

@ -0,0 +1,124 @@
package app
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"github.com/google/uuid"
)
type Movie struct {
ID string `json:"id"`
Title string `json:"title"`
Year int `json:"year"`
IMDBID string `json:"imdb_id"`
WatchedOn string `json:"watched_on"`
Rating int `json:"rating"`
Comment string `json:"comment"`
}
type MovieAPI struct {
repo *SQLite
logger *slog.Logger
}
func NewMovieAPI(repo *SQLite, logger *slog.Logger) *MovieAPI {
return &MovieAPI{
repo: repo,
logger: logger.With("api", "movie"),
}
}
func (api *MovieAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
logger := api.logger.With("method", "serveHTTP")
movieID, _ := ShiftPath(r.URL.Path)
switch {
case r.Method == http.MethodGet && movieID != "":
api.Read(w, r, movieID)
case r.Method == http.MethodGet && movieID == "":
api.List(w, r)
case r.Method == http.MethodPost:
api.Create(w, r)
default:
Error(w, http.StatusNotFound, "unregistered path", fmt.Errorf("method %q with subpath %q was not registered in /movie", r.Method, movieID), logger)
}
}
func (api *MovieAPI) Read(w http.ResponseWriter, r *http.Request, movieID string) {
logger := api.logger.With("method", "read")
movie, err := api.repo.FindOne(movieID)
switch {
case errors.Is(err, sql.ErrNoRows):
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, `{"message":"not found"}`)
return
case err != nil:
Error(w, http.StatusInternalServerError, "could not get movie", err, logger)
return
}
resJson, err := json.Marshal(movie)
if err != nil {
Error(w, http.StatusInternalServerError, "could not marshal response", err, logger)
return
}
fmt.Fprint(w, string(resJson))
}
func (api *MovieAPI) Create(w http.ResponseWriter, r *http.Request) {
logger := api.logger.With("method", "create")
body, err := io.ReadAll(r.Body)
if err != nil {
Error(w, http.StatusBadRequest, "could not read body", err, logger)
return
}
defer r.Body.Close()
var movie *Movie
if err := json.Unmarshal(body, &movie); err != nil {
Error(w, http.StatusBadRequest, "could not unmarshal request body", err, logger)
return
}
movie.ID = uuid.New().String()
if err := api.repo.StoreMovie(movie); err != nil {
Error(w, http.StatusInternalServerError, "could not store movie", err, logger)
return
}
resBody, err := json.Marshal(movie)
if err != nil {
Error(w, http.StatusInternalServerError, "could not marshal movie", err, logger)
return
}
fmt.Fprint(w, string(resBody))
}
func (api *MovieAPI) List(w http.ResponseWriter, r *http.Request) {
logger := api.logger.With("method", "list")
movies, err := api.repo.FindAll()
if err != nil {
Error(w, http.StatusInternalServerError, "could not get movies", err, logger)
return
}
resBody, err := json.Marshal(movies)
if err != nil {
Error(w, http.StatusInternalServerError, "could not marshal movies", err, logger)
return
}
fmt.Fprint(w, string(resBody))
}

View File

@ -1,4 +1,4 @@
package handler package app
import ( import (
"fmt" "fmt"
@ -43,7 +43,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// authenticate // authenticate
if key := r.Header.Get("Authorization"); key != s.apiKey { if key := r.Header.Get("Authorization"); key != s.apiKey {
Error(rec, http.StatusUnauthorized, "unauthorized", fmt.Errorf("invalid api key")) Error(rec, http.StatusUnauthorized, "unauthorized", fmt.Errorf("invalid api key"), logger)
logger.Info("unauthorized", "key", key) logger.Info("unauthorized", "key", key)
returnResponse(w, rec, r, logger) returnResponse(w, rec, r, logger)
return return
@ -58,7 +58,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
api, ok := s.apis[head] api, ok := s.apis[head]
if !ok { if !ok {
Error(rec, http.StatusNotFound, "Not found", fmt.Errorf("%s is not a valid path", r.URL.Path)) Error(rec, http.StatusNotFound, "Not found", fmt.Errorf("%s is not a valid path", r.URL.Path), logger)
returnResponse(w, rec, r, logger) returnResponse(w, rec, r, logger)
return return
} }

183
app/sqlite.go Normal file
View File

@ -0,0 +1,183 @@
package app
import (
"database/sql"
"errors"
"fmt"
_ "modernc.org/sqlite"
)
type sqliteMigration string
var sqliteMigrations = []sqliteMigration{
`CREATE TABLE movie ("id" TEXT UNIQUE, "title" TEXT, "year" INTEGER, "imdb_id" TEXT, "watched_on" TEXT, "rating" INTEGER, "comment" TEXT)`,
`CREATE TABLE system ("latest_sync" INTEGER)`,
`INSERT INTO system (latest_sync) VALUES (0)`,
}
var (
ErrInvalidConfiguration = errors.New("invalid configuration")
ErrIncompatibleSQLMigration = errors.New("incompatible migration")
ErrNotEnoughSQLMigrations = errors.New("already more migrations than wanted")
ErrSqliteFailure = errors.New("sqlite returned an error")
)
type SQLite struct {
db *sql.DB
}
func NewSQLite(dbPath string) (*SQLite, error) {
db, err := sql.Open("sqlite", dbPath)
if err != nil {
return &SQLite{}, fmt.Errorf("%w: %v", ErrInvalidConfiguration, err)
}
s := &SQLite{
db: db,
}
if err := s.migrate(sqliteMigrations); err != nil {
return &SQLite{}, err
}
return s, nil
}
func (s *SQLite) StoreMovie(movie *Movie) error {
tx, err := s.db.Begin()
if err != nil {
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
}
defer tx.Rollback()
if _, err := s.db.Exec(`REPLACE INTO movie (id, title, year, imdb_id, watched_on, rating, comment)
VALUES (?, ?, ?, ?, ?, ?, ?)`,
movie.ID, movie.Title, movie.Year, movie.IMDBID, movie.WatchedOn, movie.Rating, movie.Comment); err != nil {
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
}
if err := tx.Commit(); err != nil {
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
}
return nil
}
func (s *SQLite) FindOne(id string) (*Movie, error) {
row := s.db.QueryRow(`
SELECT title, year, imdb_id, watched_on, rating, comment
FROM movie
WHERE id=?`, id)
if row.Err() != nil {
return nil, row.Err()
}
movie := &Movie{
ID: id,
}
if err := row.Scan(&movie.Title, &movie.Year, &movie.IMDBID, &movie.WatchedOn, &movie.Rating, &movie.Comment); err != nil {
return nil, err
}
return movie, nil
}
func (s *SQLite) FindAll() ([]*Movie, error) {
rows, err := s.db.Query(`
SELECT id, title, year, imdb_id, watched_on, rating, comment
FROM movie`)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
}
movies := make([]*Movie, 0)
defer rows.Close()
for rows.Next() {
var id, title, imdbID, watchedOn, comment string
var year, rating int
if err := rows.Scan(&id, &title, &year, &imdbID, &watchedOn, &rating, &comment); err != nil {
return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
}
movies = append(movies, &Movie{
ID: id,
Title: title,
Year: year,
IMDBID: imdbID,
WatchedOn: watchedOn,
Rating: rating,
Comment: comment,
})
}
return movies, nil
}
func (s *SQLite) migrate(wanted []sqliteMigration) error {
// admin table
if _, err := s.db.Exec(`
CREATE TABLE IF NOT EXISTS migration
("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "query" TEXT)
`); err != nil {
return err
}
// find existing
rows, err := s.db.Query(`SELECT query FROM migration ORDER BY id`)
if err != nil {
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
}
existing := []sqliteMigration{}
for rows.Next() {
var query string
if err := rows.Scan(&query); err != nil {
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
}
existing = append(existing, sqliteMigration(query))
}
rows.Close()
// compare
missing, err := compareMigrations(wanted, existing)
if err != nil {
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
}
// execute missing
for _, query := range missing {
if _, err := s.db.Exec(string(query)); err != nil {
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
}
// register
if _, err := s.db.Exec(`
INSERT INTO migration
(query) VALUES (?)
`, query); err != nil {
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
}
}
return nil
}
func compareMigrations(wanted, existing []sqliteMigration) ([]sqliteMigration, error) {
needed := []sqliteMigration{}
if len(wanted) < len(existing) {
return []sqliteMigration{}, ErrNotEnoughSQLMigrations
}
for i, want := range wanted {
switch {
case i >= len(existing):
needed = append(needed, want)
case want == existing[i]:
// do nothing
case want != existing[i]:
return []sqliteMigration{}, fmt.Errorf("%w: %v", ErrIncompatibleSQLMigration, want)
}
}
return needed, nil
}

24
go.mod
View File

@ -1,3 +1,25 @@
module "ewintr.nl/emdb" module ewintr.nl/emdb
go 1.21 go 1.21
require (
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
golang.org/x/mod v0.3.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/tools v0.0.0-20201124115921-2c860bdd6e78 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
lukechampine.com/uint128 v1.2.0 // indirect
modernc.org/cc/v3 v3.40.0 // indirect
modernc.org/ccgo/v3 v3.16.13 // indirect
modernc.org/libc v1.29.0 // indirect
modernc.org/mathutil v1.6.0 // indirect
modernc.org/memory v1.7.2 // indirect
modernc.org/opt v0.1.3 // indirect
modernc.org/sqlite v1.28.0 // indirect
modernc.org/strutil v1.1.3 // indirect
modernc.org/token v1.0.1 // indirect
)

57
go.sum Normal file
View File

@ -0,0 +1,57 @@
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20201124115921-2c860bdd6e78 h1:M8tBwCtWD/cZV9DZpFYRUgaymAYAr+aIUTWzDaM3uPs=
golang.org/x/tools v0.0.0-20201124115921-2c860bdd6e78/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
lukechampine.com/uint128 v1.2.0 h1:mBi/5l91vocEN8otkC5bDLhi2KdCticRiwbdB0O+rjI=
lukechampine.com/uint128 v1.2.0/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk=
modernc.org/cc/v3 v3.40.0 h1:P3g79IUS/93SYhtoeaHW+kRCIrYaxJ27MFPv+7kaTOw=
modernc.org/cc/v3 v3.40.0/go.mod h1:/bTg4dnWkSXowUO6ssQKnOV0yMVxDYNIsIrzqTFDGH0=
modernc.org/ccgo/v3 v3.16.13 h1:Mkgdzl46i5F/CNR/Kj80Ri59hC8TKAhZrYSaqvkwzUw=
modernc.org/ccgo/v3 v3.16.13/go.mod h1:2Quk+5YgpImhPjv2Qsob1DnZ/4som1lJTodubIcoUkY=
modernc.org/libc v1.29.0 h1:tTFRFq69YKCF2QyGNuRUQxKBm1uZZLubf6Cjh/pVHXs=
modernc.org/libc v1.29.0/go.mod h1:DaG/4Q3LRRdqpiLyP0C2m1B8ZMGkQ+cCgOIjEtQlYhQ=
modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4=
modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo=
modernc.org/memory v1.7.2 h1:Klh90S215mmH8c9gO98QxQFsY+W451E8AnzjoE2ee1E=
modernc.org/memory v1.7.2/go.mod h1:NO4NVCQy0N7ln+T9ngWqOQfi7ley4vpwvARR+Hjw95E=
modernc.org/opt v0.1.3 h1:3XOZf2yznlhC+ibLltsDGzABUGVx8J6pnFMS3E4dcq4=
modernc.org/opt v0.1.3/go.mod h1:WdSiB5evDcignE70guQKxYUl14mgWtbClRi5wmkkTX0=
modernc.org/sqlite v1.28.0 h1:Zx+LyDDmXczNnEQdvPuEfcFVA2ZPyaD7UCZDjef3BHQ=
modernc.org/sqlite v1.28.0/go.mod h1:Qxpazz0zH8Z1xCFyi5GSL3FzbtZ3fvbjmywNogldEW0=
modernc.org/strutil v1.1.3 h1:fNMm+oJklMGYfU9Ylcywl0CO5O6nTfaowNsh2wpPjzY=
modernc.org/strutil v1.1.3/go.mod h1:MEHNA7PdEnEwLvspRMtWTNnp2nnyvMfkimT1NKNAGbw=
modernc.org/token v1.0.1 h1:A3qvTqOwexpfZZeyI0FeGPDlSWX5pjZu9hF4lU+EKWg=
modernc.org/token v1.0.1/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=

View File

@ -9,7 +9,7 @@ import (
"strconv" "strconv"
"syscall" "syscall"
"ewintr.nl/emdb/handler" "ewintr.nl/emdb/app"
) )
func main() { func main() {
@ -20,10 +20,17 @@ func main() {
os.Exit(1) os.Exit(1)
} }
apiKey := getParam("API_KEY", "hoi") apiKey := getParam("API_KEY", "hoi")
repo, err := app.NewSQLite(getParam("DB_PATH", "test.db"))
if err != nil {
fmt.Printf("could not create new sqlite repo: %s", err.Error())
os.Exit(1)
}
apis := handler.APIIndex{} apis := app.APIIndex{
"movie": app.NewMovieAPI(repo, logger),
}
go http.ListenAndServe(fmt.Sprintf(":%d", port), handler.NewServer(apiKey, apis, logger)) go http.ListenAndServe(fmt.Sprintf(":%d", port), app.NewServer(apiKey, apis, logger))
c := make(chan os.Signal, 1) c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)