/
smushfile.go
185 lines (154 loc) · 4.45 KB
/
smushfile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"http"
"log"
"template"
"io"
"strings"
"os"
"url"
"time"
"strconv"
)
var tmpls *template.Set
const compilerURL = "http://closure-compiler.appspot.com/compile"
type Page struct {
Title string
Data string
}
func ReadWholeFile( f io.ReadCloser ) ( r string, err os.Error ) {
defer f.Close()
const NBUF = 512
var buf [ NBUF ]byte
for {
switch nr, err := f.Read( buf[:] ); true {
case nr < 0:
log.Printf( "Error reading remote file: %v", err.String() )
r = ""
err = os.NewError( "Error reading remote file" )
return
case nr == 0: // EOF
return
case nr > 0:
r += string( buf[ 0:nr ] )
}
}
return
}
func RequireParams( request *http.Request, params []string ) ( success bool ){
err := request.ParseForm()
if err != nil {
return false
}
for _, v := range params{
if thisParam, exists := request.Form[ v ]; exists{
hasGoodValue := false;
for _, paramValue := range thisParam{
if len( paramValue ) > 0{
hasGoodValue = true
break;
}
}
if !hasGoodValue{
return false
}
}else{
return false
}
}
return true
}
func SmushIndex( response http.ResponseWriter, request *http.Request ){
var page = Page{
Title: "Index",
}
err := tmpls.Execute( response, "index", page )
if err != nil {
log.Fatal( "Couldn't execute index template: ", err.String() )
}
}
func SmushFiles( response http.ResponseWriter, request *http.Request ){
var requiredParams = []string{ "name", "source" }
if request.Method != "POST" {
http.Error( response, "Must post from main Smush form", http.StatusInternalServerError )
return
}
if !RequireParams( request, requiredParams ){
http.Error( response, "Missing required params", http.StatusInternalServerError )
return
}
var source []string
source = request.Form[ "source" ]
sourceStrings := make( []string, len( source ) )
i := 0
// move the source urls into a new array so we can preserve the order, but skip blank entries
for _, v := range source {
if v != ""{
sourceStrings[ i ] = v
i++
}
}
compileParamString := "compilation_level=SIMPLE_OPTIMIZATIONS"
compileParamString += "&output_format=text"
compileParamString += "&output_info=compiled_code"
for _, v := range sourceStrings[ :i ]{
compileParamString += "&code_url=" + v
}
compileParams, _ := url.ParseQuery( compileParamString )
compileResponse, _ := http.PostForm( compilerURL, compileParams )
result, _ := ReadWholeFile( compileResponse.Body )
if len( result ) == 1 {
http.Error( response, "Something went wrong with the Closure Compiler", http.StatusInternalServerError )
return
}
currentTime := time.LocalTime().Nanoseconds()
dirName := "out/" + strconv.Itoa64( currentTime )
fileName := dirName + "/" + request.Form[ "name" ][ 0 ] + ".min.js"
if err := os.Mkdir( dirName, uint32( 0777 ) ); err != nil{
http.Error( response, "Couldn't create output folder", http.StatusInternalServerError )
return
}
outputFile, err := os.Create( fileName )
if err != nil{
http.Error( response, "Couldn't create output file", http.StatusInternalServerError )
return
}
_, err = outputFile.WriteString( result )
if err != nil{
http.Error( response, "Couldn't write output to file", http.StatusInternalServerError )
return
}
http.Redirect( response, request, "/" + fileName, http.StatusFound )
}
// Server static files, like CSS and JS directly
func StaticFile( response http.ResponseWriter, request *http.Request ){
url := request.URL.Path
// remove the beginning slash so we don't look in the FS root for the file
if url[ 0 ] == '/' {
url = url[1:]
}
if strings.Contains( url, "../" ){
log.Println( "Attempt to traverse outside of static folders." )
http.Error( response, "Stop it.", http.StatusInternalServerError )
return
}
// Log that a static file was served
log.Print( "Serving static file ", url )
// serve the file
http.ServeFile( response, request, url )
}
func main(){
if tmplSet, err := template.ParseSetFiles( "tmpl/header.html", "tmpl/footer.html", "tmpl/index.html" ); err != nil {
log.Fatal( "Couldn't parse templates: ", err.String() )
}else{
tmpls = tmplSet
}
http.HandleFunc( "/out/", StaticFile )
http.HandleFunc( "/css/", StaticFile )
http.HandleFunc( "/js/", StaticFile )
http.HandleFunc( "/smush", SmushFiles )
http.HandleFunc( "/", SmushIndex )
if err := http.ListenAndServe( ":12345", nil ); err != nil {
log.Fatal( "ListenAndServe: ", err.String() )
}
}