parent
da239cf9ad
commit
04506ed01f
6 changed files with 226 additions and 42 deletions
@ -1,21 +1,21 @@ |
||||
package book |
||||
|
||||
type Book struct { |
||||
ID int |
||||
Title string |
||||
Authors []string |
||||
SortAuthor string |
||||
ISBN10 string |
||||
ISBN13 string |
||||
Format string |
||||
Genre string |
||||
Publisher string |
||||
Series string |
||||
Volume string |
||||
Year string |
||||
Signed bool |
||||
Description string |
||||
Notes string |
||||
OnLoan string |
||||
CoverURL string |
||||
ID int `json:"-"` |
||||
Title string `json:"title"` |
||||
Authors []string `json:"authors"` |
||||
SortAuthor string `json:"sortAuthor"` |
||||
ISBN10 string `json:"isbn-10"` |
||||
ISBN13 string `json:"isbn-13"` |
||||
Format string `json:"format"` |
||||
Genre string `json:"genre"` |
||||
Publisher string `json:"publisher"` |
||||
Series string `json:"series"` |
||||
Volume string `json:"volume"` |
||||
Year string `json:"year"` |
||||
Signed bool `json:"signed"` |
||||
Description string `json:"description"` |
||||
Notes string `json:"notes"` |
||||
OnLoan string `json:"onLoan"` |
||||
CoverURL string `json:"coverURL"` |
||||
} |
||||
|
@ -1,15 +1,80 @@ |
||||
package main |
||||
|
||||
import ( |
||||
"fmt" |
||||
"context" |
||||
"encoding/json" |
||||
"io/fs" |
||||
"log" |
||||
"net/http" |
||||
|
||||
"git.yetaga.in/alazyreader/library/book" |
||||
"git.yetaga.in/alazyreader/library/database" |
||||
"git.yetaga.in/alazyreader/library/frontend" |
||||
) |
||||
|
||||
// test 3
|
||||
type Library interface { |
||||
GetAllBooks(context.Context) ([]book.Book, error) |
||||
} |
||||
|
||||
type Router struct { |
||||
static fs.FS |
||||
lib Library |
||||
} |
||||
|
||||
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) { |
||||
if req.URL.Path == "/api" { |
||||
APIHandler(r.lib).ServeHTTP(w, req) |
||||
return |
||||
} |
||||
StaticHandler(r.static).ServeHTTP(w, req) |
||||
return |
||||
} |
||||
|
||||
func APIHandler(l Library) http.Handler { |
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
||||
books, err := l.GetAllBooks(r.Context()) |
||||
if err != nil { |
||||
http.Error(w, err.Error(), http.StatusInternalServerError) |
||||
return |
||||
} |
||||
b, err := json.Marshal(books) |
||||
if err != nil { |
||||
http.Error(w, err.Error(), http.StatusInternalServerError) |
||||
return |
||||
} |
||||
w.Header().Set("Content-Type", "application/json") |
||||
w.WriteHeader(http.StatusOK) |
||||
w.Write(b) |
||||
w.Write([]byte("\n")) |
||||
}) |
||||
} |
||||
|
||||
func StaticHandler(f fs.FS) http.Handler { |
||||
return http.FileServer(http.FS(f)) |
||||
} |
||||
|
||||
func main() { |
||||
subfs, _ := fs.Sub(frontend.Static, "files") |
||||
fmt.Println(http.ListenAndServe(":8080", http.FileServer(http.FS(subfs)))) |
||||
f, err := frontend.Root() |
||||
if err != nil { |
||||
log.Fatalln(err) |
||||
} |
||||
lib, err := database.NewMySQLConnection("root", "KigYBNCT9IU5XyB3ehzMLFWyI", "127.0.0.1", "3306", "library") |
||||
if err != nil { |
||||
log.Fatalln(err) |
||||
} |
||||
err = lib.PrepareDatabase(context.Background()) |
||||
if err != nil { |
||||
log.Fatalln(err) |
||||
} |
||||
latest, run, err := lib.RunMigrations(context.Background()) |
||||
if err != nil { |
||||
log.Fatalln(err) |
||||
} |
||||
log.Printf("latest migration: %d; migrations run: %d", latest, run) |
||||
r := &Router{ |
||||
static: f, |
||||
lib: lib, |
||||
} |
||||
log.Println("Listening on http://localhost:8080/") |
||||
log.Fatalln(http.ListenAndServe(":8080", r)) |
||||
} |
||||
|
@ -0,0 +1,20 @@ |
||||
CREATE TABLE IF NOT EXISTS books( |
||||
id INT NOT NULL AUTO_INCREMENT, |
||||
title VARCHAR(1024), |
||||
authors VARCHAR(1024), |
||||
sortauthor VARCHAR(1024), |
||||
isbn10 VARCHAR(10), |
||||
isbn13 VARCHAR(13), |
||||
format VARCHAR(255), |
||||
genre VARCHAR(255), |
||||
publisher VARCHAR(255), |
||||
series VARCHAR(255), |
||||
volume VARCHAR(255), |
||||
year VARCHAR(10), |
||||
signed BOOLEAN, |
||||
description TEXT, |
||||
notes TEXT, |
||||
onloan VARCHAR(255), |
||||
coverurl VARCHAR(1024), |
||||
PRIMARY KEY (id) |
||||
) |
@ -1,6 +1,13 @@ |
||||
package frontend |
||||
|
||||
import "embed" |
||||
import ( |
||||
"embed" |
||||
"io/fs" |
||||
) |
||||
|
||||
//go:embed files
|
||||
var Static embed.FS |
||||
var static embed.FS |
||||
|
||||
func Root() (fs.FS, error) { |
||||
return fs.Sub(static, "files") |
||||
} |
||||
|
Loading…
Reference in new issue