@ -31,9 +31,20 @@ func main() {
@@ -31,9 +31,20 @@ func main() {
e . Renderer = & Template { templates : template . Must ( template . ParseGlob ( "assets/templates/*.html" ) ) }
e . Static ( "/" , "assets/public" )
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 . File ( "/favicon.ico" , "assets/public/favicon.ico" )
e . File ( "/robots.txt" , "assets/public/robots.txt" )
e . File ( "/style.css" , "assets/public/style.css" )
e . File ( "/index.html" , "assets/public/index.html" )
e . File ( "/" , "assets/public/index.html" )
e . GET ( "/TOS.md" , func ( c echo . Context ) error {
n , code := md2html ( c , "TOS" )
return c . Render ( code , "Page" , n )
} )
e . GET ( "/:id" , func ( c echo . Context ) error {
n , code := note ( c , db )
return c . Render ( code , "Note" , n )
} )
e . Logger . Fatal ( e . Start ( ":3000" ) )
}
@ -45,11 +56,12 @@ type Note struct {
@@ -45,11 +56,12 @@ type Note struct {
Content template . HTML
}
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 = ?" )
func note ( c echo . Context , db * sql . DB ) ( Note , int ) {
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 { }
return note503 , http . StatusServiceUnavailable
}
defer stmt . Close ( )
row := stmt . QueryRow ( c . Param ( "id" ) )
@ -57,22 +69,28 @@ func note(c echo.Context, db *sql.DB) Note {
@@ -57,22 +69,28 @@ func note(c echo.Context, db *sql.DB) Note {
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
return note404 , http . StatusNotFound
}
// 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 ) ) ) ) ,
}
Content : mdTmplHTML ( [ ] byte ( text ) ) ,
} , http . StatusOK
}
func md2html ( c echo . Context , name string ) Note {
func md2html ( c echo . Context , name string ) ( Note , int ) {
path := "assets/markdown/" + name + ".md"
mdContent , err := ioutil . ReadFile ( path )
if err != nil {
c . Logger ( ) . Errorf ( "couldn't open markdown page %q: %v" , path , err )
return Note { }
return note503 , http . StatusServiceUnavailable
}
return Note { Title : name , Content : template . HTML ( string ( blackfriday . Run ( mdContent ) ) ) }
return Note { Title : name , Content : mdTmplHTML ( mdContent ) } , http . StatusOK
}
func mdTmplHTML ( content [ ] byte ) template . HTML { return template . HTML ( string ( blackfriday . Run ( content ) ) ) }
// error notes
var note404 = Note { Title : "Not found" , Content : mdTmplHTML ( [ ] byte ( "# 404 NOT FOUND" ) ) }
var note503 = Note { Title : "Service unavailable" , Content : mdTmplHTML ( [ ] byte ( "# 503 SERVICE UNAVAILABLE" ) ) }