-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce package struct to align API calls output (#15)
To make sure the different API calls which expose package information (/list, /package/*) have the same content / naming, the package object is introduced. The icon path is added to the detail output to have in the detail view all the information which is also on the top level. Moved handler and to it's own file for better readability. Server now must be started with `go run .` instead of `go run main.go`.
- Loading branch information
Showing
5 changed files
with
163 additions
and
142 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
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,150 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/gorilla/mux" | ||
) | ||
|
||
func downloadHandler(w http.ResponseWriter, r *http.Request) { | ||
vars := mux.Vars(r) | ||
file := vars["name"] | ||
|
||
path := packagesPath + "/" + file + ".zip" | ||
if _, err := os.Stat(path); os.IsNotExist(err) { | ||
log.Println(err) | ||
http.NotFound(w, r) | ||
return | ||
} | ||
|
||
d, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
log.Println(err) | ||
http.NotFound(w, r) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/octet-stream") | ||
w.Header().Set("Content-Description", "File Transfer") | ||
w.Header().Set("Content-Disposition", "attachment; filename=\""+file+".zip\"") | ||
w.Header().Set("Content-Transfer-Encoding", "binary") | ||
|
||
fmt.Fprint(w, string(d)) | ||
} | ||
|
||
func infoHandler() func(w http.ResponseWriter, r *http.Request) { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json") | ||
fmt.Fprintf(w, `{"version": "%s"}`, version) | ||
} | ||
} | ||
|
||
func packageHandler() func(w http.ResponseWriter, r *http.Request) { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json") | ||
vars := mux.Vars(r) | ||
key := vars["name"] | ||
|
||
manifest, err := readManifest(key) | ||
if err != nil { | ||
log.Printf("Manifest not found: %s, %s", key, manifest) | ||
http.NotFound(w, r) | ||
return | ||
} | ||
// It's not set by default, generate it | ||
manifest.Icon = manifest.getIcon() | ||
|
||
data, err := json.MarshalIndent(manifest, "", " ") | ||
if err != nil { | ||
log.Fatal(data) | ||
} | ||
|
||
fmt.Fprint(w, string(data)) | ||
} | ||
} | ||
|
||
func imgHandler(w http.ResponseWriter, r *http.Request) { | ||
vars := mux.Vars(r) | ||
integration := vars["name"] | ||
file := vars["file"] | ||
|
||
img, err := readImage(integration, file) | ||
if err != nil { | ||
http.Error(w, "integration "+integration+" not found", 404) | ||
return | ||
} | ||
|
||
// Package exists but does not have an icon, so the default icon is shipped | ||
if img == nil { | ||
if file == "icon.png" { | ||
img, err = ioutil.ReadFile("./img/icon.png") | ||
if err != nil { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
} else { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
} | ||
|
||
// Safety check for too short paths | ||
if len(file) < 3 { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
|
||
suffix := file[len(file)-3:] | ||
|
||
// Only .png and .jpg are supported at the moment | ||
if suffix == "png" { | ||
w.Header().Set("Content-Type", "image/png") | ||
} else if suffix == "jpg" { | ||
w.Header().Set("Content-Type", "image/jpeg") | ||
} else { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
|
||
fmt.Fprint(w, string(img)) | ||
} | ||
|
||
func listHandler() func(w http.ResponseWriter, r *http.Request) { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
|
||
integrations, err := getIntegrationPackages() | ||
if err != nil { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
|
||
var output []map[string]string | ||
for _, i := range integrations { | ||
m, err := readManifest(i) | ||
if err != nil { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
|
||
data := map[string]string{ | ||
"name": m.Name, | ||
"description": m.Description, | ||
"version": m.Version, | ||
"icon": m.getIcon(), | ||
} | ||
output = append(output, data) | ||
} | ||
j, err := json.MarshalIndent(output, "", " ") | ||
if err != nil { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
w.Header().Set("Content-Type", "application/json") | ||
fmt.Fprint(w, string(j)) | ||
} | ||
} |
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