Last active
April 8, 2019 06:33
-
-
Save bobbysciacchitano/e417a8db000a07f74eed7fc738976245 to your computer and use it in GitHub Desktop.
Just a basic web server in golang :)
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
package main | |
import ( | |
"context" | |
"encoding/json" | |
"fmt" | |
"net/http" | |
_ "github.com/go-kivik/couchdb" | |
"github.com/go-kivik/kivik" | |
"github.com/gorilla/mux" | |
) | |
// App potato... | |
type App struct { | |
couchDB *kivik.DB | |
} | |
// Query potato... | |
type Query struct { | |
Selector struct { | |
Type struct { | |
EQ string `json:"$eq"` | |
} `json:"type"` | |
} `json:"selector"` | |
} | |
// Media potato... | |
type Media struct { | |
ID string `json:"_id"` | |
Title string `json:"title"` | |
} | |
// Collection potato... | |
type Collection struct { | |
ID string `json:"_id"` | |
Name string `json:"name"` | |
} | |
// Library potato... | |
type Library struct { | |
ID string `json:"_id"` | |
Name string `json:"name"` | |
Slug string `json:"slug"` | |
NamePlural string `json:"namePlural"` | |
} | |
func (app *App) getAllCollections(w http.ResponseWriter, r *http.Request) { | |
query := Query{} | |
query.Selector.Type.EQ = "collection" | |
rows, _ := app.couchDB.Find(context.TODO(), query) | |
dataset := []Collection{} | |
for rows.Next() { | |
collection := Collection{} | |
rows.ScanDoc(&collection) | |
dataset = append(dataset, collection) | |
} | |
formatted, _ := json.Marshal(dataset) | |
fmt.Fprint(w, string(formatted)) | |
} | |
func (app *App) getCollection(w http.ResponseWriter, r *http.Request) { | |
vars := mux.Vars(r) | |
row := app.couchDB.Get(context.TODO(), vars["id"]) | |
collection := Collection{} | |
row.ScanDoc(&collection) | |
formatted, _ := json.Marshal(collection) | |
fmt.Fprint(w, string(formatted)) | |
} | |
func (app *App) getMedia(w http.ResponseWriter, r *http.Request) { | |
vars := mux.Vars(r) | |
row := app.couchDB.Get(context.TODO(), vars["id"]) | |
media := Media{} | |
row.ScanDoc(&media) | |
formatted, _ := json.Marshal(media) | |
fmt.Fprint(w, string(formatted)) | |
} | |
func (app *App) getAllLibraries(w http.ResponseWriter, r *http.Request) { | |
query := Query{} | |
query.Selector.Type.EQ = "library" | |
rows, _ := app.couchDB.Find(context.TODO(), query) | |
dataset := []Library{} | |
for rows.Next() { | |
library := Library{} | |
rows.ScanDoc(&library) | |
dataset = append(dataset, library) | |
} | |
formatted, _ := json.Marshal(dataset) | |
fmt.Fprint(w, string(formatted)) | |
} | |
func (app *App) getLibrary(w http.ResponseWriter, r *http.Request) { | |
vars := mux.Vars(r) | |
row := app.couchDB.Get(context.TODO(), vars["id"]) | |
library := Library{} | |
row.ScanDoc(&library) | |
formatted, _ := json.Marshal(library) | |
fmt.Fprint(w, string(formatted)) | |
} | |
func main() { | |
client, err := kivik.New("couch", "http://localhost:5984") | |
db := client.DB(context.TODO(), "mediabase") | |
if err != nil { | |
panic(err) | |
} | |
app := App{} | |
app.couchDB = db | |
router := mux.NewRouter() | |
router.HandleFunc("/media/{id}", app.getMedia).Methods("GET") | |
router.HandleFunc("/library", app.getAllLibraries).Methods("GET") | |
router.HandleFunc("/library/{id}", app.getLibrary).Methods("GET") | |
//router.HandleFunc("/library/{id}/media", app.potato).Methods("GET") | |
router.HandleFunc("/collection", app.getAllCollections).Methods("GET") | |
router.HandleFunc("/collection/{id}", app.getCollection).Methods("GET") | |
//router.HandleFunc("/collection/{id}/media", app.potato).Methods("GET") | |
http.ListenAndServe(":8000", router) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment