Last active
August 29, 2015 14:01
-
-
Save yosssi/a45d8eab1ecd2e2a0f94 to your computer and use it in GitHub Desktop.
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 ( | |
"encoding/json" | |
"fmt" | |
//"github.com/codegangsta/martini-contrib/render" | |
"log" | |
"net/http" | |
"os" | |
"github.com/go-martini/martini" | |
"github.com/robfig/config" | |
"github.com/ziutek/mymysql/mysql" | |
"github.com/ziutek/mymysql/native" | |
//"strings" | |
) | |
var ( | |
INFOLOG *log.Logger | |
WARNINGLOG *log.Logger | |
ERRORLOG *log.Logger | |
REQUESTACCESSLOG *log.Logger | |
REQUESTERRORLOG *log.Logger | |
// Config File vars | |
) | |
var port, logsd string = "", "" | |
var dbuser, dbpass, dbname, dbaddress string = "", "", "", "" | |
func ConfigurationInit() { | |
c, _ := config.ReadDefault("appconfig.cfg") // reading from config file | |
port, _ = c.String("default", "port") | |
logsd, _ = c.String("logs", "directory") | |
dbuser, _ = c.String("database", "dbuser") | |
dbpass, _ = c.String("database", "dbpass") | |
dbname, _ = c.String("database", "dbname") | |
dbaddress, _ = c.String("database", "address") | |
} | |
func LogInit() { | |
appfile, err := os.OpenFile(logsd+"app.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) | |
if err != nil { | |
log.Fatalln("Failed to open logs directory: ", err) | |
} | |
req_access_file, _ := os.OpenFile(logsd+"request-access.log", os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666) | |
req_error_file, _ := os.OpenFile(logsd+"request-error.log", os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666) | |
INFOLOG = log.New(appfile, | |
"INFO: ", | |
log.Ldate|log.Ltime|log.Lshortfile) | |
WARNINGLOG = log.New(appfile, | |
"WARNING: ", | |
log.Ldate|log.Ltime|log.Lshortfile) | |
ERRORLOG = log.New(appfile, | |
"ERROR: ", | |
log.Ldate|log.Ltime|log.Lshortfile) | |
REQUESTACCESSLOG = log.New(req_access_file, | |
"ACCESS: ", | |
log.Ldate|log.Ltime|log.Lshortfile) | |
REQUESTERRORLOG = log.New(req_error_file, | |
"ERROR: ", | |
log.Ldate|log.Ltime|log.Lshortfile) | |
} | |
//func (e err) { | |
// if e != nil { | |
// panic(e) | |
// } | |
//} | |
type Response map[string]interface{} | |
func (r Response) String() (s string) { | |
b, err := json.Marshal(r) | |
if err != nil { | |
s = "" | |
return | |
} | |
s = string(b) | |
return | |
} | |
func DB() martini.Handler { | |
fmt.Printf("DB") | |
fmt.Println("\n", dbaddress, dbuser, dbpass, dbname) | |
db := mysql.New("tcp", "", dbaddress, dbuser, dbpass, dbname) | |
err := db.Connect() | |
if err != nil { | |
panic(err) | |
} | |
defer db.Close() | |
//session, err := mgo.Dial("mongodb://localhost") | |
return func(c martini.Context) { | |
s := db.Clone() | |
if err := s.Connect(); err != nil { | |
panic(err) | |
} | |
defer s.Close() | |
c.Map(s) //.Use(dbname)) | |
c.Next() | |
} | |
} | |
func main() { | |
ConfigurationInit() | |
LogInit() | |
//db := &MyDatabase{ mysql.New("tcp", "", dbaddress, dbuser, dbpass, dbname) } | |
m := martini.Classic() | |
//m.Use(render.Renderer()) | |
//m.Map(db) | |
m.Use(DB()) | |
m.Get("/login", func(rw http.ResponseWriter, req *http.Request, db *native.Conn) { | |
fmt.Printf("Started Login") | |
err := db.Ping() //db.Ping()//&db.Ping() | |
if err != nil { | |
panic(err.Error()) | |
} | |
//rw.Header().Set("Content-Type", "application/json") | |
//fmt.Fprint(rw, Response{"code": errorCode, "message": userId}) | |
INFOLOG.Printf("Finished Login") | |
return | |
}) | |
INFOLOG.Println("Application is running...\r\n") | |
m.Run() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment