func main() { // Parse a `SectionValue` from `example.cfg` settings, err := forge.ParseFile("example.cfg") if err != nil { panic(err) } str_val, err := settings.GetString("global") if err != nil { panic(err) } fmt.Printf("global = \"%s\"\r\n", str_val) // Get a nested value // value, err := settings.Resolve("primary.included_setting") // fmt.Printf("primary.included_setting = \"%s\"\r\n", value.GetValue()) // Convert settings to a map settingsMap := settings.ToMap() fmt.Printf("global = \"%s\"\r\n", settingsMap["global"]) // Convert settings to JSON jsonBytes, err := settings.ToJSON() fmt.Printf("\r\n\r\n%s\r\n", string(jsonBytes)) }
func Example() { // Parse a `SectionValue` from `example.cfg` settings, err := forge.ParseFile("example.cfg") if err != nil { panic(err) } // Get a single value if settings.Exists("global") { // Get `global` casted as a string value, _ := settings.GetString("global") fmt.Printf("global = \"%s\"\r\n", value) } // Get a nested value value, err := settings.Resolve("primary.included_setting") fmt.Printf("primary.included_setting = \"%s\"\r\n", value.GetValue()) // You can also traverse down the sections manually primary, err := settings.GetSection("primary") strVal, err := primary.GetString("included_setting") fmt.Printf("primary.included_setting = \"%s\"\r\n", strVal) // Convert settings to a map settingsMap := settings.ToMap() fmt.Printf("global = \"%s\"\r\n", settingsMap["global"]) // Convert settings to JSON jsonBytes, err := settings.ToJSON() fmt.Printf("\r\n\r\n%s\r\n", string(jsonBytes)) }
func ExampleParseFile() { // Parse a `SectionValue` from `example.cfg` settings, err := forge.ParseFile("example.cfg") if err != nil { panic(err) } fmt.Println(settings) }
func TestParseFile(t *testing.T) { settings, err := forge.ParseFile("./test.cfg") if err != nil { t.Fatal(err) } values := settings.ToMap() assertDirectives(values, t) }
func BenchmarkParseFile(b *testing.B) { for i := 0; i < b.N; i++ { _, err := forge.ParseFile("./test.cfg") if err != nil { b.Fatal(err) } } }
func main() { settings, err := forge.ParseFile("config.cfg") if err != nil { panic(err) } // Convert settings to a map settingsMap := settings.ToMap() fmt.Printf("global = \"%s\"\r\n", settingsMap["global"]) host, err := settings.GetSection("host") //address, err := host.GetString("address") port, err := host.GetString("port") http.HandleFunc("/", handler) http.ListenAndServe(port, nil) }
func Run() { settings, err := forge.ParseFile("config.cfg") if err != nil { panic(err) } amqp, err := settings.GetSection("amqp") username, err := amqp.GetString("uname") amqpAddress, err := amqp.GetString("amqp_address") //ueGuid, err := amqp.GetString("ueguid") host, err := settings.GetSection("host") port, err := host.GetString("port") // Process Amqp messages go processAmqp(username, amqpAddress) log.SetFlags(log.LstdFlags | log.Llongfile) log.Printf("UTM-API SERVICE (%s) REST INTERFACE LISTENING ON %s\n", logTag, listenAddress) store := cookiestore.New([]byte("secretkey789")) router := routes.LoadRoutes() // router.Handle("/", utilities.Handler(login)) //router.Handle("/login", utilities.Handler(login)) router.HandleFunc("/register", registerHandler) router.HandleFunc("/login", loginHandler) //router.Handle("/register", utilities.Handler(register)) router.Handle("/latestState", utilities.Handler(getLatestState)) router.Handle("/reportingInterval", utilities.Handler(setReportingInterval)) n := negroni.Classic() static := negroni.NewStatic(http.Dir("static")) static.Prefix = "/static" n.Use(static) n.Use(negroni.HandlerFunc(system.MgoMiddleware)) n.Use(sessions.Sessions("global_session_store", store)) n.UseHandler(router) n.Run(port) }
// Entry point func Run() { // First, parse the configuration file settings, err := forge.ParseFile(configurationFile) if err != nil { panic(err) } amqp, err := settings.GetSection("amqp") username, err := amqp.GetString("uname") amqpAddress, err := amqp.GetString("amqp_address") host, err := settings.GetSection("host") port, err := host.GetString("port") // Set up the device expected message list map deviceExpectedMsgList = make(map[string]*[]ExpectedMsg) // And a timer to remove crud from it removeOldExpectedMsgs := time.NewTicker(time.Minute * 10) // Remove old stuff from the expected message list on a tick go func() { for _ = range removeOldExpectedMsgs.C { for uuid, expectedMsgList := range deviceExpectedMsgList { var x = 0 for x < len(*expectedMsgList) { if time.Now().After((*expectedMsgList)[x].TimeStarted.Add(time.Hour)) { globals.Dbg.PrintfTrace("%s [server] --> giving up after waiting > 1 hour for %d from device %s.\n", globals.LogTag, (*expectedMsgList)[x].ResponseId, uuid) *expectedMsgList = append((*expectedMsgList)[:x], (*expectedMsgList)[x+1:]...) } x++ } } } }() // Process Amqp messages go processAmqp(username, amqpAddress) // Set up logging log.SetFlags(log.LstdFlags) store := cookiestore.New([]byte("secretkey789")) router := routes.LoadRoutes() router.Handle("/frontPageData", utilities.Handler(getFrontPageData)) router.HandleFunc("/register", registerHandler) router.HandleFunc("/login", loginHandler) router.HandleFunc("/mode", ShowModeHandler) router.HandleFunc("/display", ShowDisplayHandler) router.HandleFunc("/logout", LogoutHandler) sendMsg := ClientSendMsg{} router.HandleFunc("/sendMsg", sendMsg.Send).Methods("POST") n := negroni.Classic() static := negroni.NewStatic(http.Dir("static")) static.Prefix = "/static" n.Use(static) n.Use(negroni.HandlerFunc(system.MgoMiddleware)) n.Use(sessions.Sessions("global_session_store", store)) n.UseHandler(router) n.Run(port) }