Add: Logger

This commit is contained in:
Óscar M. Lage 2022-03-28 21:11:10 +02:00
parent f1aef623b6
commit bbb79f42c9
6 changed files with 42 additions and 14 deletions

View File

@ -3,6 +3,8 @@
## General ## General
> General options > General options
- [ ] Flag to select store - [ ] Flag to select store
- [x] Flag that sets the debug mode
- [ ] Flag to show the version
- [ ] Config file (to select store and some other future options) - [ ] Config file (to select store and some other future options)
- [ ] Proper logger - [ ] Proper logger
- [ ] Makefile helper - [ ] Makefile helper

10
main.go
View File

@ -1,12 +1,22 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"github.com/oscarmlage/wikingo/server" "github.com/oscarmlage/wikingo/server"
) )
func main() { func main() {
fmt.Println("Starting wikingo...") fmt.Println("Starting wikingo...")
flag_debug := flag.Bool("d", false, "Enable debug mode")
flag.Parse()
// Logger
server.InitLogger(*flag_debug)
server.Debug.Printf("Yo there")
fmt.Println("Starting wikingo...")
fmt.Printf("Debug mode: %t\n", *flag_debug)
server.Serve() server.Serve()
} }

View File

@ -1,8 +1,6 @@
package model package model
import ( import (
"fmt"
"gorm.io/driver/sqlite" "gorm.io/driver/sqlite"
"gorm.io/gorm" "gorm.io/gorm"
) )
@ -19,7 +17,6 @@ func (s *StoreGorm) Open() error {
if err != nil { if err != nil {
return err return err
} }
fmt.Printf("%s\n", s.db.Name())
// Migrate the Schema // Migrate the Schema
s.db.AutoMigrate(&Page{}) s.db.AutoMigrate(&Page{})
@ -36,7 +33,6 @@ func (s *StoreGorm) AddPage(name string, body string) error {
if tx.Error != nil { if tx.Error != nil {
return tx.Error return tx.Error
} }
fmt.Println("Page created")
return nil return nil
} }
@ -55,7 +51,6 @@ func (s *StoreGorm) GetPage(name string) (Page, error) {
if tx.Error != nil { if tx.Error != nil {
return Page{}, tx.Error return Page{}, tx.Error
} }
fmt.Println(page)
return page, nil return page, nil
} }
@ -65,7 +60,6 @@ func (s *StoreGorm) GetPageVersion(name string, version string) (Page, error) {
if tx.Error != nil { if tx.Error != nil {
return Page{}, tx.Error return Page{}, tx.Error
} }
fmt.Println(page)
return page, nil return page, nil
} }

View File

@ -1,7 +1,6 @@
package server package server
import ( import (
"fmt"
"log" "log"
"net/http" "net/http"
"strings" "strings"
@ -23,12 +22,12 @@ func WikiPage(c echo.Context) error {
page_id := c.Param("page") page_id := c.Param("page")
version := c.Param("version") version := c.Param("version")
if version != "" { if version != "" {
fmt.Printf("version: %s", version) Debug.Printf("version: %s", version)
page, err = store.GetPageVersion(page_id, version) page, err = store.GetPageVersion(page_id, version)
if err != nil { if err != nil {
// If record not found, show 404 // If record not found, show 404
if err.Error() == "record not found" { if err.Error() == "record not found" {
return c.Render(http.StatusOK, "notfound.html", map[string]interface{}{ Debug.Printf("Page not found: %s/%s", page_id, version)
return c.Render(http.StatusNotFound, "404.html", map[string]interface{}{ return c.Render(http.StatusNotFound, "404.html", map[string]interface{}{
"name": page_id, "name": page_id,
}) })
@ -40,13 +39,14 @@ func WikiPage(c echo.Context) error {
if err != nil { if err != nil {
// If record not found, show the create // If record not found, show the create
if err.Error() == "record not found" { if err.Error() == "record not found" {
Debug.Printf("Page not found: %s", page_id)
return c.Render(http.StatusOK, "notfound.html", map[string]interface{}{ return c.Render(http.StatusOK, "notfound.html", map[string]interface{}{
"name": page_id, "name": page_id,
}) })
} }
} }
} }
fmt.Printf("res: %T\n", page) Debug.Printf("res: %T\n", page)
body := strings.ReplaceAll(page.Body, "\r\n", "\n") body := strings.ReplaceAll(page.Body, "\r\n", "\n")
unsafe := blackfriday.Run([]byte(body)) unsafe := blackfriday.Run([]byte(body))
html := bluemonday.UGCPolicy().SanitizeBytes(unsafe) html := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
@ -62,10 +62,10 @@ func WikiPageEdit(c echo.Context) error {
page_id := c.Param("page") page_id := c.Param("page")
version := c.Param("version") version := c.Param("version")
if version != "" { if version != "" {
fmt.Printf("version; %s\n", version) Debug.Printf("version; %s\n", version)
page, err = store.GetPageVersion(page_id, version) page, err = store.GetPageVersion(page_id, version)
} else { } else {
fmt.Println("No version") Debug.Println("No version")
page, err = store.GetPage(page_id) page, err = store.GetPage(page_id)
} }
if err != nil { if err != nil {
@ -75,13 +75,14 @@ func WikiPageEdit(c echo.Context) error {
if err != nil { if err != nil {
log.Panicln(err.Error()) log.Panicln(err.Error())
} }
Debug.Printf("Page %s created", page_id)
page, err = store.GetPage(page_id) page, err = store.GetPage(page_id)
if err != nil { if err != nil {
log.Panicln(err) log.Panicln(err)
} }
} }
} }
fmt.Printf("res: %T\n", page) Debug.Printf("res: %T\n", page)
return c.Render(http.StatusOK, "edit.html", map[string]interface{}{ return c.Render(http.StatusOK, "edit.html", map[string]interface{}{
"page": page, "page": page,
}) })
@ -104,7 +105,7 @@ func WikiList(c echo.Context) error {
if err != nil { if err != nil {
log.Panicln(err) log.Panicln(err)
} }
fmt.Printf("res: %T\n", pages) Debug.Printf("res: %T\n", pages)
return c.Render(http.StatusOK, "list.html", map[string]interface{}{ return c.Render(http.StatusOK, "list.html", map[string]interface{}{
"pages": pages, "pages": pages,
}) })

20
server/log.go Normal file
View File

@ -0,0 +1,20 @@
package server
import (
"io"
"log"
"os"
)
var (
Debug *log.Logger
)
func InitLogger(debug bool) {
// Discard policy by default
Debug = log.New(io.Discard, "", 0)
// Enable it only if debug flag was passed
if debug {
Debug = log.New(os.Stderr, "DEBUG: ", log.Ltime|log.Lshortfile)
}
}

View File

@ -40,6 +40,7 @@ func Serve() {
if err != nil { if err != nil {
log.Panicln(err) log.Panicln(err)
} }
Debug.Printf("test.db open\n")
// Echo instance // Echo instance
e := echo.New() e := echo.New()