Blame view
server.go
1.36 KB
2726b5683 server |
1 2 3 4 5 6 |
package webutility import ( "database/sql" "fmt" "net/http" |
28a6cc60c Integrated logger... |
7 |
"git.to-net.rs/marko.tikvic/webutility/logger" |
2726b5683 server |
8 9 10 11 |
"github.com/gorilla/mux" ) type Server struct { |
0c71c5487 minor changes |
12 13 |
DB *sql.DB Router *mux.Router |
28a6cc60c Integrated logger... |
14 |
Logger *logger.Logger |
0c71c5487 minor changes |
15 16 17 |
Port string DBs map[string]*sql.DB dsn map[string]string |
2726b5683 server |
18 |
} |
0c71c5487 minor changes |
19 |
func NewODBCServer(dsn, port, logDir string) (s *Server, err error) { |
2726b5683 server |
20 21 22 23 24 25 26 27 28 |
s = new(Server) s.Port = port if s.DB, err = sql.Open("odbc", fmt.Sprintf("DSN=%s;", dsn)); err != nil { return nil, err } s.Router = mux.NewRouter() |
28a6cc60c Integrated logger... |
29 |
if s.Logger, err = logger.New("err", logDir, logger.MaxLogSize1MB); err != nil { |
2726b5683 server |
30 31 |
return nil, fmt.Errorf("can't create logger: %s", err.Error()) } |
6a5c6931e added database ma... |
32 33 |
s.DBs = make(map[string]*sql.DB) s.DBs["default"] = s.DB |
0c71c5487 minor changes |
34 |
s.dsn = make(map[string]string) |
62a0d7a8a minor changes |
35 |
s.dsn["default"] = dsn |
0c71c5487 minor changes |
36 |
|
2726b5683 server |
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
return s, nil } func (s *Server) Run() { s.Logger.Print("Server listening on %s", s.Port) s.Logger.PrintAndTrace(http.ListenAndServe(s.Port, s.Router).Error()) } func (s *Server) Cleanup() { if s.DB != nil { s.DB.Close() } if s.Logger != nil { s.Logger.Close() } } func (s *Server) StartTransaction() (*sql.Tx, error) { return s.DB.Begin() } func CommitChanges(tx *sql.Tx, err *error, opt ...error) { if *err != nil { tx.Rollback() return } for _, e := range opt { if e != nil { tx.Rollback() return } } if *err = tx.Commit(); *err != nil { tx.Rollback() } } |