From cd1fd8995df263649ead6d80d64ce0e3a4d9d7dc Mon Sep 17 00:00:00 2001 From: claudemiro Date: Sat, 31 Jan 2015 08:29:01 -0300 Subject: [PATCH] File structure organization. Create a new package ipe and put everything n there except for the main function. --- app.go => ipe/app.go | 2 +- app_test.go => ipe/app_test.go | 2 +- auth.go => ipe/auth.go | 2 +- channel.go => ipe/channel.go | 2 +- channel_test.go => ipe/channel_test.go | 2 +- .../config-example.json | 0 config.go => ipe/config.go | 2 +- connection.go => ipe/connection.go | 2 +- connection_test.go => ipe/connection_test.go | 2 +- constants.go => ipe/constants.go | 2 +- errors.go => ipe/errors.go | 2 +- events.go => ipe/events.go | 2 +- extra_handlers.go => ipe/extra_handlers.go | 2 +- ipe/ipe.go | 36 +++++++++++++++++++ rest.go => ipe/rest.go | 2 +- rest_test.go_ => ipe/rest_test.go_ | 0 router.go => ipe/router.go | 2 +- routes.go => ipe/routes.go | 2 +- subscription.go => ipe/subscription.go | 2 +- webhooks.go => ipe/webhooks.go | 2 +- websockets.go => ipe/websockets.go | 2 +- main.go | 29 +++------------ 22 files changed, 59 insertions(+), 42 deletions(-) rename app.go => ipe/app.go (99%) rename app_test.go => ipe/app_test.go (99%) rename auth.go => ipe/auth.go (99%) rename channel.go => ipe/channel.go (99%) rename channel_test.go => ipe/channel_test.go (99%) rename config-example.json => ipe/config-example.json (100%) rename config.go => ipe/config.go (98%) rename connection.go => ipe/connection.go (98%) rename connection_test.go => ipe/connection_test.go (93%) rename constants.go => ipe/constants.go (99%) rename errors.go => ipe/errors.go (99%) rename events.go => ipe/events.go (99%) rename extra_handlers.go => ipe/extra_handlers.go (98%) create mode 100644 ipe/ipe.go rename rest.go => ipe/rest.go (99%) rename rest_test.go_ => ipe/rest_test.go_ (100%) rename router.go => ipe/router.go (98%) rename routes.go => ipe/routes.go (98%) rename subscription.go => ipe/subscription.go (97%) rename webhooks.go => ipe/webhooks.go (99%) rename websockets.go => ipe/websockets.go (99%) diff --git a/app.go b/ipe/app.go similarity index 99% rename from app.go rename to ipe/app.go index 121b030..4de6dfb 100644 --- a/app.go +++ b/ipe/app.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package main +package ipe import ( "errors" diff --git a/app_test.go b/ipe/app_test.go similarity index 99% rename from app_test.go rename to ipe/app_test.go index 6e2868f..9b2d931 100644 --- a/app_test.go +++ b/ipe/app_test.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package main +package ipe import ( "strconv" diff --git a/auth.go b/ipe/auth.go similarity index 99% rename from auth.go rename to ipe/auth.go index fe280d0..0b7a661 100644 --- a/auth.go +++ b/ipe/auth.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package main +package ipe import ( "net/http" diff --git a/channel.go b/ipe/channel.go similarity index 99% rename from channel.go rename to ipe/channel.go index 061047b..3df0dac 100644 --- a/channel.go +++ b/ipe/channel.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package main +package ipe import ( "encoding/json" diff --git a/channel_test.go b/ipe/channel_test.go similarity index 99% rename from channel_test.go rename to ipe/channel_test.go index 6675e63..aa527f6 100644 --- a/channel_test.go +++ b/ipe/channel_test.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package main +package ipe import "testing" diff --git a/config-example.json b/ipe/config-example.json similarity index 100% rename from config-example.json rename to ipe/config-example.json diff --git a/config.go b/ipe/config.go similarity index 98% rename from config.go rename to ipe/config.go index 8c2c179..42d71cf 100644 --- a/config.go +++ b/ipe/config.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package main +package ipe import ( "errors" diff --git a/connection.go b/ipe/connection.go similarity index 98% rename from connection.go rename to ipe/connection.go index f354109..d2463c8 100644 --- a/connection.go +++ b/ipe/connection.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package main +package ipe import ( log "github.com/golang/glog" diff --git a/connection_test.go b/ipe/connection_test.go similarity index 93% rename from connection_test.go rename to ipe/connection_test.go index 92318a5..bec49cb 100644 --- a/connection_test.go +++ b/ipe/connection_test.go @@ -2,4 +2,4 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package main +package ipe diff --git a/constants.go b/ipe/constants.go similarity index 99% rename from constants.go rename to ipe/constants.go index 06798ce..c110969 100644 --- a/constants.go +++ b/ipe/constants.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package main +package ipe // Error Codes const ( diff --git a/errors.go b/ipe/errors.go similarity index 99% rename from errors.go rename to ipe/errors.go index 54d889b..df2634e 100644 --- a/errors.go +++ b/ipe/errors.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package main +package ipe // Base interface type WebsocketError interface { diff --git a/events.go b/ipe/events.go similarity index 99% rename from events.go rename to ipe/events.go index 6e8449d..83e0938 100644 --- a/events.go +++ b/ipe/events.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package main +package ipe import ( "encoding/json" diff --git a/extra_handlers.go b/ipe/extra_handlers.go similarity index 98% rename from extra_handlers.go rename to ipe/extra_handlers.go index 4ee80c7..8a694f9 100644 --- a/extra_handlers.go +++ b/ipe/extra_handlers.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package main +package ipe import ( "fmt" diff --git a/ipe/ipe.go b/ipe/ipe.go new file mode 100644 index 0000000..72a4c66 --- /dev/null +++ b/ipe/ipe.go @@ -0,0 +1,36 @@ +// Copyright 2015 Claudemiro Alves Feitosa Neto. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package ipe + +import ( + "encoding/json" + "io/ioutil" + "net/http" +) + +// Conf holds the global configuration state +var Conf ConfigFile + +// Start Parse the configuration file and starts the ipe server +func Start(configfile string) error { + file, err := ioutil.ReadFile(configfile) + + if err != nil { + return err + } + + if err := json.Unmarshal(file, &Conf); err != nil { + return err + } + + Conf.Init() + router := NewRouter() + + if err := http.ListenAndServe(Conf.Host, router); err != nil { + return err + } + + return nil +} diff --git a/rest.go b/ipe/rest.go similarity index 99% rename from rest.go rename to ipe/rest.go index 4b75db2..2167a86 100644 --- a/rest.go +++ b/ipe/rest.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package main +package ipe import ( "encoding/json" diff --git a/rest_test.go_ b/ipe/rest_test.go_ similarity index 100% rename from rest_test.go_ rename to ipe/rest_test.go_ diff --git a/router.go b/ipe/router.go similarity index 98% rename from router.go rename to ipe/router.go index 8ad2a79..d74278c 100644 --- a/router.go +++ b/ipe/router.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package main +package ipe import ( _ "expvar" diff --git a/routes.go b/ipe/routes.go similarity index 98% rename from routes.go rename to ipe/routes.go index 8d1b0a6..48ba587 100644 --- a/routes.go +++ b/ipe/routes.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package main +package ipe import ( "net/http" diff --git a/subscription.go b/ipe/subscription.go similarity index 97% rename from subscription.go rename to ipe/subscription.go index f8754ee..97706f1 100644 --- a/subscription.go +++ b/ipe/subscription.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package main +package ipe // A Channel Subscription type Subscription struct { diff --git a/webhooks.go b/ipe/webhooks.go similarity index 99% rename from webhooks.go rename to ipe/webhooks.go index a13caa8..497f671 100644 --- a/webhooks.go +++ b/ipe/webhooks.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package main +package ipe import ( "bytes" diff --git a/websockets.go b/ipe/websockets.go similarity index 99% rename from websockets.go rename to ipe/websockets.go index 4dfd2d7..57f2ab2 100644 --- a/websockets.go +++ b/ipe/websockets.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package main +package ipe import ( "encoding/json" diff --git a/main.go b/main.go index 8718170..0fc872b 100644 --- a/main.go +++ b/main.go @@ -5,45 +5,26 @@ package main import ( - "encoding/json" "flag" "fmt" - "io/ioutil" - "net/http" + "github.com/dimiro1/ipe/ipe" log "github.com/golang/glog" ) -var Conf ConfigFile - +// Main function, initialze the system func main() { - printBanner() - var filename = flag.String("config", "config.json", "Config file location") - flag.Parse() - file, err := ioutil.ReadFile(*filename) + printBanner() - if err != nil { - log.Fatal(err) - } - - if err := json.Unmarshal(file, &Conf); err != nil { - log.Fatal(err) - } - - Conf.Init() - - router := NewRouter() - - log.Infof("Starting IpĂȘ using config file: '%s'", *filename) - - if err := http.ListenAndServe(Conf.Host, router); err != nil { + if err := ipe.Start(*filename); err != nil { log.Fatal(err) } } +// Print a beatifull banner func printBanner() { fmt.Print("\033[36m") fmt.Print(`