Skip to content

Instantly share code, notes, and snippets.

@h3rald
Created October 23, 2011 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h3rald/1307581 to your computer and use it in GitHub Desktop.
Save h3rald/1307581 to your computer and use it in GitHub Desktop.
LiteStore.go
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))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment