func main() { tracelog.Start(tracelog.LevelTrace) //tracelog.Start(tracelog.LevelInfo) var port = os.Getenv("PORT") if port == "" { tracelog.Error(nil, "The PORT environment variable has not been set.", "main") os.Exit(1) } var myGasFeedBaseUrl = os.Getenv("MY_GAS_FEED_BASE_URL") if myGasFeedBaseUrl == "" { tracelog.Error(nil, "The MY_GAS_FEED_BASE_URL environment variable has not been set.", "main") } SetMyGasFeedBaseUrl(myGasFeedBaseUrl) var myGasFeedApiKey = os.Getenv("MY_GAS_FEED_API_KEY") if myGasFeedApiKey == "" { tracelog.Error(nil, "The MY_GAS_FEED_API_KEY environment variable has not been set.", "main") } SetMyGasFeedApiKey(myGasFeedApiKey) tracelog.Info("Started", "main", "Gas Price Service is starting and listening on port %s", port) router := mux.NewRouter() for _, route := range routes { var handler http.Handler handler = route.HandlerFunc handler = Logger(handler, route.Name) router.Methods(route.Method).Path(route.Pattern).Name(route.Name).Handler(handler) } err := http.ListenAndServe(":"+port, router) if err != nil { tracelog.Error(err, "An error occurred while attempting to listen and serve", "main") } tracelog.Info("Stopped", "main", "Gas Price Service has been terminated") tracelog.Stop() }
// Starts the main server. func RunServer() { origin := "http://localhost/" url := "wss://ws.blockchain.info:443/inv" // Connect tracelog.Info(TAG, "RunServer", "Connecting to %s...\n", url) ws, err := websocket.Dial(url, "", origin) if err != nil { log.Fatal(err) } tracelog.Info(TAG, "RunServer", "Connected!") // Subscribe subscriptionMessage := "{\"op\":\"unconfirmed_sub\"}" subscriptionBytes := []byte(subscriptionMessage) tracelog.Info(TAG, "Subscribing with %s...\n", subscriptionMessage) if _, err := ws.Write(subscriptionBytes); err != nil { log.Fatal(err) } tracelog.Info(TAG, "RunServer", "Subscribed!") jsonData := make([]byte, 0) // Forever for { n := -1 buffer := make([]byte, 1024) // Read tracelog.Trace(TAG, "RunServer", "Reading from socket...") if n, err = ws.Read(buffer); err != nil { log.Fatal(err) } jsonData = append(jsonData, buffer[:n]...) if IsValidJson(jsonData) { // Process go HandleTransaction(jsonData) // Restart jsonData = make([]byte, 0) } } }
func publish(message string) { // Publish "hello world" on topic1. msgIDs, err := pubsub.Publish(getCtx(), "livefeed", &pubsub.Message{ Data: []byte(message), }) if err != nil { log.Println(err) tracelog.Error(err, `Error publishing to pubsub`, `publish`) } else { tracelog.Info(msgIDs[0], "publish", "Message stored in pubsub") } }
func Logger(inner http.Handler, name string) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { tracelog.Info("HTTP Request", "Handler", "%s\t%s\t%s", r.Method, r.RequestURI, name, ) inner.ServeHTTP(w, r) }) }
func main() { // tracelog.Start(tracelog.LevelTrace) tracelog.Start(tracelog.LevelInfo) var port = os.Getenv("PORT") if port == "" { tracelog.Error(nil, "The PORT environment variable has not been set.", "main") os.Exit(1) } var mapsApiKey = os.Getenv("GOOGLE_MAPS_API_KEY") if mapsApiKey == "" { tracelog.Error(nil, "The GOOGLE_MAPS_API_KEY enviornment variable has not been set.", "main") os.Exit(2) } SetGoogleAPIKey(mapsApiKey) tracelog.Info("Started", "main", "Google Reverse Geocode Service is starting and listening on port %s", port) router := mux.NewRouter() for _, route := range routes { var handler http.Handler handler = route.HandlerFunc handler = Logger(handler, route.Name) router.Methods(route.Method).Path(route.Pattern).Name(route.Name).Handler(handler) } err := http.ListenAndServe(":"+port, router) if err != nil { tracelog.Error(err, "An error occurred while attempting to listen and serve.", "main") os.Exit(3) } tracelog.Info("Stopped", "main", "Google Reverse Geocode Service has been terminated") tracelog.Stop() }
// Handles a transaction func HandleTransaction(b []byte) { tracelog.Trace(TAG, "HandleTransaction", "Converting JSON to Transaction...") t := TransactionFromJSON(b) tracelog.Info(TAG, "HandleTransaction", t.String()) msg := messages.TransactionMessage{Hash: t.Hash} jsonData, err := json.Marshal(msg) if err != nil { tracelog.Error(err, "Error trying to marshal", "HandleTransaction") } publish(string(jsonData)) }
// loadTranslationFiles loads the found translation files into the i18n // messaging system for use by the application func loadTranslationFiles(directory string) { // Read the directory fileInfos, err := ioutil.ReadDir(directory) if err != nil { tracelog.CompletedError(err, "localize", "loadTranslationFiles") return } // Look for JSON files for _, fileInfo := range fileInfos { if path.Ext(fileInfo.Name()) != ".json" { continue } fileName := fmt.Sprintf("%s/%s", directory, fileInfo.Name()) tracelog.Info("localize", "loadTranslationFiles", "Loading %s", fileName) i18n.MustLoadTranslationFile(fileName) } }
// LoadFiles looks for i18n folders inside the current directory and the GOPATH // to find translation files to load func LoadFiles(userLocale string, defaultLocal string) error { gopath := os.Getenv("GOPATH") pwd, err := os.Getwd() if err != nil { tracelog.CompletedError(err, "localize", "LoadFiles") return err } tracelog.Info("localize", "LoadFiles", "PWD[%s] GOPATH[%s]", pwd, gopath) // Load any translation files we can find searchDirectory(pwd, pwd) if gopath != "" { searchDirectory(gopath, pwd) } // Create a translation function for use T, err = i18n.Tfunc(userLocale, defaultLocal) if err != nil { return err } return nil }
func (c *MainController) Get() { c.TplNames = "books.tpl" tracelog.Info("main", "main", "finish") }