diff --git a/README.md b/README.md index 3de07fc..ef8e70b 100644 --- a/README.md +++ b/README.md @@ -8,14 +8,19 @@ https://diyhrt.market/api/ 2. Run air ```sh -air +air dev ``` +## Config and Profiles + +This tool works with profiles. The default profile is `startpage`. If you want to load another profile just write it as command line arg after the command. To write a config File you can create the files here: + +- `{profile}.toml` +- `.{profile}.toml` +- `~/.config/startpage/{profile}.toml` + ## TODO -- implement regular fetching -- implement starting it on a certain port depending on command line arg -- implement config file - implement ctl - implement fetching in intervals - actually building and figuring out how I should do that diff --git a/dev.toml b/dev.toml new file mode 100644 index 0000000..795b0a4 --- /dev/null +++ b/dev.toml @@ -0,0 +1,2 @@ +[Server] +Port = 1234 diff --git a/internal/rendering/config.go b/internal/rendering/config.go index 96a21ae..8f3dbdd 100644 --- a/internal/rendering/config.go +++ b/internal/rendering/config.go @@ -13,7 +13,11 @@ import ( "github.com/pelletier/go-toml" ) -type RenderingConfig struct { +type ServerConfig struct { + Port int +} + +type TemplateConfig struct { HeaderPhrases []string BackgroundScrollX string BackgroundScrollY string @@ -29,36 +33,46 @@ type RenderingConfig struct { Stores []diyhrt.Store } -func DefaultRenderingConfig() RenderingConfig { - return RenderingConfig{ - HeaderPhrases: []string{ - "GirlJuice.Inject();", - "Child.CrowdKill();", - "CopCar.Burn();", - "You.Cute = true;", - "You.Gay = true;", - "Nazi.Punch();", - "Dolls.GiveGuns();", - }, - BackgroundScrollX: "1", - BackgroundScrollY: "0", - PageTitle: "TransRights", - SearchPlaceholder: "Search on DuckDuckGo", - SearchFormAction: "https://duckduckgo.com/", - SearchInputName: "q", +type Config struct{ + Server ServerConfig + Template TemplateConfig +} - StoreFilter: diyhrt.StoreFilter{ - Limit: 0, - IncludeIds: []int{7}, +func NewConfig() Config{ + return Config{ + Server: ServerConfig{ + Port: 5500, }, + Template: TemplateConfig{ + HeaderPhrases: []string{ + "GirlJuice.Inject();", + "Child.CrowdKill();", + "CopCar.Burn();", + "You.Cute = true;", + "You.Gay = true;", + "Nazi.Punch();", + "Dolls.GiveGuns();", + }, + BackgroundScrollX: "1", + BackgroundScrollY: "0", + PageTitle: "TransRights", + SearchPlaceholder: "Search on DuckDuckGo", + SearchFormAction: "https://duckduckgo.com/", + SearchInputName: "q", - ListingFilter: diyhrt.ListingFilter{ - FromStores: []int{7}, + StoreFilter: diyhrt.StoreFilter{ + Limit: 0, + IncludeIds: []int{7}, + }, + + ListingFilter: diyhrt.ListingFilter{ + FromStores: []int{7}, + }, }, } } -func (rc *RenderingConfig) LoadDiyHrt(listings []diyhrt.Listing) { +func (rc *TemplateConfig) LoadDiyHrt(listings []diyhrt.Listing) { existingStores := make(map[int]diyhrt.Store) for _, listing := range listings { @@ -70,7 +84,7 @@ func (rc *RenderingConfig) LoadDiyHrt(listings []diyhrt.Listing) { } -func (rc *RenderingConfig) ScanForConfigFile(profile string) error { +func (rc *Config) ScanForConfigFile(profile string) error { profileFile := profile + ".toml" configPath := configdir.LocalConfig("startpage") @@ -91,7 +105,7 @@ func (rc *RenderingConfig) ScanForConfigFile(profile string) error { return errors.New("No config file found") } -func (rc *RenderingConfig) LoadConfigFile(file string) error { +func (rc *Config) LoadConfigFile(file string) error { if _, err := os.Stat(file); err != nil { return err } diff --git a/main.go b/main.go index 3253e95..d83bbb1 100644 --- a/main.go +++ b/main.go @@ -4,13 +4,17 @@ import ( "bytes" "fmt" "net/http" + "os" + "strconv" "gitea.elara.ws/Hazel/transfem-startpage/internal/diyhrt" "gitea.elara.ws/Hazel/transfem-startpage/internal/rendering" "github.com/labstack/echo/v4" ) -var CurrentRenderingConfig = rendering.DefaultRenderingConfig() + +var CurrentConfig = rendering.NewConfig() + func FetchDiyHrt() error { fmt.Println("Fetch DiyHrt Marketplaces...") @@ -19,22 +23,13 @@ func FetchDiyHrt() error { if err != nil { return err } - CurrentRenderingConfig.LoadDiyHrt(l) + CurrentConfig.Template.LoadDiyHrt(l) return nil } -func setConfig(c echo.Context) error { - err := c.Bind(&CurrentRenderingConfig) - if err != nil { - return c.String(http.StatusBadRequest, err.Error()) - } - - return c.String(http.StatusOK, "OK") -} - func getIndex(c echo.Context) error { var tpl bytes.Buffer - rendering.IndexTemplate.Execute(&tpl, CurrentRenderingConfig) + rendering.IndexTemplate.Execute(&tpl, CurrentConfig.Template) return c.HTML(http.StatusOK, tpl.String()) } @@ -42,9 +37,18 @@ func getIndex(c echo.Context) error { func main() { fmt.Println("running transfem startpage") - CurrentRenderingConfig.ScanForConfigFile("startpage") + profile := "startpage" + if len(os.Args) > 1 { + profile = os.Args[1] + } + fmt.Println("loading profile " + profile + "...") - err := FetchDiyHrt() + err := CurrentConfig.ScanForConfigFile(profile) + if err != nil { + fmt.Println(err) + } + + err = FetchDiyHrt() if err != nil { fmt.Println(err) } @@ -54,8 +58,5 @@ func main() { e.Static("/scripts", "frontend/scripts") e.GET("/", getIndex) - // this is for me to later setup the ctl such that I can config the running program on the command line - e.POST("/api/config", setConfig) - - e.Logger.Fatal(e.Start(":5500")) + e.Logger.Fatal(e.Start(":" + strconv.Itoa(CurrentConfig.Server.Port))) } diff --git a/startpage.toml b/startpage.toml deleted file mode 100644 index 7fd846c..0000000 --- a/startpage.toml +++ /dev/null @@ -1,6 +0,0 @@ -HeaderPhrases = [ - "NewTab.SFW = true;", - "You.Cute = true;", - "You.Gay = true;", - "Dolls.Headpat();", -]