-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added API endpoints for creating and removing proxy * Added documentation and added option to enable or disable api * Use Chi instead of gin as webserver * Move env to main instead of handling it in http_api.go * Moved getEnv function to main * Removed manually registering proxies * Added new method to create file with specific name and moved code to other methods * Removed global variable * Use ProxyConfig to handle JSON unmarshal * Update documentation * Update documentation * Refactor api package * Change StartWebserver to ListenAndServe to be consistent * Changed http status codes * Use Chi URL parameters * Added return statements * Changed http status code and removed fatal log * Removed debug statement * Consistent naming * Removed global variable * Rename http_api.go to api.go * Cleaned up JSON unmarshal * Removed unnecessary type conversion * Simplify if statement
- Loading branch information
Showing
6 changed files
with
188 additions
and
17 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
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,115 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/go-chi/chi/v5" | ||
"github.com/go-chi/chi/v5/middleware" | ||
"github.com/haveachin/infrared" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
// ListenAndServe StartWebserver Start Webserver if environment variable "api-enable" is set to true | ||
func ListenAndServe(configPath string, apiBind string) { | ||
fmt.Println("Starting WebAPI on " + apiBind) | ||
router := chi.NewRouter() | ||
router.Use(middleware.Logger) | ||
|
||
router.Post("/proxies", addProxy(configPath)) | ||
router.Post("/proxies/{fileName}", addProxyWithName(configPath)) | ||
router.Delete("/proxies/{fileName}", removeProxy(configPath)) | ||
|
||
err := http.ListenAndServe(apiBind, router) | ||
if err != nil { | ||
log.Fatal(err) | ||
return | ||
} | ||
} | ||
|
||
func addProxy(configPath string) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
rawData, err := ioutil.ReadAll(r.Body) | ||
if err != nil || string(rawData) == "" { | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
jsonIsValid := checkJSONAndRegister(rawData, "", configPath) | ||
if jsonIsValid { | ||
w.WriteHeader(http.StatusOK) | ||
return | ||
} else { | ||
w.WriteHeader(http.StatusBadRequest) | ||
w.Write([]byte("{'error': 'domainName and proxyTo could not be found'}")) | ||
return | ||
} | ||
} | ||
} | ||
|
||
func addProxyWithName(configPath string) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
fileName := chi.URLParam(r, "fileName") | ||
|
||
rawData, err := ioutil.ReadAll(r.Body) | ||
if err != nil || string(rawData) == "" { | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
jsonIsValid := checkJSONAndRegister(rawData, fileName, configPath) | ||
if jsonIsValid { | ||
w.WriteHeader(http.StatusOK) | ||
return | ||
} else { | ||
w.WriteHeader(http.StatusBadRequest) | ||
w.Write([]byte("{'error': 'domainName and proxyTo could not be found'}")) | ||
return | ||
} | ||
} | ||
} | ||
|
||
func removeProxy(configPath string) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
file := chi.URLParam(r, "fileName") | ||
fmt.Println(file) | ||
|
||
err := os.Remove(configPath + "/" + file) | ||
if err != nil { | ||
w.WriteHeader(http.StatusNoContent) | ||
w.Write([]byte(err.Error())) | ||
return | ||
} | ||
} | ||
} | ||
|
||
// Helper method to check for domainName and proxyTo in a given JSON array | ||
// If the filename is empty the domain will be used as the filename - files with the same name will be overwritten | ||
func checkJSONAndRegister(rawData []byte, filename string, configPath string) (successful bool) { | ||
var cfg infrared.ProxyConfig | ||
err := json.Unmarshal(rawData, &cfg) | ||
if err != nil { | ||
fmt.Println(err) | ||
return false | ||
} | ||
|
||
if cfg.DomainName == "" || cfg.ProxyTo == "" { | ||
return false | ||
} | ||
|
||
path := configPath + "/" + filename | ||
// If fileName is empty use domainName as filename | ||
if filename == "" { | ||
path = configPath + "/" + cfg.DomainName | ||
} | ||
|
||
err = os.WriteFile(path, rawData, 0644) | ||
if err != nil { | ||
fmt.Println(err) | ||
return false | ||
} | ||
|
||
return true | ||
} |
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
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