Created
July 2, 2018 22:12
-
-
Save DazWilkin/a278939c50ed32b504bdf6c02b12ad8a to your computer and use it in GitHub Desktop.
Kubernetes Deployment Dependencies
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
package main | |
import ( | |
"encoding/json" | |
"errors" | |
"fmt" | |
"io" | |
"log" | |
"net/http" | |
"net/url" | |
"os" | |
"path/filepath" | |
) | |
const ( | |
defaultRoot = "." | |
defaultPort = "8080" | |
) | |
var ( | |
env = map[string]string{ | |
"path": "SIMPLE_CONFIG_PATH", | |
"port": "SIMPLE_CONFIG_PORT", | |
} | |
root string | |
port string | |
) | |
type Config struct { | |
Variable string `json:"variable"` | |
} | |
// Constructors | |
func ConfigFromBody(body io.ReadCloser) (Config, error) { | |
config := &Config{} | |
decoder := json.NewDecoder(body) | |
err := decoder.Decode(config) | |
return *config, err | |
} | |
func ConfigFromValues(values url.Values) (Config, error) { | |
config := &Config{} | |
var err error | |
variable := values.Get("variable") | |
if variable == "" { | |
log.Println("Parameter 'variable' missing") | |
err = errors.New("Parameter 'variable' missing") | |
} else { | |
log.Printf("Variable: %s\n", variable) | |
config.Variable = variable | |
} | |
return *config, err | |
} | |
func handler(w http.ResponseWriter, r *http.Request) { | |
switch r.Method { | |
case "GET": | |
log.Println("GET") | |
// Expect ..?variable=[path/to/file] | |
config, err := ConfigFromValues(r.URL.Query()) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusBadRequest) | |
return | |
} | |
log.Printf("variable=%s\n", config.Variable) | |
fullFilename := fmt.Sprintf("%s/%s", root, config.Variable) | |
log.Printf("Checking: %s\n", fullFilename) | |
_, err = os.Stat(fullFilename) | |
if err != nil { | |
if os.IsNotExist(err) { | |
log.Println("File expected but does not exist") | |
http.Error(w, err.Error(), http.StatusNotFound) | |
return | |
} | |
if os.IsExist(err) { | |
log.Println("Ok") | |
fmt.Fprintf(w, "Ok") | |
return | |
} | |
// Else | |
log.Println(err) | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
} | |
case "POST": | |
log.Println("POST") | |
config, err := ConfigFromBody(r.Body) | |
if err != nil { | |
log.Println(err) | |
} | |
log.Printf("variable=%s\n", config.Variable) | |
// Variable of the form "henry", "/henry" or "some/path/to/henry" | |
// Grab the Variable's path i.e. ".", "/", or "./some/path/to" | |
path := filepath.Dir(config.Variable) | |
// If needed | |
if path != "" { | |
// Create the new path by concat'ing it with the rootPath | |
fullPath := fmt.Sprintf("%s/%s", root, path) | |
log.Printf("Creating path: %s\n", fullPath) | |
err = os.MkdirAll(fullPath, os.ModePerm) | |
if err != nil { | |
log.Println(err) | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
} | |
// Variable still contains its path and filename | |
log.Printf("Creating: %s\n", config.Variable) | |
fullFilename := fmt.Sprintf("%s/%s", root, config.Variable) | |
log.Printf("Checking: %s\n", fullFilename) | |
_, err = os.Stat(fullFilename) | |
if err != nil { | |
if os.IsNotExist(err) { | |
log.Println("File does not exist, creating") | |
file, err := os.Create(fullFilename) | |
if err != nil { | |
log.Println(err) | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
defer file.Close() | |
log.Println("Ok") | |
fmt.Fprintln(w, "Ok") | |
return | |
} | |
if os.IsExist(err) { | |
log.Println("File already exists, abandoning") | |
http.Error(w, err.Error(), http.StatusConflict) | |
return | |
} | |
// Else | |
log.Println(err) | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
} | |
} | |
} | |
func init() { | |
// Check for path override in environment | |
root = os.Getenv(env["path"]) | |
if root == "" { | |
root = defaultRoot | |
} | |
log.Printf("root: %s\n", root) | |
// Check for port override in environment | |
port = os.Getenv(env["port"]) | |
if port == "" { | |
port = defaultPort | |
} | |
log.Printf("port: %s", port) | |
} | |
func main() { | |
http.HandleFunc("/", handler) | |
log.Fatal( | |
http.ListenAndServe( | |
fmt.Sprintf(":%v", port), | |
nil, | |
)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment