forked from tangled.org/core
this repo has no description

configgy appview with envvars

also setup nixos module

Changed files
+112 -36
appview
cmd
appview
+2 -2
appview/auth/auth.go
··· 27 PDSEndpoint string 28 } 29 30 - func Make() (*Auth, error) { 31 - store := sessions.NewCookieStore([]byte(appview.SessionCookieSecret)) 32 return &Auth{store}, nil 33 } 34
··· 27 PDSEndpoint string 28 } 29 30 + func Make(secret string) (*Auth, error) { 31 + store := sessions.NewCookieStore([]byte(secret)) 32 return &Auth{store}, nil 33 } 34
+24
appview/config.go
···
··· 1 + package appview 2 + 3 + import ( 4 + "context" 5 + 6 + "github.com/sethvargo/go-envconfig" 7 + ) 8 + 9 + type Config struct { 10 + CookieSecret string `env:"TANGLED_COOKIE_SECRET, default=00000000000000000000000000000000"` 11 + Hostname string `env:"TANGLED_HOSTNAME, default=0.0.0.0"` 12 + Port string `env:"TANGLED_PORT, default=3000"` 13 + DbPath string `env:"TANGLED_DB_PATH, default=appview.db"` 14 + } 15 + 16 + func LoadConfig(ctx context.Context) (*Config, error) { 17 + var cfg Config 18 + err := envconfig.Process(ctx, &cfg) 19 + if err != nil { 20 + return nil, err 21 + } 22 + 23 + return &cfg, nil 24 + }
-2
appview/consts.go
··· 1 package appview 2 3 const ( 4 - SessionCookieSecret = "TODO_CHANGE_ME" 5 SessionName = "appview-session" 6 SessionHandle = "handle" 7 SessionDid = "did" ··· 10 SessionRefreshJwt = "refreshJwt" 11 SessionExpiry = "expiry" 12 SessionAuthenticated = "authenticated" 13 - SqliteDbPath = "appview.db" 14 )
··· 1 package appview 2 3 const ( 4 SessionName = "appview-session" 5 SessionHandle = "handle" 6 SessionDid = "did" ··· 9 SessionRefreshJwt = "refreshJwt" 10 SessionExpiry = "expiry" 11 SessionAuthenticated = "authenticated" 12 )
+6 -4
appview/state/state.go
··· 34 pages *pages.Pages 35 resolver *appview.Resolver 36 jc *jetstream.JetstreamClient 37 } 38 39 - func Make() (*State, error) { 40 - db, err := db.Make(appview.SqliteDbPath) 41 if err != nil { 42 return nil, err 43 } 44 45 - auth, err := auth.Make() 46 if err != nil { 47 return nil, err 48 } 49 50 - enforcer, err := rbac.NewEnforcer(appview.SqliteDbPath) 51 if err != nil { 52 return nil, err 53 } ··· 75 pgs, 76 resolver, 77 jc, 78 } 79 80 return state, nil
··· 34 pages *pages.Pages 35 resolver *appview.Resolver 36 jc *jetstream.JetstreamClient 37 + config *appview.Config 38 } 39 40 + func Make(config *appview.Config) (*State, error) { 41 + db, err := db.Make(config.DbPath) 42 if err != nil { 43 return nil, err 44 } 45 46 + auth, err := auth.Make(config.CookieSecret) 47 if err != nil { 48 return nil, err 49 } 50 51 + enforcer, err := rbac.NewEnforcer(config.DbPath) 52 if err != nil { 53 return nil, err 54 } ··· 76 pgs, 77 resolver, 78 jc, 79 + config, 80 } 81 82 return state, nil
+10 -3
cmd/appview/main.go
··· 1 package main 2 3 import ( 4 "fmt" 5 "log" 6 "log/slog" 7 "net/http" 8 "os" 9 10 "github.com/sotangled/tangled/appview/state" 11 ) 12 13 func main() { 14 15 - slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, nil))) 16 17 - state, err := state.Make() 18 19 if err != nil { 20 log.Fatal(err) 21 } 22 23 - addr := fmt.Sprintf("%s:%d", "localhost", 3000) 24 25 log.Println("starting server on", addr) 26 log.Println(http.ListenAndServe(addr, state.Router()))
··· 1 package main 2 3 import ( 4 + "context" 5 "fmt" 6 "log" 7 "log/slog" 8 "net/http" 9 "os" 10 11 + "github.com/sotangled/tangled/appview" 12 "github.com/sotangled/tangled/appview/state" 13 ) 14 15 func main() { 16 + slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, nil))) 17 18 + c, err := appview.LoadConfig(context.Background()) 19 + if err != nil { 20 + log.Println("failed to load config", "error", err) 21 + return 22 + } 23 24 + state, err := state.Make(c) 25 26 if err != nil { 27 log.Fatal(err) 28 } 29 30 + addr := fmt.Sprintf("%s:%s", c.Hostname, c.Port) 31 32 log.Println("starting server on", addr) 33 log.Println(http.ListenAndServe(addr, state.Router()))
+70 -25
flake.nix
··· 43 }); 44 inherit (gitignore.lib) gitignoreSource; 45 in { 46 - overlays.default = final: prev: { 47 indigo-lexgen = with final; 48 final.buildGoModule { 49 pname = "indigo-lexgen"; ··· 68 ''; 69 doCheck = false; 70 subPackages = ["cmd/appview"]; 71 - vendorHash = "sha256-ywhhGrv8KNqy9tCMCnA1PU/RQ/+0Xyitej1L48TcFvI="; 72 env.CGO_ENABLED = 1; 73 stdenv = pkgsStatic.stdenv; 74 }; 75 knotserver = with final; 76 final.pkgsStatic.buildGoModule { 77 pname = "knotserver"; 78 version = "0.1.0"; 79 src = gitignoreSource ./.; 80 subPackages = ["cmd/knotserver"]; 81 - vendorHash = "sha256-ywhhGrv8KNqy9tCMCnA1PU/RQ/+0Xyitej1L48TcFvI="; 82 env.CGO_ENABLED = 1; 83 }; 84 - repoguard = with final; 85 - final.pkgsStatic.buildGoModule { 86 - pname = "repoguard"; 87 - version = "0.1.0"; 88 - src = gitignoreSource ./.; 89 - subPackages = ["cmd/repoguard"]; 90 - vendorHash = "sha256-ywhhGrv8KNqy9tCMCnA1PU/RQ/+0Xyitej1L48TcFvI="; 91 - env.CGO_ENABLED = 0; 92 - }; 93 - keyfetch = with final; 94 - final.pkgsStatic.buildGoModule { 95 - pname = "keyfetch"; 96 - version = "0.1.0"; 97 - src = gitignoreSource ./.; 98 - subPackages = ["cmd/keyfetch"]; 99 - vendorHash = "sha256-ywhhGrv8KNqy9tCMCnA1PU/RQ/+0Xyitej1L48TcFvI="; 100 - env.CGO_ENABLED = 0; 101 - }; 102 }; 103 packages = forAllSystems (system: { 104 inherit (nixpkgsFor."${system}") indigo-lexgen appview knotserver repoguard keyfetch; ··· 123 pkgs.tailwindcss 124 ]; 125 shellHook = '' 126 - cp -f ${htmx-src} appview/pages/static/htmx.min.js 127 - cp -f ${lucide-src} appview/pages/static/lucide.min.js 128 - cp -f ${ia-fonts-src}/"iA Writer Quattro"/Static/*.ttf appview/pages/static/fonts/ 129 - cp -f ${ia-fonts-src}/"iA Writer Mono"/Static/*.ttf appview/pages/static/fonts/ 130 ''; 131 }; 132 }); ··· 150 program = ''${air-watcher "knotserver"}/bin/run''; 151 }; 152 }); 153 }; 154 }
··· 43 }); 44 inherit (gitignore.lib) gitignoreSource; 45 in { 46 + overlays.default = final: prev: let 47 + goModHash = "sha256-ywhhGrv8KNqy9tCMCnA1PU/RQ/+0Xyitej1L48TcFvI="; 48 + buildCmdPackage = name: 49 + final.buildGoModule { 50 + pname = name; 51 + version = "0.1.0"; 52 + src = gitignoreSource ./.; 53 + subPackages = ["cmd/${name}"]; 54 + vendorHash = goModHash; 55 + env.CGO_ENABLED = 0; 56 + }; 57 + in { 58 indigo-lexgen = with final; 59 final.buildGoModule { 60 pname = "indigo-lexgen"; ··· 79 ''; 80 doCheck = false; 81 subPackages = ["cmd/appview"]; 82 + vendorHash = goModHash; 83 env.CGO_ENABLED = 1; 84 stdenv = pkgsStatic.stdenv; 85 }; 86 + 87 knotserver = with final; 88 final.pkgsStatic.buildGoModule { 89 pname = "knotserver"; 90 version = "0.1.0"; 91 src = gitignoreSource ./.; 92 subPackages = ["cmd/knotserver"]; 93 + vendorHash = goModHash; 94 env.CGO_ENABLED = 1; 95 }; 96 + repoguard = buildCmdPackage "repoguard"; 97 + keyfetch = buildCmdPackage "keyfetch"; 98 }; 99 packages = forAllSystems (system: { 100 inherit (nixpkgsFor."${system}") indigo-lexgen appview knotserver repoguard keyfetch; ··· 119 pkgs.tailwindcss 120 ]; 121 shellHook = '' 122 + cp -f ${htmx-src} appview/pages/static/htmx.min.js 123 + cp -f ${lucide-src} appview/pages/static/lucide.min.js 124 + cp -f ${ia-fonts-src}/"iA Writer Quattro"/Static/*.ttf appview/pages/static/fonts/ 125 + cp -f ${ia-fonts-src}/"iA Writer Mono"/Static/*.ttf appview/pages/static/fonts/ 126 ''; 127 }; 128 }); ··· 146 program = ''${air-watcher "knotserver"}/bin/run''; 147 }; 148 }); 149 + 150 + nixosModules.default = { 151 + config, 152 + pkgs, 153 + lib, 154 + ... 155 + }: 156 + with lib; { 157 + options = { 158 + services.tangled-appview = { 159 + enable = mkOption { 160 + type = types.bool; 161 + default = false; 162 + description = "Enable tangled appview"; 163 + }; 164 + port = mkOption { 165 + type = types.int; 166 + default = 3000; 167 + description = "Port to run the appview on"; 168 + }; 169 + cookie_secret = mkOption { 170 + type = types.str; 171 + default = "00000000000000000000000000000000"; 172 + description = "Cookie secret"; 173 + }; 174 + }; 175 + }; 176 + 177 + config = mkIf config.services.tangled-appview.enable { 178 + nixpkgs.overlays = [self.overlays.default]; 179 + systemd.services.tangled-appview = { 180 + description = "tangled appview service"; 181 + wantedBy = ["multi-user.target"]; 182 + 183 + serviceConfig = { 184 + ListenStream = "0.0.0.0:${toString config.services.tangled-appview.port}"; 185 + ExecStart = "${pkgs.tangled-appview}/bin/tangled-appview"; 186 + Restart = "always"; 187 + }; 188 + 189 + environment = { 190 + TANGLED_PORT = "${toString config.services.tangled-appview.port}"; 191 + TANGLED_HOST = "localhost"; 192 + TANGLED_DB_PATH = "appview.db"; 193 + TANGLED_COOKIE_SECRET = config.services.tangled-appview.cookie_secret; 194 + }; 195 + }; 196 + }; 197 + }; 198 }; 199 }