Skip to content

Commit

Permalink
make s-web-portfolio serve the porfolio
Browse files Browse the repository at this point in the history
  • Loading branch information
cottand committed Aug 10, 2024
1 parent 37d5630 commit b3c5c4c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
21 changes: 20 additions & 1 deletion services/lib/bedrock/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log/slog"
"net/http"
"os"
"path"
"strconv"
)

Expand All @@ -18,7 +19,25 @@ func Init() {

//slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stderr, nil)))

slog.Info("Bedrock initialized")
slog.Info("bedrock initialized")

d, err := LocalNixDir()
if err == nil {
slog.Info("using local Nix dir", "dir", d)
}
}

// LocalNixDir returns the parent of the parent dir the binary is in.
// For Nix-built binaries, this usually matches
// the root of the derivation the binary is being run from.
//
// Use this to access files included in the build via the Nix derivation.
func LocalNixDir() (string, error) {
e, err := os.Executable()
if err != nil {
return "", terrors.Propagate(err)
}
return path.Dir(path.Dir(e)), nil
}

func GetBaseConfig() (*BaseConfig, error) {
Expand Down
13 changes: 7 additions & 6 deletions services/s-web-portfolio/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ let
subPackages = [ name ];
vendorHash = null;
});
binWithWeb = buildEnv {
inherit name;
paths = [ bin web ];
pathsToLink = [ "/bin" "/srv" ];
};
# has files under /srv
web = (builtins.getFlake "github:cottand/web-portfolio/148bf78b0fa4b87c73079274c629f1e02564867d").packages.${system}.static;
image = dockerTools.buildImage {
inherit name;
copyToRoot = buildEnv {
inherit name;
paths = [ bash bin web ];
pathsToLink = [ "/bin" "/srv" ];
};
copyToRoot = binWithWeb;
config.Cmd = [ "/bin/${name}" ];
};
in
bin // { image = image; }
binWithWeb // { image = image; }
15 changes: 9 additions & 6 deletions services/s-web-portfolio/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"github.com/monzo/terrors"
"log/slog"
"log"
"net/http"
)
import "github.com/cottand/selfhosted/services/lib/bedrock"
Expand All @@ -11,16 +11,19 @@ func main() {
bedrock.Init()
conf, err := bedrock.GetBaseConfig()
if err != nil {
slog.Error(err.Error())
panic(err)
log.Fatalf(terrors.Propagate(err).Error())
}

fs := http.FileServer(http.Dir("/srv"))
root, err := bedrock.LocalNixDir()
if err != nil {
log.Fatalf(terrors.Propagate(err).Error())
}

fs := http.FileServer(http.Dir(root + "/srv"))
http.Handle("/", fs)

err = http.ListenAndServe(conf.HttpBind(), nil)
if err != nil {
slog.Error(terrors.Augment(err, "failed to start server", nil).Error())
panic(err)
log.Fatalf(terrors.Augment(err, "failed to start server", nil).Error())
}
}

0 comments on commit b3c5c4c

Please sign in to comment.