-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): added new endpoints /api/v1/settings - getting and update …
…config file
- Loading branch information
Showing
6 changed files
with
316 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package handler | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/wittano/komputer/pkgs/settings" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
func UpdateSettings(w http.ResponseWriter, r *http.Request) error { | ||
defer r.Body.Close() | ||
|
||
rawBody, err := io.ReadAll(r.Body) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var newSetting settings.Settings | ||
if err = json.Unmarshal(rawBody, &newSetting); err != nil { | ||
return err | ||
} | ||
|
||
return settings.Config.Update(newSetting) | ||
} | ||
|
||
func GetSettings(w http.ResponseWriter, _ *http.Request) error { | ||
data, err := json.Marshal(settings.Config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
w.WriteHeader(http.StatusOK) | ||
_, err = w.Write(data) | ||
|
||
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 |
---|---|---|
@@ -1,20 +1,32 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"github.com/wittano/komputer/api/handler" | ||
"github.com/wittano/komputer/pkgs/settings" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
func main() { | ||
configPath := flag.String("config", settings.DefaultSettingsPath, "Path to web console configuration file") | ||
flag.Parse() | ||
|
||
if err := settings.Load(*configPath); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
v1 := http.NewServeMux() | ||
v1.HandleFunc("POST /v1/audio", handler.MakeHttpHandler(handler.UploadNewAudio)) | ||
v1.HandleFunc("POST /api/v1/audio", handler.MakeHttpHandler(handler.UploadNewAudio)) | ||
v1.HandleFunc("GET /api/v1/setting", handler.MakeHttpHandler(handler.GetSettings)) | ||
v1.HandleFunc("PUT /api/v1/setting", handler.MakeHttpHandler(handler.UpdateSettings)) | ||
|
||
server := http.Server{ | ||
Addr: ":8080", | ||
Handler: v1, | ||
} | ||
defer server.Close() | ||
|
||
log.Println("Server listening on 8080") | ||
log.Fatal(server.ListenAndServe()) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package settings | ||
|
||
import ( | ||
"errors" | ||
"github.com/mitchellh/go-homedir" | ||
"gopkg.in/yaml.v3" | ||
"os" | ||
"path/filepath" | ||
"sync" | ||
) | ||
|
||
const ( | ||
DefaultAssertDir = ".cache/komputer" | ||
DefaultSettingsPath = ".config/komputer/settings.yml" | ||
) | ||
|
||
const maxFileSize = 8 * (1 << 20) // 8MB in bytes | ||
|
||
type UploadSettings struct { | ||
MaxFileCount uint `yaml:"max_file_count" json:"max_file_count"` | ||
MaxFileSize uint `yaml:"max_file_size" json:"max_file_size"` | ||
} | ||
|
||
type Settings struct { | ||
AssetDir string `yaml:"asset_dir" json:"asset_dir"` | ||
Upload UploadSettings `yaml:"upload" json:"upload"` | ||
} | ||
|
||
func (s *Settings) Update(new Settings) error { | ||
if new.AssetDir != "" && s.AssetDir != new.AssetDir { | ||
if err := os.MkdirAll(new.AssetDir, 0700); err != nil { | ||
return err | ||
} | ||
|
||
err := moveAssets(s.AssetDir, new.AssetDir) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
s.AssetDir = new.AssetDir | ||
} | ||
|
||
if new.Upload.MaxFileCount != 0 && s.Upload.MaxFileCount != new.Upload.MaxFileCount { | ||
s.Upload.MaxFileCount = new.Upload.MaxFileCount | ||
} | ||
|
||
if new.Upload.MaxFileSize != 0 && s.Upload.MaxFileSize != new.Upload.MaxFileSize { | ||
s.Upload.MaxFileSize = new.Upload.MaxFileSize | ||
} | ||
|
||
return nil | ||
} | ||
|
||
var Config *Settings | ||
|
||
func Load(path string) error { | ||
if Config != nil { | ||
return nil | ||
} | ||
|
||
home, err := homedir.Dir() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
settingPath := filepath.Join(home, DefaultSettingsPath) | ||
if path != "" { | ||
settingPath = filepath.Join(path) | ||
} | ||
|
||
if _, err := os.Stat(settingPath); errors.Is(err, os.ErrNotExist) { | ||
Config, err = defaultSettings(settingPath) | ||
return err | ||
} | ||
|
||
f, err := os.Open(settingPath) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
d := yaml.NewDecoder(f) | ||
if err = d.Decode(&Config); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func defaultSettings(path string) (*Settings, error) { | ||
f, err := os.Create(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
|
||
defaultSettings := Settings{ | ||
AssetDir: DefaultAssertDir, | ||
Upload: UploadSettings{ | ||
MaxFileCount: 5, | ||
MaxFileSize: maxFileSize, | ||
}, | ||
} | ||
|
||
e := yaml.NewEncoder(f) | ||
defer e.Close() | ||
if err = e.Encode(&defaultSettings); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &defaultSettings, nil | ||
} | ||
|
||
func moveAssets(oldSrc string, path string) (err error) { | ||
dirs, err := os.ReadDir(oldSrc) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(len(dirs)) | ||
|
||
for _, dir := range dirs { | ||
go func(wg *sync.WaitGroup, oldSrc string, file os.DirEntry) { | ||
defer wg.Done() | ||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
filename := filepath.Join(oldSrc, file.Name()) | ||
newPath := filepath.Join(path, filepath.Base(filename)) | ||
if err = os.Rename(filename, newPath); err != nil { | ||
return | ||
} | ||
}(&wg, oldSrc, dir) | ||
} | ||
|
||
wg.Wait() | ||
|
||
return | ||
} |
Oops, something went wrong.