-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: preliminary implementation os sites 2.0 (#1417)
Server initialization and lots of other details. --------- Co-authored-by: juligasa <[email protected]> Co-authored-by: Eric Vicenti <[email protected]>
- Loading branch information
1 parent
c377cfd
commit c04b67a
Showing
151 changed files
with
4,752 additions
and
9,966 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
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
8 changes: 4 additions & 4 deletions
8
backend/cmd/minttergw/Dockerfile → backend/cmd/mintter-site/Dockerfile
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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
# Build from the root with `docker build . -f ./backend/cmd/minttergw/Dockerfile`. | ||
# Build from the root with `docker build . -f ./backend/cmd/mintter-site/Dockerfile`. | ||
FROM golang:1.20-alpine AS builder | ||
WORKDIR /code | ||
COPY go.mod go.sum ./ | ||
COPY third_party ./third_party | ||
RUN go mod download | ||
COPY backend ./backend | ||
RUN apk add build-base | ||
RUN go install ./backend/cmd/minttergw/ | ||
RUN go install ./backend/cmd/mintter-site/ | ||
|
||
FROM alpine:latest | ||
COPY --from=builder /go/bin/minttergw /usr/local/bin/minttergw | ||
COPY --from=builder /go/bin/mintter-site /usr/local/bin/mintter-site | ||
EXPOSE 55000 55001 55002 | ||
CMD ["/usr/local/bin/minttergw"] | ||
CMD ["/usr/local/bin/mintter-site"] |
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,77 @@ | ||
// Program mintter-site implements the Hypermedia Site server. | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"os" | ||
|
||
"mintter/backend/cmd/mintter-site/sites" | ||
"mintter/backend/daemon" | ||
|
||
"github.com/burdiyan/go/mainutil" | ||
"github.com/peterbourgon/ff/v3" | ||
) | ||
|
||
func main() { | ||
const envVarPrefix = "MINTTER" | ||
|
||
mainutil.Run(func() error { | ||
ctx := mainutil.TrapSignals() | ||
|
||
fs := flag.NewFlagSet("mintter-site", flag.ExitOnError) | ||
fs.Usage = func() { | ||
fmt.Fprintf(fs.Output(), `Usage: %s ADDRESS [flags] | ||
This program is similar to our main mintterd program in a lot of ways, but has more suitable defaults for running on a server as site. | ||
It requires one positional argument ADDRESS, which has to be a Web network address this site is supposed to be available at. | ||
The address can be a DNS name, or an IP address, and it has to be a URL with a scheme and port (if applicable). | ||
Examples: | ||
- http://127.0.0.1:42542 | ||
- https://mintter.com | ||
- http://example.com | ||
Flags: | ||
`, fs.Name()) | ||
fs.PrintDefaults() | ||
} | ||
|
||
cfg := sites.DefaultConfig() | ||
cfg.BindFlags(fs) | ||
if err := ff.Parse(fs, os.Args[1:], ff.WithEnvVarPrefix(envVarPrefix)); err != nil { | ||
return err | ||
} | ||
|
||
if len(os.Args) < 2 { | ||
fs.Usage() | ||
fmt.Fprintf(fs.Output(), "Error: Positional argument ADDRESS is missing.\n") | ||
os.Exit(1) | ||
} | ||
|
||
rawURL := os.Args[1] | ||
|
||
if err := cfg.Base.ExpandDataDir(); err != nil { | ||
return err | ||
} | ||
|
||
dir, err := daemon.InitRepo(cfg.Base.DataDir, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
app, err := sites.Load(ctx, rawURL, cfg, dir) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = app.Wait() | ||
if errors.Is(err, context.Canceled) { | ||
return nil | ||
} | ||
|
||
return err | ||
}) | ||
} |
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,14 @@ | ||
package sites | ||
|
||
import "mintter/backend/config" | ||
|
||
// DefaultConfig for sites. | ||
func DefaultConfig() config.Config { | ||
cfg := config.Default() | ||
cfg.DataDir = "~/.mintter-site" | ||
cfg.Syncing.Disabled = true | ||
cfg.P2P.ForceReachabilityPublic = true | ||
cfg.P2P.NoRelay = true | ||
|
||
return cfg | ||
} |
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,106 @@ | ||
package sites | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"mintter/backend/config" | ||
"mintter/backend/core" | ||
"mintter/backend/daemon" | ||
"mintter/backend/daemon/storage" | ||
accounts "mintter/backend/genproto/accounts/v1alpha" | ||
"mintter/backend/ipfs" | ||
"mintter/backend/mttnet" | ||
"mintter/backend/pkg/future" | ||
"mintter/backend/pkg/must" | ||
"net/url" | ||
|
||
"crawshaw.io/sqlite/sqlitex" | ||
) | ||
|
||
// App is the site daemon app. | ||
type App struct { | ||
*daemon.App | ||
|
||
Website *Website | ||
Address *url.URL | ||
Config config.Config | ||
} | ||
|
||
// Load the site daemon. | ||
func Load(ctx context.Context, address string, cfg config.Config, dir *storage.Dir) (*App, error) { | ||
u, err := url.Parse(address) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse address: %w", err) | ||
} | ||
|
||
if u.Scheme != "http" && u.Scheme != "https" { | ||
return nil, fmt.Errorf("address URL only supports http or https: got %q", address) | ||
} | ||
|
||
if u.Path != "" { | ||
return nil, fmt.Errorf("address URL must not have a path: %s", address) | ||
} | ||
|
||
cfg.P2P.AnnounceAddrs = must.Do2( | ||
ipfs.ParseMultiaddrs( | ||
ipfs.DefaultListenAddrsDNS(u.Hostname(), cfg.P2P.Port))) | ||
|
||
nf := future.New[*mttnet.Node]() | ||
ndb := future.New[*sqlitex.Pool]() | ||
site := NewServer(address, nf.ReadOnly, ndb.ReadOnly) | ||
|
||
app, err := daemon.Load(ctx, cfg, dir, site, daemon.GenericHandler{ | ||
Path: "/.well-known/hypermedia-site", | ||
Handler: site, | ||
Mode: daemon.RouteNav, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// This is some ugly stuff. Site server needs some stuff that are passed from the daemon. | ||
go func() { | ||
if err := ndb.Resolve(app.DB); err != nil { | ||
panic(err) | ||
} | ||
|
||
node, err := app.Net.Await(ctx) | ||
if err != nil && !errors.Is(err, context.Canceled) { | ||
panic(err) | ||
} | ||
|
||
if err := nf.Resolve(node); err != nil { | ||
panic(err) | ||
} | ||
}() | ||
|
||
if _, ok := dir.Identity().Get(); !ok { | ||
account, err := core.NewKeyPairRandom() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to generate random account key pair: %w", err) | ||
} | ||
|
||
if err := app.RPC.Daemon.RegisterAccount(ctx, account); err != nil { | ||
return nil, fmt.Errorf("failed to create registration: %w", err) | ||
} | ||
} | ||
|
||
if _, err := app.RPC.Accounts.UpdateProfile(ctx, &accounts.Profile{ | ||
Alias: address, | ||
Bio: "Hypermedia Site. Powered by Mintter.", | ||
}); err != nil { | ||
return nil, fmt.Errorf("failed to update profile: %w", err) | ||
} | ||
|
||
setupURL := site.GetSetupURL(ctx) | ||
|
||
fmt.Println("Site Invitation secret token: " + setupURL) | ||
|
||
return &App{ | ||
App: app, | ||
Website: site, | ||
Address: u, | ||
Config: cfg, | ||
}, nil | ||
} |
Oops, something went wrong.