Hello World
Let's create a simple Go server, first let's setup the server and do hello world
// @filename: main.go
package main
import (
"fmt"
"net/http"
)
func handleHello (res http.ResponseWriter, req *http.Request) {
// Covert to []bytpe type
res.Write([]byte("Hello from a Go program"))
}
func main {
server := http.NewServeMux()
server.HandleFunc("/hello", handleHello)
err := http.ListenAndServe(":3333", server)
if err != nil {
fmt.Println("Server cannot start")
}
}
After run go run .
, you can visit localhost:3333/hello
to see the server is running and return hello world.
Static HTML
Now let's serve a static HTML, let say we have a public
folder with an index.html
file inside.
We want to serve the public files on our serve
// @filename: main.go
package main
import (
"fmt"
"net/http"
)
func handleHello (res http.ResponseWriter, req *http.Request) {
// Covert to []bytpe type
res.Write([]byte("Hello from a Go program"))
}
func main {
server := http.NewServeMux()
server.HandleFunc("/hello", handleHello)
// Serve public files
fs := http.FileServer(http.Dir("./public"))
server.Handle("/", fs)
err := http.ListenAndServe(":3333", server)
if err != nil {
fmt.Println("Server cannot start")
}
}
Rerun the server, visit localhost:3333
you should be able to see the index.html file
Data Layer
// @filename: data/exhibitions.go
package data
type Exhibition struct {
Title string
Description string
Image string
CurrentlyOpened bool
}
var list = []Exhibition{
{
Title: "Life in Ancient Greek",
Description: "Uncover the world of ancient Greece through the sculptures, tools, and jewelry found in ruins from over 2000 years ago that have been unearthed through modern science and technology.",
Image: "ancient-greece.png",
CurrentlyOpened: false,
},
{
Title: "Aristotle: Life and Legacy",
Description: "Explore the life and legacy of the great philosopher Aristotle, one of the most influential thinkers of all time. Through rare artifacts and ancient texts, learn about his ideas on ethics, politics, and metaphysics that have shaped the world for centuries.",
Image: "aristotle.png",
CurrentlyOpened: true,
},
}
func GetAll() []Exhibition {
return list
}
func Add(exb Exhibition) {
list = append(list, exb)
}
We can test the data layer by add two api endpoints
GET: /api/exhibitions
POST: /api/exhibitions/new
API Layer
// @filename: api/get.go
package api
import (
"encoding/json"
"net/http"
"strconv"
"project/museum/data"
)
func Get(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
// /api/exhibitions?id=1
id := r.URL.Query()["id"]
if id != nil {
finalId, err := strconv.Atoi(id[0]) // parseInt
if err == nil && finalId < len(data.GetAll()) {
// Send one item back to Client using json encoder
json.NewEncoder(w).Encode(data.GetAll()[finalId])
} else {
http.Error(w, "Invalid Exhibition", http.StatusBadRequest)
}
} else {
// Send all item to client
json.NewEncoder(w).Encode(data.GetAll())
}
}
// @filename: api/post.go
package api
import (
"encoding/json"
"net/http"
"project/museum/data"
)
func Post(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
var item data.Exhibition
// Read data from client
err := json.NewDecoder(r.Body).Decode(&item)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
data.Add(item)
w.WriteHeader(http.StatusCreated)
w.Write(([]byte("OK")))
} else {
http.Error(w, "Unsupported Method", http.StatusMethodNotAllowed)
}
}
Now we can link those two api in our server
// @filename: main.go
package main
import (
"fmt"
"net/http"
"project/museum/api"
"project/museum/data"
)
func handleHello (res http.ResponseWriter, req *http.Request) {
// Covert to []bytpe type
res.Write([]byte("Hello from a Go program"))
}
func main {
server := http.NewServeMux()
server.HandleFunc("/hello", handleHello)
// Get and Post
server.handleFunc("/api/exhibitions", api.Get)
server.handleFunc("/api/exhibitions", api.Post)
// Serve public files
fs := http.FileServer(http.Dir("./public"))
server.Handle("/", fs)
err := http.ListenAndServe(":3333", server)
if err != nil {
fmt.Println("Server cannot start")
}
}
Rerun the server, then you can use POSTMAN to check apis.
Template
Now we want to display the data in our client by using template, so that we can do server side rendering which is more performance compare to Client side data fetching + rendering
// @filename: templates/index.tmpl
<!DOCTYPE html>
<html lang="en">
<head>
...
</head>
<body>
<main>
{{ range .}}
<article
class="{{- if .CurrentlyOpened -}} opened {{- else -}} closed {{- end -}}"
>
<h2>{{ .Title }}</h2>
<p>
{{ .Description }}
</p>
<img src="gallery/{{ .Image }}" fetchpriority="high" decoding="sync" />
</article>
{{ end }}
</main>
</body>
</html>
So .
means the whole data, because we will return an array of object, therefore we use range
to foreach object, so the .
represent each object
And you can see, it support logic condition {{ if cond }} A {{ else }} B {{ end }}
If you want to remove all the whie space, you can use -
, then it becomes {{- if .CurrentlyOpened -}} opened {{- else -}} closed {{- end -}}
Let's serve the template
// @filename: main.go
package main
import (
"fmt"
"net/http"
"text/template"
"project/museum/api"
"project/museum/data"
)
func handleHello (res http.ResponseWriter, req *http.Request) {
res.Write([]byte("Hello from a Go program"))
}
func handleTemplate(res http.ResponseWriter, req *http.Request) {
html, err := template.ParseFiles("templates/index.tmpl")
if err != nil {
res.WriteHeader(http.StatusInternalServerError)
res.Write([]byte("Internal Server Error"))
return
}
// Send data to template
html.Execute(res, data.GetAll())
}
func main() {
// Create a route handler
server := http.NewServeMux()
server.HandleFunc("/hello", handleHello)
// Get and Post
server.HandleFunc("/api/exhibitions", api.Get)
server.HandleFunc("/api/exhibitions/new", api.Post)
// template
server.HandleFunc("/templates", handleTemplate)
// Server public files
fs := http.FileServer(http.Dir("./public"))
server.Handle("/", fs)
err := http.ListenAndServe(":3333", server)
if err != nil {
fmt.Println("Server cannot start")
}
}
DONE. If you change template and css, js you don't need to restart the Go server
标签:http,err,simple,server,api,func,Go,data From: https://www.cnblogs.com/Answer1215/p/18012506