Skip to content

Commit

Permalink
Merge pull request #2 from DamienFontaine/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
DamienFontaine committed May 3, 2016
2 parents 325f00c + 58f5495 commit 68dcc35
Show file tree
Hide file tree
Showing 328 changed files with 140,907 additions and 356 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
*.coverprofile
testdata/logs/*.log
testdata/lunarc.log
testdata/access.log
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: go
sudo: false
go:
- 1.5
- 1.6
services:
- mongodb
before_install:
Expand All @@ -14,7 +14,7 @@ before_script:
script:
- go test -coverprofile=config.coverprofile ./config
- go test -coverprofile=controllers.coverprofile ./controllers
- go test -coverprofile=datasource.coverprofile ./datasource
- go test -tags=integration -coverprofile=datasource.coverprofile ./datasource
- go test -coverprofile=models.coverprofile ./models
- go test -tags=integration -coverprofile=services.coverprofile ./services
- go test -coverprofile=utils.coverprofile ./utils
Expand Down
72 changes: 72 additions & 0 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Godeps/Readme

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 69 additions & 0 deletions LoggingServeMux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) - Damien Fontaine <damien.fontaine@lineolia.net>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>

package lunarc

import (
"net/http"
"os"

"github.com/DamienFontaine/lunarc/config"

"github.com/Sirupsen/logrus"
)

const aFilename = "access.log"

// LoggingServeMux logs HTTP requests
type LoggingServeMux struct {
serveMux *http.ServeMux
conf config.Server
}

// NewLoggingServeMux allocates and returns a new LoggingServeMux
func NewLoggingServeMux(conf config.Server) *LoggingServeMux {
serveMux := http.NewServeMux()
return &LoggingServeMux{serveMux, conf}
}

// Handler sastisfy interface
func (mux *LoggingServeMux) Handler(r *http.Request) (h http.Handler, pattern string) {
return mux.serveMux.Handler(r)
}

//ServeHTTP
func (mux *LoggingServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
mux.serveMux.ServeHTTP(w, r)
}

//Handle register handler
func (mux *LoggingServeMux) Handle(pattern string, handler http.Handler) {

var log = logrus.New()

logFile, err := os.OpenFile(mux.conf.Log.File+aFilename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Out = os.Stderr
log.Warningf("Can't open logfile: %v", err)
} else {
log.Out = logFile
}
mux.serveMux.Handle(pattern, Logging(handler, log))
}

// HandleFunc registers the handler function for the given pattern.
func (mux *LoggingServeMux) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request)) {
mux.serveMux.Handle(pattern, http.HandlerFunc(handler))
}
45 changes: 29 additions & 16 deletions config/environmentconfig.go → LoggingServeMux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,39 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>

package config
package lunarc

import "strings"
import (
"net/http"
"testing"

//EnvironmentConfig contains all configs
type EnvironmentConfig struct {
Env map[string]Config
}
"github.com/Sirupsen/logrus/hooks/test"
)

//UnmarshalYAML implements Unmarshaler. Avoid use of env in the YAML file.
func (ec *EnvironmentConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
return unmarshal(&ec.Env)
func GetLoggingHTTPServer(t *testing.T, env string) (s *WebServer) {
s, err := NewWebServer("config.yml", env)
if err != nil {
t.Fatalf("Non expected error: %v", err)
}
return
}

//GetEnvironment returns a config.Config for the specified environment in parameter
func (ec *EnvironmentConfig) GetEnvironment(config *Config, environment string) {
for env, conf := range ec.Env {
if strings.Compare(environment, env) == 0 {
*config = conf
}
func TestNewLoggingServeMux(t *testing.T) {
server := GetLoggingHTTPServer(t, "test")

go server.Start()

hook := test.NewGlobal()

_, err := http.Get("http://localhost:8888/")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

if len(hook.Entries) != 1 {
t.Fatalf("Must return 1 but : %d", len(hook.Entries))
}
config = nil

go server.Stop()
<-server.Done
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

``` sh
$ go get github.com/DamienFontaine/lunarc
$ cd $GOPATH/src/github.com/DamienFontaine/lunarc
$ godep restore
```

## License
Expand Down
52 changes: 52 additions & 0 deletions StatusResponseWriter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) - Damien Fontaine <damien.fontaine@lineolia.net>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>

package lunarc

import "net/http"

// StatusResponseWriter returns status code
type StatusResponseWriter struct {
http.ResponseWriter
status int
length int
}

// Status return status code
func (w *StatusResponseWriter) Status() int {
return w.status
}

// Length return response size
func (w *StatusResponseWriter) Length() int {
return w.length
}

// Header Satisfy the http.ResponseWriter interface
func (w *StatusResponseWriter) Header() http.Header {
return w.ResponseWriter.Header()
}

// Write Satisfy the http.ResponseWriter interface
func (w *StatusResponseWriter) Write(data []byte) (int, error) {
w.length = len(data)
return w.ResponseWriter.Write(data)
}

// WriteHeader writes status code
func (w *StatusResponseWriter) WriteHeader(statusCode int) {
w.status = statusCode
w.ResponseWriter.WriteHeader(statusCode)
}
17 changes: 3 additions & 14 deletions config/config.go → config/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,7 @@

package config

//Config du Server
type Config struct {
Server struct {
Port int
URL string
Jwt struct {
Key string
}
}
Mongo struct {
Port int
Host string
Database string
}
//Environment contains all configs
type Environment interface {
GetEnvironment(string) interface{}
}
Loading

0 comments on commit 68dcc35

Please sign in to comment.