Created
December 17, 2015 20:42
-
-
Save mpfund/bd4ab0c03d2d1cf611b8 to your computer and use it in GitHub Desktop.
gin web server + db
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/HouzuoGuo/tiedot/db" | |
"github.com/gin-gonic/gin" | |
) | |
func main() { | |
gin.SetMode(gin.ReleaseMode) | |
r := gin.Default() | |
mydb, err := db.OpenDB("test.db") | |
checkerr(err) | |
cols := mydb.AllCols() | |
if !contains(cols, "jobs") { | |
err = mydb.Create("jobs") | |
checkerr(err) | |
} | |
col := mydb.Use("jobs") | |
_, err = col.Insert(map[string]interface{}{ | |
"A": 3, | |
"B": 4, | |
}) | |
checkerr(err) | |
col.Index([]string{"A"}) | |
//r.Static("/assets", "./files") | |
//r.LoadHTMLGlob("templates/*") | |
r.GET("/ping", func(c *gin.Context) { | |
query:= []interface{}{ | |
map[string]interface{}{ | |
"eq":"3", | |
"in":[]interface{}{"A"}, | |
}, | |
} | |
queryResult := make(map[int]struct{}) | |
err :=db.EvalQuery(query,col,&queryResult) | |
if err !=nil{ | |
c.String(200,err.Error()) | |
}else{ | |
c.String(200,fmt.Sprintf("%#v",queryResult)) | |
} | |
}) | |
r.GET("/", func(c *gin.Context) { | |
c.HTML(200, "index.tmpl", gin.H{ | |
"title": "Main website", | |
}) | |
}) | |
r.Run(":8080") | |
} | |
func contains(strs []string, str string) bool { | |
for _, v := range strs { | |
if v == str { | |
return true | |
} | |
} | |
return false | |
} | |
func checkerr(err error) { | |
if err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment