From ce75957e592c0dcba21e3c7c952c72530fd388e9 Mon Sep 17 00:00:00 2001 From: jan Date: Tue, 11 Oct 2016 15:44:33 +0200 Subject: /health route zum anzeigen von mem usage und offene db connections diff --git a/grilist/health.go b/grilist/health.go new file mode 100644 index 0000000..15697ea --- /dev/null +++ b/grilist/health.go @@ -0,0 +1,16 @@ +package grilist + +import ( + "fmt" + "net/http" + "runtime" + + "github.com/julienschmidt/httprouter" +) + +func (g *Grilist) HealthCheck(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { + var mem runtime.MemStats + runtime.ReadMemStats(&mem) + + w.Write([]byte(fmt.Sprintf("mem in use: %d\nopen g.DB connections: %d", mem.Alloc, g.DB.Stats().OpenConnections))) +} diff --git a/main.go b/main.go index 68a9ede..9231f7c 100644 --- a/main.go +++ b/main.go @@ -68,6 +68,7 @@ func main() { } router.GET("/", index) + router.GET("/health", app.HealthCheck) router.HandlerFunc("GET", "/login", login.DisplayLogin) router.HandlerFunc("POST", "/login", login.Login) router.HandlerFunc("GET", "/settings", login.DisplayUserSettings) -- cgit v0.10.1 From f9c8526a4f11e075b4a9a506b246605e6dae1436 Mon Sep 17 00:00:00 2001 From: jan Date: Tue, 11 Oct 2016 15:59:52 +0200 Subject: Makefile 'app' target diff --git a/Makefile b/Makefile index fbef6fa..9206ed2 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,7 @@ include config.mk +BINARY_PATH=./bin +APP_NAME=grilist IMPORTER_FILES = $(shell find tools/importer/ -type f -name '*.go') CSS_FILES = $(patsubst assets_src/%, assets/%, $(shell find assets_src/css/ -type f -name '*.css')) @@ -12,7 +14,7 @@ LESSC := ./node_modules/less/bin/lessc -include $(DEPFILES) -all: $(CSS_FILES) $(LESS_FILES) $(JS_FILES) +all: assets app print_info: @echo $(CSS_FILES) @@ -20,6 +22,9 @@ print_info: @echo $(JS_FILES) @echo $(DEPFILES) +app: + go build -o $(BINARY_PATH)/$(APP_NAME) + .npm_update: package.json touch $@ npm install @@ -27,7 +32,9 @@ print_info: package.json: package.json.tpl config.mk export NAME=$(NAME) &&\ export VERSION=$(VERSION) &&\ - ./bin/template.awk < $< | sh > $@ + $(BINARY_PATH)/template.awk < $< | sh > $@ + +assets: $(CSS_FILES) $(LESS_FILES) $(JS_FILES) assets/css/%.css: assets_src/css/%.css @mkdir -p $(@D) @@ -39,13 +46,14 @@ assets/css/%.css: assets_src/css/%.less .npm_update @$(LESSC) $< > $@ assets_src/js/%.d: assets_src/js/%.js - @./bin/generate-dependencies $(patsubst assets_src/%, assets/%, $<) $< $@ + @$(BINARY_PATH)/generate-dependencies $(patsubst assets_src/%, assets/%, $<) $< $@ assets/js/%.js: assets_src/js/%.js assets_src/js/%.d .babelrc .npm_update @echo browserify $< -o $@ @$(BROWSERIFY) --debug $< -o $@ -t babelify clean: + -rm -- $(BINARY_PATH)/$(APP_NAME) -rm -- $(CSS_FILES) -rm -- $(LESS_FILES) -rm -- $(JS_FILES) @@ -59,11 +67,11 @@ superclean: clean ultraclean: superclean -rm -r -- node_modules -run: all +run: assets go run *.go importer: $(IMPORTER_FILES) go build -o importer tools/importer/*.go -.PHONY: all print_info clean superclean ultraclean run +.PHONY: all assets app print_info clean superclean ultraclean run -- cgit v0.10.1 From 8cb799463ed0117091f0f9b1abea5eb6722aaa59 Mon Sep 17 00:00:00 2001 From: jan Date: Tue, 11 Oct 2016 16:36:59 +0200 Subject: grilist binary ignorieren diff --git a/.gitignore b/.gitignore index 76e7090..e915543 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ assets/css/* #Binaries /importer +bin/grilist -- cgit v0.10.1 From 738b5027d664258ca8efbfe3d7eeea31d621535f Mon Sep 17 00:00:00 2001 From: jan Date: Tue, 11 Oct 2016 15:59:47 +0200 Subject: NAME variable verwenden diff --git a/Makefile b/Makefile index 9206ed2..12f5f53 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,6 @@ include config.mk BINARY_PATH=./bin -APP_NAME=grilist IMPORTER_FILES = $(shell find tools/importer/ -type f -name '*.go') CSS_FILES = $(patsubst assets_src/%, assets/%, $(shell find assets_src/css/ -type f -name '*.css')) @@ -23,7 +22,7 @@ print_info: @echo $(DEPFILES) app: - go build -o $(BINARY_PATH)/$(APP_NAME) + go build -o $(BINARY_PATH)/$(NAME) .npm_update: package.json touch $@ @@ -53,7 +52,7 @@ assets/js/%.js: assets_src/js/%.js assets_src/js/%.d .babelrc .npm_update @$(BROWSERIFY) --debug $< -o $@ -t babelify clean: - -rm -- $(BINARY_PATH)/$(APP_NAME) + -rm -- $(BINARY_PATH)/$(NAME) -rm -- $(CSS_FILES) -rm -- $(LESS_FILES) -rm -- $(JS_FILES) -- cgit v0.10.1 From f958866bbb20f824ea231fc31158412210fd62dd Mon Sep 17 00:00:00 2001 From: jan Date: Tue, 11 Oct 2016 16:13:16 +0200 Subject: gril view: die aehnlichen grils werden jetzt auf mobilgeraeten besser angezeigt diff --git a/views/pages/gril.html b/views/pages/gril.html index 69d7fcd..b2a00a6 100644 --- a/views/pages/gril.html +++ b/views/pages/gril.html @@ -78,7 +78,7 @@

Ähnliche Grils

{{ range .SimilarGrils }} -
+
{{ template "card_gril" . }}
{{ end }} -- cgit v0.10.1 From e2d40c07c10ba46c7b32bbfbbf00493f40fe3c56 Mon Sep 17 00:00:00 2001 From: jan Date: Tue, 11 Oct 2016 16:31:52 +0200 Subject: serienmodul hinzugefuegt, die datenbank muss aber erst mal gerichtet werden diff --git a/main.go b/main.go index 9231f7c..7ee05ad 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ import ( "fagott.pw/grilist/modules/grils" "fagott.pw/grilist/modules/lists" "fagott.pw/grilist/modules/search" + "fagott.pw/grilist/modules/series" "fagott.pw/grilist/modules/tags" "fagott.pw/grilist/modules/user" @@ -87,6 +88,7 @@ func main() { loadModule(tags.New()) loadModule(search.New()) loadModule(user.New()) + loadModule(series.New()) router.GET("/dashboard", viewDashboard) log.Fatal(http.ListenAndServe(":8080", nil)) diff --git a/modules/series/module.go b/modules/series/module.go new file mode 100644 index 0000000..295e16b --- /dev/null +++ b/modules/series/module.go @@ -0,0 +1,44 @@ +package series + +import ( + "net/http" + "strconv" + + "fagott.pw/charakterin" + "fagott.pw/grilist/grilist" + "github.com/julienschmidt/httprouter" +) + +type Module struct { + g *grilist.Grilist +} + +func New() *Module { + return &Module{} +} + +func (m *Module) Name() string { + return "Series" +} + +func (m *Module) Init(g *grilist.Grilist) { + m.g = g + + m.g.Router.GET("/series/:id", m.viewSeries) +} + +func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.DashboardCategory { + return make([]grilist.DashboardCategory, 0) +} + +func (m *Module) viewSeries(w http.ResponseWriter, r *http.Request, p httprouter.Params) { + _, _ = m.g.Charakterin.GetUserFromRequest(r) + + _, err := strconv.Atoi(p.ByName("id")) + if err != nil { + http.Redirect(w, r, "/", 302) + return + } + + http.Error(w, "not implemented", 500) +} -- cgit v0.10.1 From b5051d2358e12f877e5c52053dcf1a16959e4ded Mon Sep 17 00:00:00 2001 From: jan Date: Tue, 11 Oct 2016 17:44:12 +0200 Subject: irgendwie angefangen 'models' zu verwenden, wir muessen mal gucken wie wir das genau gestalten wollen diff --git a/models/gril.go b/models/gril.go new file mode 100644 index 0000000..f0b8f8f --- /dev/null +++ b/models/gril.go @@ -0,0 +1,61 @@ +package models + +import ( + "database/sql" + "fmt" + "strconv" + "strings" + "time" + + "fagott.pw/grilist/modules/series" + + "github.com/lib/pq" +) + +type DataSource int + +const ( + DataSourceACD DataSource = iota + DataSourceAniDB + DataSourceAnilist +) + +type Trait struct { + Name string + Value string + OfficialValue string +} + +type Gril struct { + ID int + KanjiName string + RomajiName string + OtherNames []string + Age sql.NullInt64 + Birthday pq.NullTime + Tags []string + Traits []Trait + ForeignIDs map[DataSource]int + UpdatedAt time.Time + Lists []int + Series []series.Series +} + +// Der kram hier sollte eigentlich auch eher wo anders hin als ins Model, oder?! +func (g *Gril) Slug() string { + if g.RomajiName == "" { + return strconv.Itoa(g.ID) + } + return fmt.Sprintf( + "%d/%s", + g.ID, + strings.Replace(g.RomajiName, " ", "", -1)) +} + +// Das hier sollte auch irgendwo anders hin. +func (g *Gril) ImagePath(useThumbnail bool) string { + if useThumbnail { + return fmt.Sprintf("http://img.grilist.moe/gril/thumb/%d.jpg", g.ID) + } + return fmt.Sprintf("http://img.grilist.moe/gril/full/%d.jpg", g.ID) +} diff --git a/modules/grils/gril.go b/modules/grils/gril.go deleted file mode 100644 index 7b54ab1..0000000 --- a/modules/grils/gril.go +++ /dev/null @@ -1,63 +0,0 @@ -package grils - -import ( - "database/sql" - "fmt" - "strconv" - "strings" - "time" - - "fagott.pw/grilist/modules/series" - - "github.com/lib/pq" -) - -type DataSource int - -const ( - DataSourceACD DataSource = iota - DataSourceAniDB - DataSourceAnilist -) - -type Trait struct { - Name string - Value string - OfficialValue string -} - -type Gril struct { - ID int - KanjiName string - RomajiName string - OtherNames []string - Age sql.NullInt64 - Birthday pq.NullTime - Tags []string - Traits []Trait - ForeignIDs map[DataSource]int - UpdatedAt time.Time - Lists []int - Series []series.Series -} - -func (g *Gril) Slug() string { - if g.RomajiName == "" { - return strconv.Itoa(g.ID) - } - return fmt.Sprintf( - "%d/%s", - g.ID, - strings.Replace(g.RomajiName, " ", "", -1)) -} - -func ImagePath(id int, useThumbnail bool) string { - if useThumbnail { - return fmt.Sprintf("http://img.grilist.moe/gril/thumb/%d.jpg", id) - } - return fmt.Sprintf("http://img.grilist.moe/gril/full/%d.jpg", id) -} - -func (g *Gril) ImagePath(prioritizeThumbnail bool) string { - return ImagePath(g.ID, prioritizeThumbnail) -} diff --git a/modules/grils/grils.go b/modules/grils/grils.go index 4913e11..1b1bb5f 100644 --- a/modules/grils/grils.go +++ b/modules/grils/grils.go @@ -13,15 +13,11 @@ import ( "fagott.pw/grilist/cache" "fagott.pw/grilist/frontend" "fagott.pw/grilist/grilist" + "fagott.pw/grilist/models" "github.com/julienschmidt/httprouter" ) -type CachedGril struct { - Created time.Time - Gril *Gril -} - var ( pgArrayReg = regexp.MustCompile(`(((?P(([^",\\{}\s(NULL)])+|"([^"\\]|\\"|\\\\)*")))(,)?)`) pgValueIdx int @@ -54,8 +50,8 @@ func (m *GrilsModule) Init(g *grilist.Grilist) { m.c = cache.New() } -func (m *GrilsModule) getGrils(whereClause string, params ...interface{}) ([]*Gril, error) { - var grils []*Gril +func (m *GrilsModule) getGrils(whereClause string, params ...interface{}) ([]*models.Gril, error) { + var grils []*models.Gril rows, err := m.g.DB.Query(fmt.Sprintf(`SELECT id, kanji_name, romaji_name, other_names, updated_at, age, birthday, tags FROM grilist.grils_flattened WHERE %s`, whereClause), params...) if err != nil { @@ -64,7 +60,7 @@ func (m *GrilsModule) getGrils(whereClause string, params ...interface{}) ([]*Gr defer rows.Close() for rows.Next() { - gril := &Gril{} + gril := &models.Gril{} var tags []byte var otherNames []byte err = rows.Scan(&gril.ID, &gril.KanjiName, &gril.RomajiName, &otherNames, &gril.UpdatedAt, &gril.Age, &gril.Birthday, &tags) @@ -83,7 +79,7 @@ func (m *GrilsModule) getGrils(whereClause string, params ...interface{}) ([]*Gr return grils, nil } -func (m *GrilsModule) GetListsOfGril(gril *Gril) error { +func (m *GrilsModule) GetListsOfGril(gril *models.Gril) error { rows, err := m.g.DB.Query(`SELECT list_id FROM grilist.lists_grils WHERE gril_id = $1`, gril.ID) if err != nil { return err @@ -120,7 +116,7 @@ func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.Das } for rows.Next() { - var g Gril + var g models.Gril if err := rows.Scan(&g.ID, &g.RomajiName, &g.KanjiName); err != nil { log.Println(err) continue @@ -143,12 +139,12 @@ func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.Das return categories } -func (m *GrilsModule) FromID(id int) (*Gril, error) { +func (m *GrilsModule) FromID(id int) (*models.Gril, error) { if g, ok := m.c.Get(id); ok { - return g.(*Gril), nil + return g.(*models.Gril), nil } - gril := &Gril{ + gril := &models.Gril{ ID: id, } t1 := time.Now() @@ -219,9 +215,9 @@ func (m *GrilsModule) FromID(id int) (*Gril, error) { return gril, nil } -func (m *GrilsModule) FromIDs(ids []int) ([]*Gril, error) { +func (m *GrilsModule) FromIDs(ids []int) ([]*models.Gril, error) { if len(ids) == 0 { - return make([]*Gril, 0), nil + return make([]*models.Gril, 0), nil } idList := "(" @@ -274,7 +270,7 @@ func (m *GrilsModule) viewGril(w http.ResponseWriter, r *http.Request, p httprou } defer rows.Close() - var similar []*Gril + var similar []*models.Gril for rows.Next() { var id int if err := rows.Scan(&id); err != nil { diff --git a/modules/lists/lists.go b/modules/lists/lists.go index d1913be..5934e9a 100644 --- a/modules/lists/lists.go +++ b/modules/lists/lists.go @@ -16,6 +16,7 @@ import ( "fagott.pw/grilist/cache" "fagott.pw/grilist/frontend" "fagott.pw/grilist/grilist" + "fagott.pw/grilist/models" "fagott.pw/grilist/modules/grils" "github.com/julienschmidt/httprouter" "github.com/lib/pq" @@ -41,7 +42,7 @@ type List struct { // ListGril ist ein geranktes Gril type ListGril struct { - Gril *grils.Gril + Gril *models.Gril Order int } diff --git a/modules/search/search.go b/modules/search/search.go index a614c08..4048dbe 100644 --- a/modules/search/search.go +++ b/modules/search/search.go @@ -1,6 +1,7 @@ package search import ( + "fmt" "log" "net/http" @@ -72,7 +73,9 @@ func (m *Module) instantSearchGril(w http.ResponseWriter, r *http.Request, p htt continue } - result.ImagePath = grils.ImagePath(result.ID, true) + // Jan: Das ist hier irgendwie scheisse, aber wir bauen ja grad irgendwie den kram auf Models um. Wir brauchen da + // irgendwas besseres. + result.ImagePath = fmt.Sprintf("http://img.grilist.moe/gril/thumb/%d.jpg", result.ID) results = append(results, result) } -- cgit v0.10.1