Browse Source

Add server

golang
garritfra 6 years ago
parent
commit
35b373fcdb
  1. 22
      core/Server.go
  2. 2
      core/main.go

22
core/Server.go

@ -0,0 +1,22 @@
package main
import (
"net/http"
"strings"
)
// ServeHTTP starts the HTTP server on port 8080
func StartServer() {
http.HandleFunc("/", sayHello)
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}
func sayHello(w http.ResponseWriter, r *http.Request) {
message := r.URL.Path
message = strings.TrimPrefix(message, "/")
message = "Hello " + message
w.Write([]byte(message))
}

2
core/main.go

@ -16,6 +16,8 @@ type Transaction struct {
func main() {
blockchain := newBlockchain()
StartServer()
data, _ := json.Marshal(Transaction{Sender: "foo", Receiver: "bar", Amount: 100})
block := newBlock(blockchain.blocks[0].Hash, data)
blockchain.addBlock(block)

Loading…
Cancel
Save