-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Anik <anikbhattacharya93@gmail.com>
- Loading branch information
Showing
5 changed files
with
85 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package catalogserver | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
) | ||
|
||
// Server is a manager.Runnable Server, that serves the FBC | ||
// content of the extension catalogs added to the cluster | ||
type Server struct { | ||
Dir string | ||
Port string | ||
Mux *http.ServeMux | ||
} | ||
|
||
// NewServer takes directory and port number, and returns | ||
// a Server that serves the FBC content stored in the | ||
// directory on the given port number. | ||
func NewServer(dir, port string) Server { | ||
return Server{ | ||
Dir: dir, | ||
Port: port, | ||
Mux: &http.ServeMux{}, | ||
} | ||
} | ||
|
||
func (s Server) Start(_ context.Context) error { | ||
s.Mux.HandleFunc("/catalogs", func(w http.ResponseWriter, r *http.Request) { | ||
files, err := os.ReadDir(s.Dir) | ||
if err != nil { | ||
w.WriteHeader(http.StatusBadRequest) | ||
fmt.Fprintf(w, "error reading catalog store directory: %v", err) | ||
return | ||
} | ||
for _, file := range files { | ||
name := file.Name() | ||
fmt.Fprintf(w, "%v\n", name[:len(name)-len(filepath.Ext(name))]) | ||
} | ||
}) | ||
server := &http.Server{ | ||
Handler: s.Mux, | ||
Addr: s.Port, | ||
ReadHeaderTimeout: 3 * time.Second, | ||
} | ||
return server.ListenAndServe() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters