@ -5,6 +5,11 @@ import (
"io"
"io"
"io/ioutil"
"io/ioutil"
"net/http"
"net/http"
"time"
"database/sql"
_ "github.com/mattn/go-sqlite3"
"github.com/labstack/echo"
"github.com/labstack/echo"
"github.com/russross/blackfriday"
"github.com/russross/blackfriday"
@ -18,23 +23,56 @@ func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Con
func main ( ) {
func main ( ) {
e := echo . New ( )
e := echo . New ( )
db , err := sql . Open ( "sqlite3" , "./database.sqlite" )
if err != nil {
e . Logger . Error ( err )
}
defer db . Close ( )
e . Renderer = & Template { templates : template . Must ( template . ParseGlob ( "assets/templates/*.html" ) ) }
e . Renderer = & Template { templates : template . Must ( template . ParseGlob ( "assets/templates/*.html" ) ) }
e . Static ( "/" , "assets/public" )
e . Static ( "/" , "assets/public" )
e . GET ( "/TOS.md" , func ( c echo . Context ) error { return c . Render ( http . StatusOK , "Page" , md2html ( c , "TOS" ) ) } )
e . GET ( "/TOS.md" , func ( c echo . Context ) error { return c . Render ( http . StatusOK , "Page" , md2html ( c , "TOS" ) ) } )
e . GET ( "/:id" , func ( c echo . Context ) error { return c . Render ( http . StatusOK , "Note" , note ( c , db ) ) } )
e . Logger . Fatal ( e . Start ( ":3000" ) )
e . Logger . Fatal ( e . Start ( ":3000" ) )
}
}
type Note struct {
type Note struct {
ID , Title string
ID , Title , Text string
Published , Edited time . Time
Views int
Content template . HTML
Content template . HTML
}
}
func md2html ( c echo . Context , name string ) * Note {
func note ( c echo . Context , db * sql . DB ) Note {
stmt , err := db . Prepare ( "select id, text, strftime('%s', published) as published, strftime('%s',edited) as edited, password, views from notes where id = ?" )
if err != nil {
c . Logger ( ) . Error ( err )
return Note { }
}
defer stmt . Close ( )
row := stmt . QueryRow ( c . Param ( "id" ) )
var id , text , password , published , edited string
var views int
if err := row . Scan ( & id , & text , & published , & edited , & password , & views ) ; err != nil {
c . Logger ( ) . Error ( err )
return Note { } // TODO: use predefined error notes
}
// cand := regexp.MustCompile("[\n\r]").Split(text, 1)
// fmt.Println("CANDIDATE", cand[0])
return Note {
ID : id ,
Content : template . HTML ( string ( blackfriday . Run ( [ ] byte ( text ) ) ) ) ,
}
}
func md2html ( c echo . Context , name string ) Note {
path := "assets/markdown/" + name + ".md"
path := "assets/markdown/" + name + ".md"
mdContent , err := ioutil . ReadFile ( path )
mdContent , err := ioutil . ReadFile ( path )
if err != nil {
if err != nil {
c . Logger ( ) . Errorf ( "couldn't open markdown page %q: %v" , path , err )
c . Logger ( ) . Errorf ( "couldn't open markdown page %q: %v" , path , err )
return nil
return Note { }
}
}
return & Note { Content : template . HTML ( string ( blackfriday . Run ( mdContent ) ) ) }
return Note { Title : name , Content : template . HTML ( string ( blackfriday . Run ( mdContent ) ) ) }
}
}