Skip to content

Commit 4a86e71

Browse files
committed
Add frontend binary.
1 parent 5320dd1 commit 4a86e71

File tree

4 files changed

+119
-8
lines changed

4 files changed

+119
-8
lines changed

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ CMDS=$(shell find ./cmd/* -maxdepth 1 -type d -exec basename {} \; )
22

33
cmd_targets = $(addprefix ./cmd/, $(CMDS))
44

5-
all:
5+
all: frontend
66
go install -v $(cmd_targets)
77

8+
frontend:
9+
cd frontend && npm run build && statik -src=build && npm run build:clean
10+
811
$(CMDS):
912
go install -v $(addprefix ./cmd/, $@)
1013

11-
.PHONY: all
14+
.PHONY: all frontend

cmd/ipfs-archive-api/api.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ func main() {
2121
app.Usage = "IPFS api service"
2222
app.Version = version.Version
2323
app.Flags = []cli.Flag{
24-
cli.IntFlag{
25-
Name: "port,p",
26-
Usage: "The port to listen on",
27-
Value: 7002,
24+
cli.StringFlag{
25+
Name: "address,a",
26+
Usage: "The address to listen on",
27+
},
28+
29+
cli.StringSliceFlag{
30+
Name: "backend,b",
31+
Usage: "The ipfs-archive backends to connect to.",
2832
},
2933
}
3034
app.Action = run
@@ -39,18 +43,26 @@ func run(cliCtx *cli.Context) error {
3943
panic(err)
4044
}
4145

46+
if !cliCtx.IsSet("address") {
47+
cli.ShowAppHelpAndExit(cliCtx, -1)
48+
}
49+
50+
if !cliCtx.IsSet("backend") {
51+
cli.ShowAppHelpAndExit(cliCtx, -1)
52+
}
53+
4254
// load embedded swagger file
4355
swaggerSpec, err := loads.Analyzed(restapi.SwaggerJSON, "")
4456
if err != nil {
4557
logger.Error("Error loading swaggerApi", zap.Error(err))
46-
return cli.NewExitError("", -1)
58+
return cli.NewExitError("Unable to start api.", -1)
4759
}
4860

4961
// create new service API
5062
swaggerApi := operations.NewAPI(swaggerSpec)
5163
server := restapi.NewServer(swaggerApi)
5264

53-
handler := api.NewServer(ctx, logger, []string{"localhost:7001"})
65+
handler := api.NewServer(ctx, logger, cliCtx.StringSlice("backend"))
5466
handler.Handle(swaggerApi)
5567

5668
server.SetAPI(swaggerApi)
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
"os"
6+
"os/signal"
7+
8+
"go.uber.org/zap"
9+
10+
"github.com/rakyll/statik/fs"
11+
"gopkg.in/urfave/cli.v1"
12+
13+
"context"
14+
"time"
15+
16+
"fmt"
17+
18+
_ "github.com/jirwin/ipfs-archive/frontend/statik"
19+
"github.com/jirwin/ipfs-archive/version"
20+
)
21+
22+
func main() {
23+
app := cli.NewApp()
24+
app.Name = "ipfs-archive-frontend"
25+
app.Usage = "ipfs-archive frontend service"
26+
app.Version = version.Version
27+
app.Flags = []cli.Flag{
28+
cli.StringFlag{
29+
Name: "address,a",
30+
Usage: "The address to listen on",
31+
},
32+
}
33+
app.Action = run
34+
35+
app.Run(os.Args)
36+
}
37+
38+
func run(cliCtx *cli.Context) error {
39+
ctx := context.Background()
40+
logger, err := zap.NewProduction()
41+
if err != nil {
42+
panic(err)
43+
}
44+
45+
if !cliCtx.IsSet("address") {
46+
cli.ShowAppHelpAndExit(cliCtx, -1)
47+
}
48+
49+
statikFS, err := fs.New()
50+
if err != nil {
51+
logger.Error(err.Error())
52+
return cli.NewExitError(err.Error(), -1)
53+
}
54+
55+
stop := make(chan os.Signal, 1)
56+
signal.Notify(stop, os.Interrupt)
57+
58+
mux := http.NewServeMux()
59+
mux.Handle("/", http.FileServer(statikFS))
60+
61+
addr := cliCtx.String("address")
62+
63+
server := &http.Server{
64+
Addr: addr,
65+
Handler: mux,
66+
}
67+
68+
logger.Info(fmt.Sprintf("Listening on %s", addr))
69+
70+
go func() {
71+
if err := server.ListenAndServe(); err != nil {
72+
logger.Fatal(err.Error(), zap.Error(err))
73+
}
74+
}()
75+
76+
<-stop
77+
78+
logger.Info("Shutting down server.")
79+
shutdownCtx, _ := context.WithTimeout(ctx, time.Second*10)
80+
server.Shutdown(shutdownCtx)
81+
logger.Info("Server shutdown.")
82+
83+
return nil
84+
}

frontend/statik/statik.go

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)