public
Created

LiteStore.go

  • Download Gist
litestore.go
Go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
package main
 
import (
"fmt"
"flag"
"crypto/sha1"
"time"
"strconv"
"io/ioutil"
sqlite3 "github.com/kuroneko/gosqlite3"
"goweb"
"json"
"http"
//"rand"
)
 
const (
LS_PORT = 8008
LS_FILE = "data.db"
LS_VERSION = "0.1"
LS_NAME = "LiteStore"
LS_AUTHOR = "Fabio Cevasco"
LS_YEAR = "2011"
)
 
// Flags
var port *int = flag.Int("p", LS_PORT, "Port number")
var version *bool = flag.Bool("v", false, "Display the current version")
var file *string = flag.String("f", LS_FILE, "Database file")
 
// Database Tables
var table_documents = sqlite3.Table{ "documents", "key TEXT PRIMARY KEY, document TEXT" }
var table_tags = sqlite3.Table{ "tags", "tag TEXT, key TEXT, UNIQUE(tag, key)" }
 
func generateSHA1(s string) string{
h := sha1.New()
h.Write([]byte(s+timestamp()))
return string(h.Sum())
}
 
func timestamp() string {
return strconv.Itoa64(time.LocalTime().Seconds())
}
 
func initDb(db *sqlite3.Database) {
table_documents.Create(db)
table_tags.Create(db)
}
 
func validRequest(context *goweb.Context, content []uint8) bool {
if context.Request.Header["Content-Type"][0] != "application/json" {
context.RespondWithErrorMessage("Content-Type is not set to application/json", http.StatusUnsupportedMediaType)
return false
}
if len(content) == 0 {
context.RespondWithErrorMessage("No Content", http.StatusNoContent)
return false
}
return true
}
 
 
/////
 
// RestController for /documents
type DocumentsController struct {}
 
// Handler for POST /documents - create new document
func (controller *DocumentsController) Create(context *goweb.Context) {
req := context.Request
content, err := ioutil.ReadAll(req.Body)
if err != nil {
context.RespondWithErrorMessage("Bad Request", http.StatusBadRequest)
return
}
if !validRequest(context, content) { return }
 
var document interface{}
json.Unmarshal([]byte(content), &document)
 
if document != nil {
context.RespondWithData(document)
} else {
context.RespondWithErrorMessage("Invalid JSON data supplied", http.StatusBadRequest)
}
}
 
// Handler for DELETE /documents/{id} - Delete specific document
func (controller *DocumentsController) Delete(id string, context *goweb.Context) {
context.RespondWithNotImplemented()
}
 
// Handler for DELETE /documents - Delete many documents
func (controller *DocumentsController) DeleteMany(context *goweb.Context) {
context.RespondWithNotImplemented()
}
 
// Handler for GET /documents/{id} - Get specific document
func (controller *DocumentsController) Read(id string, context *goweb.Context) {
context.RespondWithNotImplemented()
}
 
// Handler for GET /documents - Get many documents
func (controller *DocumentsController) ReadMany(context *goweb.Context) {
context.RespondWithNotImplemented()
}
 
// Handler for PUT /documents/{id} - Update single document
func (controller *DocumentsController) Update(id string, context *goweb.Context) {
context.RespondWithNotImplemented()
}
 
// Handler for PUT /documents - Update entire collection
func (controller *DocumentsController) UpdateMany(context *goweb.Context) {
context.RespondWithNotImplemented()
}
 
/////
 
func main() {
flag.Parse()
 
if *version {
fmt.Printf("%v v%v - (c) %v %v\n", LS_NAME, LS_VERSION, LS_YEAR, LS_AUTHOR)
return
}
 
 
 
goweb.MapRest("/documents", new(DocumentsController))
fmt.Printf("Initializing database...\n")
sqlite3.Session(*file, func(db *sqlite3.Database){
initDb(db)
})
 
fmt.Printf("Starting %v Server v%v on port %v...\nPress CTRL+C to exit.\n\n", LS_NAME, LS_VERSION, *port)
goweb.ListenAndServe(":"+strconv.Itoa(*port))
 
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.