annotate backend/main.go @ 0:a17a4894f4bd draft default tip

Initial commit converting git to mercurial
author Alfred Burgess <aburgess@ucc.asn.au>
date Sun, 14 Apr 2024 19:35:23 +0800
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
1 package main
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
2
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
3 import (
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
4 "os"
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
5 "fmt"
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
6 "errors"
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
7 "html/template"
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
8 "log"
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
9 "net/http"
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
10 "regexp"
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
11 )
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
12
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
13 type ApiReq struct {
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
14 endpoint string
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
15 }
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
16
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
17 type Page struct {
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
18 Title string
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
19 Content []byte
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
20 }
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
21
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
22 func (p *Page) save() error {
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
23 filename := p.Title + ".txt"
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
24 return os.WriteFile(filename, p.Content, 0600)
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
25 }
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
26
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
27 func loadPage(title string) ( *Page, error ) {
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
28 filename := title + ".txt"
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
29 body, err := os.ReadFile(filename)
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
30 if err != nil {
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
31 return nil, err
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
32 }
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
33 return &Page{Title: title, Content: body}, nil
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
34 }
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
35
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
36 func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
37 t, err := template.ParseFiles( "views/" + tmpl + ".html" )
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
38 if err != nil {
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
39 http.Error( w, err.Error(), http.StatusInternalServerError )
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
40 return
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
41 }
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
42 err = t.Execute( w, p )
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
43 if err != nil {
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
44 http.Error( w, err.Error(), http.StatusInternalServerError )
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
45 return
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
46 }
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
47 }
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
48
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
49 func apiHandler(w http.ResponseWriter, r *http.Request) {
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
50 endpoint, _ := getTitle( w, r )
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
51 log.Printf(`API Request: %s`, endpoint )
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
52 }
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
53
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
54 func errorViewHandler(w http.ResponseWriter, r *http.Request) {
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
55 title, _ := getTitle( w, r )
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
56 log.Printf( `Rendering error page: %s`, title )
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
57 p := &Page{ Title: title }
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
58 renderTemplate( w, "error", p )
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
59 }
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
60 func viewHandler(w http.ResponseWriter, r *http.Request) {
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
61 title, err := getTitle( w, r )
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
62 if err != nil { title = "404" }
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
63 log.Printf( `Rendering page: %s`, title )
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
64 p, err := loadPage( title )
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
65 if err != nil {
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
66 http.Redirect( w, r, "/error/404", http.StatusFound )
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
67 return
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
68 }
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
69 renderTemplate( w, "test", p )
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
70 }
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
71
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
72 var validPath = regexp.MustCompile( "^/(view|error|api)/([a-zA-Z0-9]+)$" )
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
73 func getTitle( w http.ResponseWriter, r *http.Request ) ( string, error ) {
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
74 m := validPath.FindStringSubmatch( r.URL.Path )
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
75 if m == nil {
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
76 http.NotFound( w, r )
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
77 return "", errors.New("Unsupported Route")
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
78 }
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
79 return m[2], nil
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
80 }
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
81
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
82 func main() {
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
83 http.HandleFunc("/view/", viewHandler)
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
84 http.HandleFunc("/error/", errorViewHandler)
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
85 http.HandleFunc("/api/", apiHandler)
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
86
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
87 fmt.Printf("Server Running")
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
88
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
89 log.Fatal(http.ListenAndServe(":8080", nil))
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
90 }
a17a4894f4bd Initial commit converting git to mercurial
Alfred Burgess <aburgess@ucc.asn.au>
parents:
diff changeset
91