V
V
v- death2015-10-26 22:17:40
go
v- death, 2015-10-26 22:17:40

How to create multiple file servers in golang?

Here is my code

package main

import (
  "encoding/json"
  "io"
  "log"
  "net/http"
  "strings"
)

func main() {
  const jsonStream = `
    {"Port": "8080", "Dir": "/home/v-smerti/localhost/public/file_server"}
    {"Port": "8081", "Dir": "/home/v-smerti/localhost/public/file"}
  `
  type Message struct {
    Port, Dir string
  }
  dec := json.NewDecoder(strings.NewReader(jsonStream))
  for {
    var m Message
    if err := dec.Decode(&m); err == io.EOF {
      break
    } else if err != nil {
      log.Fatal(err)
    }

    go http.ListenAndServe(m.Port, http.FileServer(http.Dir(m.Dir)))
  }
}

I want to give each folder its own access port (purely for academic purposes).
During go run main.go, the compiler does not see any errors. But the software simply hangs a little and finishes its work.
If you remove the go routine announcement, then it writes in the logs
8080: /home/v-smerti/localhost/public/file_server
panic: listen tcp: missing port in address 8080

goroutine 1 [running]:
main.main()
  /home/v-smerti/localhost/api/src/file_server/main.go:29 +0x431

goroutine 17 [syscall, locked to thread]:
runtime.goexit()
  /usr/lib/go/src/runtime/asm_386.s:1662 +0x1
exit status 2

How to fix?
There is one more problem. If json is read from a file and inserted into the jsonStream, then a type conflict occurs. Expects byte and I shove string
# command-line-arguments
./main.go:23: cannot use bs (type []byte) as type string in argument to strings.NewReader

How to fix ? thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill, 2015-10-26
@vGrabko99

only for "purely academic purposes"

package main

import (
  "encoding/json"
  "io"
  "log"
  "net/http"
  "strings"
)

func main() {
  const jsonStream = `
    {"Port": ":8080", "Dir": "/home/v-smerti/localhost/public/file_server"}
    {"Port": ":8081", "Dir": "/home/v-smerti/localhost/public/file"}
  `
  type Message struct {
    Port, Dir string
  }
  dec := json.NewDecoder(strings.NewReader(jsonStream))
  for {
    var m Message
    if err := dec.Decode(&m); err == io.EOF {
      break
    } else if err != nil {
      log.Fatal(err)
    }

    go http.ListenAndServe(m.Port, http.FileServer(http.Dir(m.Dir)))
  }

  forever := make(chan struct{})
  <-forever
}

ps:
1
missing port in address 8080 - it says there is no port in the address, either specify ip:port or :port and listen to all interfaces
2
cannot use bs (type []byte) as type string in argument to strings.NewReader
too everything is written, you need to "cast" []byte to string: string(bs)
3 software just "hangs" a little and finishes its work.
that's right, main has finished its work, the goroutines are spinning "separately" no one is waiting for
them ps2: dec := json.NewDecoder(strings.NewReader(jsonStream)) to work with the stream (os.Open() as an example of working with a local file), a so json.Unmarshal

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question