├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── cmd └── orbitdb │ ├── cmd │ ├── add.go │ ├── create.go │ ├── del.go │ ├── drop.go │ ├── get.go │ ├── get_test.go │ ├── identify.go │ ├── inc.go │ ├── index.go │ ├── info.go │ ├── list.go │ ├── put.go │ ├── query.go │ └── root.go │ └── main.go └── http ├── request.go └── request_test.go /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level of experience, 9 | nationality, personal appearance, race, religion, or sexual identity and 10 | orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at [community@orbitdb.org](mailto:community@orbitdb.org), which goes to all members of the @OrbitDB community team, or to [richardlitt@orbitdb.org](mailto:richardlitt@orbitdb.org), which goes only to [@RichardLitt](https://github.com/RichardLitt) or to [haadcode@orbitdb.org](mailto:haadcode@orbitdb.org), which goes only to [@haadcode](https://github.com/haadcode). All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contribute 2 | 3 | Please contribute! Here are some things that would be great: 4 | 5 | - [Open an issue!](https://github.com/orbitdb/go-orbit-db/issues/new) 6 | - Open a pull request! 7 | - Say hi! :wave: 8 | 9 | Please note that we have a [Code of Conduct](CODE_OF_CONDUCT.md), and that all activity in the [@OrbitDB](https://github.com/orbitdb) organization falls under it. Read it before you contribute, as being part of this community means that you agree to abide by it. Thanks. 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2019 OrbitDB community 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # go-orbit-db 2 | 3 | [![Gitter](https://img.shields.io/gitter/room/nwjs/nw.js.svg)](https://gitter.im/orbitdb/Lobby) [![Matrix](https://img.shields.io/badge/matrix-%23orbitdb%3Apermaweb.io-blue.svg)](https://riot.permaweb.io/#/room/#orbitdb:permaweb.io) [![Discord](https://img.shields.io/discord/475789330380488707?color=blueviolet&label=discord)](https://discord.gg/cscuf5T) 4 | 5 | > Go implementation of OrbitDB. 6 | 7 | OrbitDB is a decentralized database system using IPFS for peer-to-peer replication. 8 | 9 | ## Install 10 | 11 | This project uses [Go](https://golang.org/). 12 | 13 | ```sh 14 | go get github.com/orbitdb/go-orbit-db 15 | ``` 16 | 17 | ## Usage 18 | 19 | ```sh 20 | orbitdb 21 | ``` 22 | 23 | ## Maintainers 24 | 25 | Hayden Young ([@haydenyoung](https://github.com/haydenyoung)) is the maintainer and lead author of the OrbitDB Go implementation. 26 | 27 | ## Contribute 28 | 29 | We would be happy to accept PRs! If you want to work on something, it'd be good to talk beforehand to make sure nobody else is working on it. You can reach us [on Gitter](https://gitter.im/orbitdb/Lobby), or in the [issues section](https://github.com/orbitdb/go-orbit-db/issues). 30 | 31 | We also have **regular community calls**, which we announce in the issues in [the @orbitdb welcome repository](https://github.com/orbitdb/welcome/issues). Join us! 32 | 33 | For specific guidelines for contributing to this repository, check out the [Contributing guide](CONTRIBUTING.md). For more on contributing to OrbitDB in general, take a look at the [@OrbitDB welcome repository](https://github.com/orbitdb/welcome). Please note that all interactions in [@OrbitDB](https://github.com/orbitdb) fall under our [Code of Conduct](CODE_OF_CONDUCT.md). 34 | 35 | ## License 36 | 37 | [MIT](LICENSE) © 2019 OrbitDB community 38 | -------------------------------------------------------------------------------- /cmd/orbitdb/cmd/add.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "github.com/spf13/cobra" 7 | "github.com/orbitdb/go-orbit-db/http" 8 | ) 9 | 10 | var addCmd = &cobra.Command{ 11 | Use: "add ", 12 | Short: "Adds a record to the feed or eventlog database.", 13 | Long: ` 14 | The add command appends a new record to a database of type feed or eventlog. Add 15 | will not work with any other database type. 16 | 17 | When adding a record, must be plain text data, 18 | 19 | E.g. 20 | 21 | orbitdb add feed "OrbitDB"`, 22 | Run: func(cmd *cobra.Command, args []string) { 23 | if len(args) != 2 { 24 | fmt.Println("Usage: orbitdb add ") 25 | os.Exit(1) 26 | } 27 | 28 | var r http.Request 29 | 30 | r.SetURL(httpHost, "/db/", args[0], "/add") 31 | r.Data = args[1] 32 | 33 | fmt.Println(r.Post()) 34 | }, 35 | } 36 | 37 | func init() { 38 | rootCmd.AddCommand(addCmd) 39 | } 40 | -------------------------------------------------------------------------------- /cmd/orbitdb/cmd/create.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "github.com/spf13/cobra" 7 | "github.com/orbitdb/go-orbit-db/http" 8 | ) 9 | 10 | var createCmd = &cobra.Command{ 11 | Use: "create ", 12 | Short: "Creates a database or opens it if it already exists.", 13 | Long: ` 14 | The create command creates a new database with name of type . 15 | 16 | If the database already exists, it is opened.`, 17 | Run: func(cmd *cobra.Command, args []string) { 18 | if len(args) != 2 { 19 | fmt.Println("Usage: orbitdb create ") 20 | os.Exit(1) 21 | } 22 | 23 | params := make(map[string]string) 24 | params["create"] = "true" 25 | params["type"] = args[1] 26 | 27 | var r http.Request 28 | 29 | r.SetURL(httpHost, "/db/", args[0]) 30 | r.SetJSONData(params) 31 | 32 | fmt.Println(r.Post()) 33 | }, 34 | } 35 | 36 | func init() { 37 | rootCmd.AddCommand(createCmd) 38 | } 39 | -------------------------------------------------------------------------------- /cmd/orbitdb/cmd/del.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "github.com/spf13/cobra" 7 | "github.com/orbitdb/go-orbit-db/http" 8 | ) 9 | 10 | var delCmd = &cobra.Command{ 11 | Use: "del ", 12 | Short: "Deletes a record from the database.", 13 | Long: ` 14 | The del command deletes a record identified by from . 15 | 16 | Only applies to docstore, keyvalue and feed.`, 17 | Run: func(cmd *cobra.Command, args []string) { 18 | if len(args) < 2 { 19 | fmt.Println("Usage: orbitdb del ") 20 | os.Exit(1) 21 | } 22 | 23 | var r http.Request 24 | 25 | r.SetURL(httpHost, "/db/", args[0], "/", args[1]) 26 | 27 | fmt.Println(r.Delete()) 28 | }, 29 | } 30 | 31 | func init() { 32 | rootCmd.AddCommand(delCmd) 33 | } 34 | -------------------------------------------------------------------------------- /cmd/orbitdb/cmd/drop.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "github.com/spf13/cobra" 7 | "github.com/orbitdb/go-orbit-db/http" 8 | ) 9 | 10 | var dropCmd = &cobra.Command{ 11 | Use: "drop ", 12 | Short: "Drop a database.", 13 | Long: ` 14 | The drop command drops locally. 15 | 16 | This does not affect replicated databases.`, 17 | Run: func(cmd *cobra.Command, args []string) { 18 | if len(args) < 1 { 19 | fmt.Println("Usage: orbitdb drop ") 20 | os.Exit(1) 21 | } 22 | 23 | var r http.Request 24 | 25 | r.SetURL(httpHost, "/db/", args[0]) 26 | 27 | fmt.Println(r.Delete()) 28 | }, 29 | } 30 | 31 | func init() { 32 | rootCmd.AddCommand(dropCmd) 33 | } 34 | -------------------------------------------------------------------------------- /cmd/orbitdb/cmd/get.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "github.com/spf13/cobra" 7 | "github.com/orbitdb/go-orbit-db/http" 8 | ) 9 | 10 | var getCmd = &cobra.Command{ 11 | Use: "get []", 12 | Short: "Gets a record from the database.", 13 | Long: ` 14 | The get command gets a record identified by from the database named 15 | .`, 16 | Run: func(cmd *cobra.Command, args []string) { 17 | if len(args) < 1 { 18 | fmt.Println("Usage: orbitdb get []") 19 | os.Exit(1) 20 | } 21 | 22 | var r http.Request 23 | 24 | r.SetURL(httpHost, "/db/", args[0]) 25 | 26 | // if no id is supplied, assume counter is being retrieved. 27 | if len(args) == 1 { 28 | r.SetURL(r.Url, "/value") 29 | } else { 30 | r.SetURL(r.Url, "/", args[1]) 31 | } 32 | 33 | fmt.Println(r.Get()) 34 | }, 35 | } 36 | 37 | func init() { 38 | rootCmd.AddCommand(getCmd) 39 | } 40 | -------------------------------------------------------------------------------- /cmd/orbitdb/cmd/get_test.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "os" 5 | "os/exec" 6 | "testing" 7 | "log" 8 | ) 9 | 10 | var mainGo = "../main.go" 11 | var feedDBName = "test-feed" 12 | 13 | func TestMain(m *testing.M) { 14 | exec.Command("go", "run", mainGo, "create", feedDBName, "feed").Run() 15 | exec.Command("go", "run", mainGo, "add", feedDBName, "{\"feed-item-1\":\"\"}").Run() 16 | 17 | os.Exit(m.Run()) 18 | } 19 | 20 | func TestGetWithFeed(t *testing.T) { 21 | out, err := exec.Command("go", "run", mainGo, "get", feedDBName, "feed-item-1").Output() 22 | if err != nil { 23 | log.Fatal(err) 24 | } 25 | 26 | expected := "{\"feed-item-1\":\"\"}\n" 27 | actual := string(out) 28 | 29 | if actual != expected { 30 | t.Error(feedDBName, "database should have one item called feed-item-1") 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /cmd/orbitdb/cmd/identify.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "github.com/spf13/cobra" 5 | "github.com/orbitdb/go-orbit-db/http" 6 | "fmt" 7 | ) 8 | 9 | var identifyCmd = &cobra.Command{ 10 | Use: "identify", 11 | Short: "OrbitDB's identity settings.", 12 | Long: ` 13 | The identify command shows information about the peer.`, 14 | Run: func(cmd *cobra.Command, args []string) { 15 | var r http.Request 16 | 17 | r.SetURL(httpHost, "/identity") 18 | 19 | fmt.Println(r.Get()) 20 | }, 21 | } 22 | 23 | func init() { 24 | rootCmd.AddCommand(identifyCmd) 25 | } 26 | -------------------------------------------------------------------------------- /cmd/orbitdb/cmd/inc.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "github.com/spf13/cobra" 7 | "github.com/orbitdb/go-orbit-db/http" 8 | ) 9 | 10 | var incCmd = &cobra.Command{ 11 | Use: "inc []", 12 | Aliases: []string{"increment"}, 13 | Short: "Increments a counter.", 14 | Long: ` 15 | The inc command increments a counter data store. If no is provided 16 | counter is incremented by 1.`, 17 | Run: func(cmd *cobra.Command, args []string) { 18 | if len(args) < 1 { 19 | fmt.Println("Usage: orbitdb inc []]") 20 | os.Exit(1) 21 | } 22 | 23 | var r http.Request 24 | 25 | r.SetURL(httpHost, "/db/", args[0], "/inc") 26 | 27 | if len(args) == 2 { 28 | r.SetURL(r.Url, "/", args[1]) 29 | } 30 | 31 | fmt.Println(r.Post()) 32 | }, 33 | } 34 | 35 | func init() { 36 | rootCmd.AddCommand(incCmd) 37 | } 38 | -------------------------------------------------------------------------------- /cmd/orbitdb/cmd/index.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "github.com/spf13/cobra" 7 | "github.com/orbitdb/go-orbit-db/http" 8 | ) 9 | 10 | // indexCmd represents the index command 11 | var indexCmd = &cobra.Command{ 12 | Use: "index ", 13 | Short: "Gets information about the data stored in .", 14 | Long: ` 15 | The index command lists all informatoin about data stored in the database 16 | named . 17 | 18 | The information returned will differ depending on the data type.`, 19 | Run: func(cmd *cobra.Command, args []string) { 20 | if len(args) < 1 { 21 | fmt.Println("Usage: orbitdb index name") 22 | os.Exit(1) 23 | } 24 | 25 | var r http.Request 26 | 27 | r.SetURL(httpHost, "/db/", args[0], "/index") 28 | 29 | fmt.Println(r.Get()) 30 | }, 31 | } 32 | 33 | func init() { 34 | rootCmd.AddCommand(indexCmd) 35 | } 36 | -------------------------------------------------------------------------------- /cmd/orbitdb/cmd/info.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "github.com/spf13/cobra" 7 | "github.com/orbitdb/go-orbit-db/http" 8 | ) 9 | 10 | var infoCmd = &cobra.Command{ 11 | Use: "info ", 12 | Short: "Lists details about the database.", 13 | Long: ` 14 | The info command lists the details of the database named .`, 15 | Run: func(cmd *cobra.Command, args []string) { 16 | if len(args) < 1 { 17 | fmt.Println("Usage: orbitdb info ") 18 | os.Exit(1) 19 | } 20 | 21 | var r http.Request 22 | 23 | r.SetURL(httpHost, "/db/", args[0]) 24 | 25 | fmt.Println(r.Get()) 26 | }, 27 | } 28 | 29 | func init() { 30 | rootCmd.AddCommand(infoCmd) 31 | } 32 | -------------------------------------------------------------------------------- /cmd/orbitdb/cmd/list.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/spf13/cobra" 7 | "github.com/orbitdb/go-orbit-db/http" 8 | ) 9 | 10 | var listCmd = &cobra.Command{ 11 | Use: "list", 12 | Short: "Lists all registered databases.", 13 | Long: ` 14 | The list command lists all registered databases. A registered database is one 15 | that has been created and/or loaded using the create command.`, 16 | Run: func(cmd *cobra.Command, args []string) { 17 | var r http.Request 18 | 19 | r.SetURL(httpHost, "/dbs") 20 | 21 | fmt.Println(r.Get()) 22 | }, 23 | } 24 | 25 | func init() { 26 | rootCmd.AddCommand(listCmd) 27 | } 28 | -------------------------------------------------------------------------------- /cmd/orbitdb/cmd/put.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "github.com/spf13/cobra" 7 | "github.com/orbitdb/go-orbit-db/http" 8 | ) 9 | 10 | var putCmd = &cobra.Command{ 11 | Use: "put ", 12 | Short: "Adds a record to a document database.", 13 | Long: ` 14 | The put command adds a record with to a document database named 15 | . 16 | 17 | If the database has a unique identifier and contains an existing id, 18 | the matching record will be updated. 19 | 20 | must be JSON-formatted. 21 | `, 22 | Run: func(cmd *cobra.Command, args []string) { 23 | if len(args) != 2 { 24 | fmt.Println("Usage: orbitdb put ") 25 | os.Exit(1) 26 | } 27 | 28 | var r http.Request 29 | 30 | r.SetURL(httpHost, "/db/", args[0]) 31 | r.Data = args[1] 32 | 33 | fmt.Println(r.Post()) 34 | }, 35 | } 36 | 37 | func init() { 38 | rootCmd.AddCommand(putCmd) 39 | } 40 | -------------------------------------------------------------------------------- /cmd/orbitdb/cmd/query.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "github.com/spf13/cobra" 7 | "github.com/orbitdb/go-orbit-db/http" 8 | ) 9 | 10 | var queryCmd = &cobra.Command{ 11 | Use: "query [params]", 12 | Short: "Executes a query against the database.", 13 | Long: ` 14 | The query command queries the database . 15 | 16 | [params] is a list of JSON-formatted querying parameters: 17 | 18 | propname 19 | cond 20 | values 21 | 22 | If no [params] are specified, all records are returned.`, 23 | Run: func(cmd *cobra.Command, args []string) { 24 | if len(args) < 1 { 25 | fmt.Println("Usage: orbitdb query [params]") 26 | os.Exit(1) 27 | } 28 | 29 | var r http.Request 30 | 31 | if len(args) == 1 { 32 | r.SetURL(httpHost, "/db/", args[0], "/all") 33 | fmt.Println(r.Get()) 34 | } else { 35 | r.SetURL(httpHost, "/db/", args[0], "/query") 36 | r.Data = args[1] 37 | fmt.Println(r.Post()) 38 | } 39 | }, 40 | } 41 | 42 | func init() { 43 | rootCmd.AddCommand(queryCmd) 44 | } 45 | -------------------------------------------------------------------------------- /cmd/orbitdb/cmd/root.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "github.com/spf13/cobra" 7 | 8 | homedir "github.com/mitchellh/go-homedir" 9 | "github.com/spf13/viper" 10 | ) 11 | 12 | 13 | var cfgFile string 14 | 15 | var httpHost = "https://localhost:3000" 16 | 17 | 18 | // rootCmd represents the base command when called without any subcommands 19 | var rootCmd = &cobra.Command{ 20 | Use: "orbitdb", 21 | Short: "OrbitDB is a decentralized database system.", 22 | Long: ``, 23 | } 24 | 25 | func Execute() { 26 | if err := rootCmd.Execute(); err != nil { 27 | fmt.Println(err) 28 | os.Exit(1) 29 | } 30 | } 31 | 32 | func init() { 33 | cobra.OnInitialize(initConfig) 34 | 35 | rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.orbitdb.yaml)") 36 | rootCmd.PersistentFlags().StringVar(&httpHost, "http-host", httpHost, "HTTP API host address.") 37 | 38 | rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") 39 | } 40 | 41 | func initConfig() { 42 | if cfgFile != "" { 43 | viper.SetConfigFile(cfgFile) 44 | } else { 45 | home, err := homedir.Dir() 46 | if err != nil { 47 | fmt.Println(err) 48 | os.Exit(1) 49 | } 50 | 51 | viper.AddConfigPath(home) 52 | viper.SetConfigName(".orbitdb") 53 | } 54 | 55 | viper.AutomaticEnv() 56 | 57 | // If a config file is found, read it in. 58 | if err := viper.ReadInConfig(); err == nil { 59 | fmt.Println("Using config file:", viper.ConfigFileUsed()) 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /cmd/orbitdb/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "github.com/orbitdb/go-orbit-db/cmd/orbitdb/cmd" 4 | 5 | func main() { 6 | cmd.Execute() 7 | } 8 | -------------------------------------------------------------------------------- /http/request.go: -------------------------------------------------------------------------------- 1 | package http 2 | 3 | import ( 4 | "encoding/json" 5 | "io/ioutil" 6 | "net/http" 7 | "time" 8 | "strings" 9 | "bytes" 10 | "net/url" 11 | ) 12 | 13 | type Request struct { 14 | Url, Data string 15 | } 16 | 17 | func (r *Request) SetURL(parts ...string) { 18 | var url strings.Builder 19 | 20 | for i := 0; i < len(parts); i++ { 21 | url.WriteString(parts[i]) 22 | } 23 | 24 | r.Url = url.String() 25 | } 26 | 27 | func (r *Request) SetJSONData(jsonMap map[string]string) { 28 | jsonString, _ := json.Marshal(jsonMap) 29 | r.Data = string(jsonString) 30 | } 31 | 32 | func (r *Request) Get() string { 33 | timeout := time.Duration(5 * time.Second) 34 | 35 | client := http.Client{ 36 | Timeout: timeout, 37 | } 38 | 39 | request, err := http.NewRequest("GET", r.Url, bytes.NewBufferString(r.Data)) 40 | 41 | if err != nil { 42 | panic(err) 43 | } 44 | 45 | response, err := client.Do(request) 46 | 47 | if err != nil { 48 | panic(err) 49 | } else { 50 | data, _ := ioutil.ReadAll(response.Body) 51 | return string(data) 52 | } 53 | 54 | return "" 55 | } 56 | 57 | func isJSON(str string) bool { 58 | var js json.RawMessage 59 | return json.Unmarshal([]byte(str), &js) == nil 60 | } 61 | 62 | func (r *Request) Post() string { 63 | returned := "" 64 | var postData = []byte("") 65 | var postType = "text/plain" 66 | 67 | if isJSON(r.Data) { 68 | rawIn := json.RawMessage(r.Data) 69 | jsonData, err := rawIn.MarshalJSON() 70 | postData = jsonData 71 | postType = "application/json" 72 | 73 | if err != nil { 74 | panic(err) 75 | } 76 | } else { 77 | postData = []byte(url.QueryEscape(r.Data)) 78 | } 79 | 80 | request, err := http.NewRequest(http.MethodPost, r.Url, bytes.NewReader(postData)) 81 | 82 | request.Header.Set("Content-Type", postType) 83 | 84 | timeout := time.Duration(5 * time.Second) 85 | 86 | client := http.Client{ 87 | Timeout: timeout, 88 | } 89 | 90 | response, err := client.Do(request) 91 | 92 | if err != nil { 93 | panic(err) 94 | } else { 95 | body, _ := ioutil.ReadAll(response.Body) 96 | 97 | returned = string(body) 98 | } 99 | 100 | return returned 101 | } 102 | 103 | func (r *Request) Delete() string { 104 | returned := "" 105 | var postData = []byte("") 106 | var postType = "text/plain" 107 | 108 | if isJSON(r.Data) { 109 | rawIn := json.RawMessage(r.Data) 110 | jsonData, err := rawIn.MarshalJSON() 111 | postData = jsonData 112 | postType = "application/json" 113 | 114 | if err != nil { 115 | panic(err) 116 | } 117 | } else { 118 | postData = []byte(url.QueryEscape(r.Data)) 119 | } 120 | 121 | request, err := http.NewRequest(http.MethodDelete, r.Url, bytes.NewReader(postData)) 122 | 123 | request.Header.Set("Content-Type", postType) 124 | 125 | timeout := time.Duration(5 * time.Second) 126 | 127 | client := http.Client{ 128 | Timeout: timeout, 129 | } 130 | 131 | response, err := client.Do(request) 132 | 133 | if err != nil { 134 | panic(err) 135 | } else { 136 | body, _ := ioutil.ReadAll(response.Body) 137 | 138 | returned = string(body) 139 | } 140 | 141 | return returned 142 | } 143 | -------------------------------------------------------------------------------- /http/request_test.go: -------------------------------------------------------------------------------- 1 | package http 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func TestBuildURL(t *testing.T) { 10 | url := BuildURL("http://", "localhost", ":3000") 11 | assert.Equal(t, "http://localhost:3000", url, "Expecting `http://localhost:3000`") 12 | } 13 | --------------------------------------------------------------------------------