-
Notifications
You must be signed in to change notification settings - Fork 100
Portability patch #512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
TheMetallists
wants to merge
1
commit into
la5nta:develop
Choose a base branch
from
TheMetallists:develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Portability patch #512
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,6 +1,9 @@ | ||
| .build/ | ||
| .idea/ | ||
| pat | ||
| pat*.pkg | ||
| docker-data/ | ||
| .aider* | ||
| *.swp | ||
| libportapat/build/*.jar | ||
| libportapat/build/*.aar |
This file contains hidden or 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,52 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "github.com/la5nta/pat/libportapat" | ||
| "log" | ||
| "os" | ||
| "os/signal" | ||
| "path/filepath" | ||
| "syscall" | ||
| ) | ||
|
|
||
| /* | ||
| This file is a launcher for the portable (libportapat) version of PAT. | ||
| It is primarily designed for being embedded into an Android's apk | ||
| package - as a library. | ||
| This package is designed to start this library on a regular PC/SBC, | ||
| for debugging or using PAT as a portable app. | ||
| */ | ||
|
|
||
| func main() { | ||
| log.Println("Starting portapat...") | ||
| libportapat.SetRootPath(getDataDirectory()) | ||
| libportapat.SetRandomizePort(false) | ||
|
|
||
| sigs := make(chan os.Signal, 1) | ||
| signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) | ||
|
|
||
| err := libportapat.Start() | ||
| if err != nil { | ||
| log.Fatalln("Unable to start portapat:", err) | ||
| } | ||
|
|
||
| log.Println("Open your browser and navigate to: ", libportapat.GetUrlForBrowsers()) | ||
| _ = <-sigs | ||
| libportapat.Stop() | ||
| } | ||
|
|
||
| func getDataDirectory() string { | ||
| exePath, err := os.Executable() | ||
| if err != nil { | ||
| log.Fatalln("Failed to get exe path:", err) | ||
| } | ||
| dataDir := filepath.Join(filepath.Dir(exePath), "data") | ||
| if _, err := os.Stat(dataDir); os.IsNotExist(err) { | ||
| err = os.MkdirAll(dataDir, 0755) | ||
| if err != nil { | ||
| log.Fatalln("Failed to create data directory:", err) | ||
| } | ||
| } | ||
|
|
||
| return dataDir | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,2 @@ | ||
| #!/usr/bin/env bash | ||
| gomobile bind -v -target=android .. |
This file contains hidden or 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,66 @@ | ||
| package libportapat | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| cfg2 "github.com/la5nta/pat/cfg" | ||
| "github.com/la5nta/pat/libportapat/porta_paths" | ||
| "log" | ||
| "math/rand" | ||
| "os" | ||
| "strings" | ||
| ) | ||
|
|
||
| func SetRandomizePort(rndport bool) { | ||
| flagRandomisePort = rndport | ||
| } | ||
|
|
||
| func doRandomisePort() error { | ||
| newPort := 48000 + rand.Intn(1000) | ||
| strConfig := ConfigRead() | ||
| var cfg cfg2.Config | ||
|
|
||
| if len(strConfig) < 1 || strings.Contains(strConfig, "!!! ERROR READING CONFIG") { | ||
| cfg = cfg2.DefaultConfig | ||
| } else { | ||
| err := json.Unmarshal([]byte(strConfig), &cfg) | ||
| if err != nil { | ||
| log.Println("Error unmarshalling: ", err) | ||
| cfg = cfg2.DefaultConfig | ||
| } | ||
| } | ||
|
|
||
| cfg.HTTPAddr = fmt.Sprintf("127.0.0.1:%d", newPort) | ||
|
|
||
| bytConfig, err := json.MarshalIndent(&cfg, "", "\t") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return ConfigWrite(string(bytConfig)) | ||
| } | ||
|
|
||
| func ConfigRead() string { | ||
| cfgPath := porta_paths.GetPortaPath(porta_paths.PATH_KIND_CONFIG) | ||
| cfgBinary, err := os.ReadFile(cfgPath) | ||
| if err != nil { | ||
| fmt.Printf("Error reading config file: %s\n", err) | ||
| return "!!! ERROR READING CONFIG: " + err.Error() | ||
| } | ||
| return string(cfgBinary) | ||
| } | ||
|
|
||
| func ConfigWrite(newconf string) error { | ||
| var cfg cfg2.Config | ||
| errc := json.Unmarshal([]byte(newconf), &cfg) | ||
| if errc != nil { | ||
| return errors.New("Format error: " + errc.Error()) | ||
| } | ||
|
|
||
| cfgPath := porta_paths.GetPortaPath(porta_paths.PATH_KIND_CONFIG) | ||
| err := os.WriteFile(cfgPath, []byte(newconf), 0644) | ||
| if err != nil { | ||
| fmt.Printf("Error writing config file: %s\n", err) | ||
| } | ||
| return nil | ||
| } | ||
This file contains hidden or 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,157 @@ | ||
| package libportapat | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "github.com/la5nta/pat/api" | ||
| "github.com/la5nta/pat/app" | ||
| "github.com/la5nta/pat/cfg" | ||
| "github.com/la5nta/pat/cli" | ||
| "github.com/la5nta/pat/internal/buildinfo" | ||
| "github.com/la5nta/pat/libportapat/porta_paths" | ||
| "github.com/la5nta/pat/web_frontend" | ||
| "log" | ||
| "os" | ||
| "runtime" | ||
| "strings" | ||
| "sync" | ||
| ) | ||
|
|
||
| func SetRootPath(rootPath string) { | ||
| porta_paths.SetRootPath(rootPath) | ||
| } | ||
|
|
||
| func prepareOptions(opts *app.Options) { | ||
| opts.MailboxPath = porta_paths.GetPortaPath(porta_paths.PATH_KIND_MAILBOX) | ||
| opts.FormsPath = porta_paths.GetPortaPath(porta_paths.PATH_KIND_FORMS) | ||
| opts.ConfigPath = porta_paths.GetPortaPath(porta_paths.PATH_KIND_CONFIG) | ||
| opts.PrehooksPath = porta_paths.GetPortaPath(porta_paths.PATH_KIND_PREHOOKS) | ||
| opts.LogPath = porta_paths.GetPortaPath(porta_paths.PATH_KIND_LOG) | ||
| opts.EventLogPath = porta_paths.GetPortaPath(porta_paths.PATH_KIND_EVENTLOG) | ||
| } | ||
|
|
||
| var appRunSync sync.Mutex | ||
| var stopChan = make(chan bool) | ||
|
|
||
| var flagRandomisePort = false | ||
|
|
||
| func Start() error { | ||
| fmt.Println("Start", runtime.GOOS) | ||
| if !appRunSync.TryLock() { | ||
| return errors.New("App is already running. If it is not so, try stopping the app via settings/killing the process.") | ||
| } | ||
| api.EmbeddedFS = web_frontend.EmbeddedFS | ||
| if flagRandomisePort { | ||
| eport := doRandomisePort() | ||
| if eport != nil { | ||
| return eport | ||
| } | ||
| } | ||
|
|
||
| go func() { | ||
| var opts app.Options | ||
| prepareOptions(&opts) | ||
|
|
||
| cmd, _, _, _ := cli.FindCommand([]string{"portapat", "http", ""}) | ||
|
|
||
| for runApp(opts, cmd, []string{}) { | ||
| log.Println("App restarted?") | ||
| } | ||
| }() | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func Stop() { | ||
| fmt.Println("Stop") | ||
| stopChan <- true | ||
| appRunSync.Unlock() | ||
| } | ||
|
|
||
| func runApp(opts app.Options, cmd app.Command, args []string) bool { | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
|
|
||
| a := app.New(opts) | ||
| defer a.Close() | ||
|
|
||
| // Graceful shutdown/reload handling. | ||
| shouldReload := make(chan bool, 1) | ||
| done := make(chan struct{}) | ||
| a.OnReload = func() error { | ||
| // Avoid reloading of bad config | ||
| if _, err := app.LoadConfig(opts.ConfigPath, cfg.DefaultConfig); err != nil { | ||
| return fmt.Errorf("bad config: %v", err) | ||
| } | ||
| cancel() | ||
| shouldReload <- true | ||
| return nil | ||
| } | ||
| go func() { | ||
| defer close(shouldReload) | ||
| dirtyDisconnectNext := true // Do the dirty disconnect right away | ||
| for { | ||
| select { | ||
| case _ = <-stopChan: | ||
| if ok := a.AbortActiveConnection(dirtyDisconnectNext); ok { | ||
| dirtyDisconnectNext = !dirtyDisconnectNext | ||
| continue | ||
| } | ||
| cancel() | ||
| shouldReload <- false | ||
| return | ||
| case <-done: | ||
| return | ||
| } | ||
| } | ||
| }() | ||
|
|
||
| // Run the app | ||
| a.Run(ctx, cmd, args) | ||
| close(done) | ||
| return <-shouldReload | ||
| } | ||
|
|
||
| // the app's version consists of the upstream version and the port's code version | ||
| // a.k.a. subverion (indicates both Java and GO changes) | ||
| const PP_SUBVERSION = "0.1" | ||
|
|
||
| func GetVersionStr(title string) string { | ||
| return fmt.Sprintf("%s v%s-%s [%s]", title, buildinfo.Version, PP_SUBVERSION, runtime.GOARCH) | ||
| } | ||
|
|
||
| func GetUrlForBrowsers() string { | ||
| cfgPath := porta_paths.GetPortaPath(porta_paths.PATH_KIND_CONFIG) | ||
| cfgBinary, err := os.ReadFile(cfgPath) | ||
| defUrl := "http://127.0.0.1:8080" | ||
|
|
||
| if err != nil { | ||
| // assuming the config does not exist | ||
| return defUrl | ||
| } | ||
|
|
||
| var config cfg.Config | ||
| err = json.Unmarshal(cfgBinary, &config) | ||
| if err != nil { | ||
| log.Println("Error parsing config file:", err) | ||
| return defUrl | ||
| } | ||
|
|
||
| // handling NULL ipv6 addresses | ||
| if strings.Contains(config.HTTPAddr, "[::]") { | ||
| parts := strings.Split(config.HTTPAddr, ":") | ||
| port := parts[len(parts)-1] | ||
| return "http://[::1]:" + port | ||
| } | ||
|
|
||
| // handling NULL ipv4 addresses | ||
| if strings.Contains(config.HTTPAddr, "0.0.0.0") { | ||
| parts := strings.Split(config.HTTPAddr, ":") | ||
| port := parts[len(parts)-1] | ||
| return "http://127.0.0.1:" + port | ||
| } | ||
|
|
||
| return "http://" + config.HTTPAddr | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of setting the custom port by rewriting the config file, you have two other options:
-addr [host:port]as arguments to thehttpcommand.PAT_HTTPADDR=[host:port](this applies to all config options)By doing this, you don't need to read/write the config file.