Browse Source

sqlite access added

master
Christian Müller 8 years ago
parent
commit
54f1c1d460
  1. 1
      .gitignore
  2. 14
      Gopkg.lock
  3. 4
      Gopkg.toml
  4. 2
      assets/templates/note.html
  5. 46
      server.go

1
.gitignore vendored

@ -2,7 +2,6 @@ dump.rdb @@ -2,7 +2,6 @@ dump.rdb
bin/
node_modules/
npm-debug.log
database.sqlite
database.sqlite-journal
.DS_Store
vendor

14
Gopkg.lock generated

@ -25,6 +25,12 @@ @@ -25,6 +25,12 @@
revision = "fc9e8d8ef48496124e79ae0df75490096eccf6fe"
version = "v0.0.2"
[[projects]]
name = "github.com/mattn/go-sqlite3"
packages = ["."]
revision = "ca5e3819723d8eeaf170ad510e7da1d6d2e94a08"
version = "v1.2.0"
[[projects]]
name = "github.com/russross/blackfriday"
packages = ["."]
@ -55,6 +61,12 @@ @@ -55,6 +61,12 @@
packages = ["acme","acme/autocert"]
revision = "81e90905daefcd6fd217b62423c0908922eadb30"
[[projects]]
branch = "master"
name = "golang.org/x/net"
packages = ["context"]
revision = "66aacef3dd8a676686c7ae3716979581e8b03c47"
[[projects]]
branch = "master"
name = "golang.org/x/sys"
@ -64,6 +76,6 @@ @@ -64,6 +76,6 @@
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "a39fe24a6fa292ee2a7c5dabd94c32271f0c0cc16d928c8f1113600c76fb6d1f"
inputs-digest = "6cae41086ddec63b527c56e9e182bb69c9e1b50afa8d32f031516cd7b0d2e0e6"
solver-name = "gps-cdcl"
solver-version = 1

4
Gopkg.toml

@ -5,3 +5,7 @@ @@ -5,3 +5,7 @@
[[constraint]]
name = "github.com/russross/blackfriday"
version = "2.0.0"
[[constraint]]
name = "github.com/mattn/go-sqlite3"
version = "1.2.0"

2
assets/templates/note.html

@ -1,3 +1,4 @@ @@ -1,3 +1,4 @@
{{define "Note"}}
<!DOCTYPE html>
<html>
<head>
@ -19,3 +20,4 @@ @@ -19,3 +20,4 @@
</footer>
</body>
</html>
{{end}}

46
server.go

@ -5,6 +5,11 @@ import ( @@ -5,6 +5,11 @@ import (
"io"
"io/ioutil"
"net/http"
"time"
"database/sql"
_ "github.com/mattn/go-sqlite3"
"github.com/labstack/echo"
"github.com/russross/blackfriday"
@ -18,23 +23,56 @@ func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Con @@ -18,23 +23,56 @@ func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Con
func main() {
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.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.Logger.Fatal(e.Start(":3000"))
}
type Note struct {
ID, Title string
ID, Title, Text string
Published, Edited time.Time
Views int
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"
mdContent, err := ioutil.ReadFile(path)
if err != nil {
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)))}
}

Loading…
Cancel
Save