Skip to content

Instantly share code, notes, and snippets.

@9yx
Created April 7, 2012 17:52
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 9yx/2330858 to your computer and use it in GitHub Desktop.
Save 9yx/2330858 to your computer and use it in GitHub Desktop.
// webtest project main.go
package main
import (
//web "webhelp"
bson "launchpad.net/mgo/bson"
mgo "launchpad.net/mgo"
"net/http"
"time"
"fmt"
)
var (
clt *http.Client
fullsession *mgo.Session
readsession *mgo.Session
fullDatabase *mgo.Database
readDatabase *mgo.Database
)
func check(err error) {
if err != nil {
panic(err)
}
}
func ConnectDB() {
var err error
fullsession, err = mgo.Dial("127.0.0.1")
check(err)
fullsession.SetSafe(&mgo.Safe{})
fullsession.SetMode(mgo.Monotonic, true)
fullDatabase = fullsession.DB("projectweb")
err = fullDatabase.Login("full", "Thaij")
check(err)
}
func CloseDB() {
fullDatabase.Logout()
fullsession.Close()
}
type Commentaries struct {
Nick bson.ObjectId
Date time.Time
Text string
}
type Article struct {
Nick string
Text string
CatText string
Title string
Date time.Time
LastEdit time.Time
Id bson.ObjectId "_id,omitempty"
Roles []string
Comm *Commentaries
}
func NewArticle(nick string, text string, cattext string, title string, Roles []string) {
c := fullDatabase.C("article")
a := &Article{nick, text, cattext, title, bson.Now(), bson.Now(), bson.NewObjectId(), Roles, new(Commentaries)}
fmt.Println(a.Id)
err := c.Insert(a)
fmt.Println("----------------------")
fmt.Println("Error:",err)
fmt.Println("----------------------")
check(err)
}
func CreateWebServer() {
http.HandleFunc("/auth", serveAuth)
check(http.ListenAndServe(":8080", nil))
}
func serveAuth(w http.ResponseWriter, r *http.Request) {
NewArticle("test", "test", "test","test", []string{"Test"})
}
func main() {
//web.ConnectDB()
//web.InitTemplate()
//web.CreateWebServer()
//web.CloseDB()
ConnectDB()
CreateWebServer()
CloseDB()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment