Initial commit
This commit is contained in:
commit
7536cde428
28
articles.go
Normal file
28
articles.go
Normal file
@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Article type
|
||||
type Article struct {
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
// Articles Collection
|
||||
type Articles []Article
|
||||
|
||||
func getAllArticles() http.Handler {
|
||||
fn := func(w http.ResponseWriter, r *http.Request) {
|
||||
articles := Articles{
|
||||
Article{Title: "Test Title", Description: "Test Description", Content: "Hello content!"},
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(articles)
|
||||
}
|
||||
|
||||
return http.HandlerFunc(fn)
|
||||
}
|
14
main.go
Normal file
14
main.go
Normal file
@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("Hello world!")
|
||||
|
||||
s := Server{
|
||||
port: ":3000",
|
||||
}
|
||||
s.serve()
|
||||
}
|
28
server.go
Normal file
28
server.go
Normal file
@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Server base struct
|
||||
type Server struct {
|
||||
port string
|
||||
}
|
||||
|
||||
func (s *Server) serve() {
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/", s.handle())
|
||||
mux.Handle("/articles", getAllArticles())
|
||||
|
||||
log.Print("Listening on", s.port)
|
||||
log.Fatal(http.ListenAndServe(s.port, mux))
|
||||
}
|
||||
|
||||
func (s *Server) handle() http.Handler {
|
||||
fn := func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte(string("Hello World")))
|
||||
}
|
||||
|
||||
return http.HandlerFunc(fn)
|
||||
}
|
Loading…
Reference in New Issue
Block a user