Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Two simple method:</p> <p><strong>1.global database session</strong></p> <pre><code>package main import ( "net/http" "log" "fmt" "./backend" ) var mongoConn * backend.MongoDBConn func AddHandler(w http.ResponseWriter, r *http.Request) { log.Printf("serving %v %v", r.Method, r.URL.Path[1:]) if r.Method != "POST" { fmt.Fprintln(w, "Not POST Method ") return } title := r.FormValue("title") description := r.FormValue("description") fmt.Fprintf(w, " title description %v %v", title, description) //I can't call mongoConn.AddToDo(title, description) from here mongoConn.AddToDo(title, description) } const AddForm = ` &lt;html&gt;&lt;body&gt; &lt;form method="POST" action="/add"&gt; Name: &lt;input type="text" name="title"&gt; Age: &lt;input type="text" name="description"&gt; &lt;input type="submit" value="Add"&gt; &lt;/form&gt; &lt;/body&gt;&lt;/html&gt; ` func Index(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, AddForm) } func main() { //connect to mongoDB mongoConn = backend.NewMongoDBConn() _ = mongoConn.Connect("localhost") defer mongoConn.Stop() http.HandleFunc("/", Index) http.HandleFunc("/add", AddHandler) log.Println("Start Server:") err := http.ListenAndServe(":8080", nil) if err != nil { log.Fatal("ListenAndServe:", err) } } </code></pre> <p><strong>2.a new db connection on every request</strong></p> <pre><code>import ( "./backend" //other boilerplate imports ) func AddHandler(writer http.ResponseWriter, request *http.Request) { log.Printf("serving %v %v", request.Method, request.URL.Path[1:]) if request.Method != "POST" { serve404(writer) return } title := request.FormValue("title") description := request.FormValue("description") fmt.Fprintf(writer, " title description %v %v", title, description) //................ mongoConn := backend.NewMongoDBConn() _ = mongoConn.Connect("localhost") mongoConn.AddToDo(title, description) //.................... mongoConn.Stop() } ...... </code></pre> <p><strong>a better solution:</strong></p> <blockquote> <p>You could create a pool of db sessions, then before processing the request you pick one and put in the context of that request. Then after the request is done you push the connection back to the pool.</p> <p>If the pool is empty you create a new connection If the pool is full you close the connection</p> </blockquote> <p>For more information, click <a href="https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/gjvK4zcYy6U" rel="nofollow">here</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload