Created
September 10, 2020 14:34
-
-
Save ghedamat/366369e1e0c6685b75aa2c8dc0787ded to your computer and use it in GitHub Desktop.
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 ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
) | |
type Page struct { | |
Title string | |
Body []byte | |
} | |
type Action struct { | |
Label string `json:"label"` | |
Url string `json:"url"` | |
Verb string `json:"verb"` | |
Context string `json:"context"` | |
ContentTypes []string `json:"content_types"` | |
AccessType string `json:"access_type"` | |
} | |
type Response struct { | |
Name string `json:"name"` | |
Description string `json:"description"` | |
SupportedTypes []string `json:"supported_types"` | |
Actions []Action `json:"actions"` | |
} | |
func loadPage(title string) (*Page, error) { | |
filename := title + ".md" | |
body, err := ioutil.ReadFile(filename) | |
if err != nil { | |
return nil, err | |
} | |
return &Page{Title: title, Body: body}, nil | |
} | |
func handler(w http.ResponseWriter, r *http.Request) { | |
enableCors(&w) | |
fmt.Println(r.URL.Path) | |
fmt.Fprintf(w, "Hi there, I love %s!\n", r.URL.Path[1:]) | |
} | |
func install(w http.ResponseWriter, r *http.Request) { | |
enableCors(&w) | |
//ct := []string{"Note"} | |
//action := Action{"Bullet entry", "http://localhost:8080/bullet", "get", "Item", ct, "decrypted"} | |
response := Response{"sn-bullet-test", "sn bullet test", []string{"Note"}, []Action{}} | |
js, err := json.Marshal(response) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
w.Header().Set("Content-Type", "application/json") | |
w.Write(js) | |
} | |
func enableCors(w *http.ResponseWriter) { | |
(*w).Header().Set("Access-Control-Allow-Origin", "*") | |
(*w).Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE") | |
(*w).Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization") | |
} | |
func main() { | |
p2, _ := loadPage("TestPage") | |
fmt.Println(string(p2.Body)) | |
http.HandleFunc("/", handler) | |
http.HandleFunc("/install", install) | |
//http.HandleFunc("/bullet", bullet) | |
log.Fatal(http.ListenAndServe(":8080", nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment